Fix: guard resetPersistence when not initialized in syncState.reset#627
Open
claushaas wants to merge 1 commit intoLegendApp:mainfrom
Open
Fix: guard resetPersistence when not initialized in syncState.reset#627claushaas wants to merge 1 commit intoLegendApp:mainfrom
claushaas wants to merge 1 commit intoLegendApp:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a runtime TypeError that occurs when reset() is called on a synced observable before its persistence layer has been initialized. The issue arises because resetPersistence is only assigned during the asynchronous loadLocal function, but the type system assumes it's always defined.
Changes:
- Added optional chaining and nullish coalescing to safely call
resetPersistence?.()in thereset()function - Falls back to
Promise.resolve()whenresetPersistenceis undefined
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Context
When syncStateValue.reset() is called early (before loadLocal initializes persistence), resetPersistence is still undefined. reset() calls syncStateValue.resetPersistence() unconditionally, which throws:
TypeError: syncStateValue.resetPersistence is not a function (it is undefined).
Steps to reproduce
Call syncState(obs).reset() immediately after creating a synced observable.
In async persistence setups, reset can run before loadLocal finishes.
Root cause
resetPersistence is only assigned inside loadLocal(...). Until then it’s undefined, but reset() calls it without a guard.
Change
Replace the direct call with a safe optional call and fallback.
How this fixes it
Prevents the exception and allows reset() to work even when called before persistence initialization, without changing behavior once resetPersistence exists.
Patch
Notes
This is backward-compatible and no-op when persistence has already been initialized.
If you want me to retry PR creation with a different auth method, tell me what you prefer.