Omnistac / zedux

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

fix(react): make `useAtomSelector` wait for idle time to do failsafe cleanup #113

Closed bowheart closed 2 months ago

bowheart commented 3 months ago

Description

useAtomSelector's failsafe queueMicrotask is a little too aggressive and can (very rarely) lead to race conditions with React's own queued microtasks. #106 and Zedux v1.3.x already fix this for React 19, but React 18 is still the current version and still needs a better (temporary) fix.

Use requestIdleCallback (falling back to setTimeout in node) instead of queueMicrotask. This means (in StrictMode in React 18) that some leaked selector nodes will be visible for a short time before Zedux circles back and cleans them up, but it's better than being too aggressive and cleaning up graph edges that shouldn't be.