Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Fix effect cleanup #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

KidkArolis
Copy link

Noticed this issue happen in a real app scenario.

Sometimes, dispatching flushEffectsSymbol causes entitiesToStop to be cleared out from the state, without the cleanup ever happening.

This was puzzling me at first, because you'd think that if there were any entitiesToStop, then they would be stopped before the flushEffectsSymbol ever gets a chance to be dispatched. But without this patch, calling fireEvent.click(goodbyeButton); twice right after another causes 2 "goodbye" messages to be logged instead of just the one.

In particular, I think what's going on is that when the 2nd goodbye click happens, react batches the dispatch START and dispatch flushEffectsSymbol together before executing the cleanup effect. The sequence is the following:

> Click 1

Dispatch: START
Reducer: Queues up the timeout effect

> Click 2

Dispatch: START (but this time effects need to be flushed before running the reducer!)
Effect: Runs cleanup useEffect with { entitiesToStop: [] }
Effect: Runs execution useEffect that also dispatches flushEffectsSymbol
Reducer: Queues up the new timeout effect
Reducer: Handles flushEffectsSymbol wiping the entitiesToStop (!)

Effect: Runs cleanup useEffect with { entitiesToStop: [] }
Effect: Runs execution useEffect that also dispatches flushEffectsSymbol
Reducer: Handles flushEffectsSymbol

Effect: Runs cleanup useEffect with { entitiesToStop: [] }
Effect: Runs execution useEffect that also dispatches flushEffectsSymbol

Nothing new is dispatched this time around, we've settled

The fix makes sure we never lose any entitiesToStop, and that we do clear them from the state when they have actually been stopped.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant