cefn / lauf

Monorepo for lauf packages.
MIT License
5 stars 0 forks source link

Race condition in useSelected() #157

Closed cefn closed 3 years ago

cefn commented 3 years ago

At the time of raising this issue the useEffect call within the useSelected hook carries out the subscription to state. This therefore takes place separately from the render in which the first 'snapshot' of a selector is made.

https://github.com/cefn/lauf/blob/67f47c4ba3e7672ac6e037b9b32b678b054c26e8/modules/store-react/src/index.ts#L50-L70

This creates a potential race condition. Between the snapshot and the subscription, an change can be made to the selected state which is not yet subscribed for notification.

This logical hole should be closed to ensure that all edits are detected, including those happening in the moment between the render-inline-call to useSelected and the tick in which the useEffect call follows.