zedux icon indicating copy to clipboard operation
zedux copied to clipboard

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

Open bowheart opened this issue 6 months ago • 0 comments

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.

bowheart avatar Aug 10 '24 18:08 bowheart