Omnistac / zedux

:zap: A Molecular State Engine for React
https://Omnistac.github.io/zedux/
MIT License
344 stars 7 forks source link

feat(atoms): add `runOnInvalidate` option to `injectPromise` #69

Closed bowheart closed 1 year ago

bowheart commented 1 year ago

Description

With query atoms, calling instance.invalidate() reevaluates the atom, thus recreating the promise. When migrating from a query atom to using injectPromise (a common task when more power is needed), instance.invalidate() is no longer sufficient to make the promise rerun.

There are many workarounds, but all are pretty inconvenient. This PR adds a runOnInvalidate config option to injectPromise itself to get the query atom behavior back.

const myPromiseAtom = atom(
  'myPromise',
  () => injectPromise(myPromiseFactory, [], { runOnInvalidate })
)

const myPromiseInstance = myEcosystem.getInstance(myPromiseAtom)

// now this will cause `myPromiseFactory` to rerun, updating the store's loading status:
myPromiseInstance.invalidate()