fix(storage): flush pending debounced writes on unmount instead of discarding them - #725
Open
Matthew-Selvam wants to merge 1 commit into
Open
Conversation
…scarding them The unmount cleanup cleared the debounce timer without flushing, and pendingCollectionsRef — a per-mount ref — was dropped with the component. An edit made within the 500ms debounce window of navigating away existed only in localStorage; the next mount's pullFromServer() then overwrote localStorage with server data, destroying the write permanently. The cleanup now flushes whatever is pending (server mode only, non-empty queue only) before tearing down. A failed post-teardown flush still re-queues its collections and the existing !mountedRef.current guard keeps it from arming stray timers.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The
useStorageSyncunmount cleanup cleared the debounce timer without flushing, andpendingCollectionsRef— a per-mountuseRef— was dropped with the component. An edit made within the 500ms debounce window of navigating away existed only in localStorage; the next mount'spullFromServer()then unconditionally overwrote localStorage with server data, destroying the write permanently and silently.Fix
The cleanup now flushes whatever is pending before tearing down:
serverModeRef— in local mode the queue is always empty and the PUT would be pointless work.!mountedRef.currentguard already keeps it from arming stray retry timers — so the previous fix for the dead-hook retry loop is unaffected.Test plan
a pending debounced write is flushed on unmount, not discarded— edit dispatched inside the debounce window, unmount, assert the PUT landsunmount with nothing pending does not push— asserts the flush is conditionala push that fails after unmount does not leave a timer runningstill passes (in-flight push ≠ pending push; no double-send)Found during a broader code review of the storage sync layer; no issue existed yet for it.