Skip to content

Fix: reset synced observable#571

Open
FelipeFlamarini wants to merge 3 commits intoLegendApp:mainfrom
FelipeFlamarini:fix/reset-double-call-and-isloaded-state
Open

Fix: reset synced observable#571
FelipeFlamarini wants to merge 3 commits intoLegendApp:mainfrom
FelipeFlamarini:fix/reset-double-call-and-isloaded-state

Conversation

@FelipeFlamarini
Copy link

@FelipeFlamarini FelipeFlamarini commented Aug 21, 2025

This PR was developed when I started having problems trying to reset a syncedCrud observable with syncState.reset().

  • fix syncState.reset() not updating observable's value when the initial value of a synced observable is an empty object
  • fix syncState.reset() not updating observable's value when called multiple times (I had this issue in an Expo to-dos test page, where I set up a synced observable and added one to-do, then reset, then added one to-do again, then reset again. On the second reset, the observable's value didn't update)
  • fix syncState.isLoaded not being set correctly when resetting a synced observable that didn't have the get property (e.g. syncedCrud with list property)
  • add tests for the use cases fixed in this PR

@MCW77
Copy link
Contributor

MCW77 commented Sep 4, 2025

Had the same problem with multiple resets. See https://discord.com/channels/1241991273322119250/1341015670539681843/1341015675904331776

Your fix has one problem. It clones the value to reset to and thus reset will throw if json.parse or json.stringify used by clone throw.
legend-state adds Date, Map and Set serialization on top of what JSON.stringify does. Anything else in the reset value will make reset throw. On top of what you already did in your fix, we could run available transforms on the reset value to avoid this.

A much easier and faster solution would be to not have a reset value in the syncedOptions but a function returning the reset value. Docs would have to state that the function returning the reset value should not return the same reference.
initial already is typed (() => T) | T | undefined. So we can just remove the T option and set the obs to the value returned by the function passed to initial or undefined.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants