-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide current value of store in second argument of stores #7520
Comments
Similar to (but slightly different from) #6750, which was deemed a breaking change as it would change the Typescript signature of stores. Also worth noting #6750 (comment) from @WHenderson which suggests that v4 should make the second parameter an object with multiple properties, |
I think I have the same problem. I just want the final value in the teardown callback. I use update as a workaround. export const text: Writable<string> = writable<string>(
localStorage.getItem('text') ?? '',
(_, update) => {
return () =>
// hack: re-update the store with the same value just to get the current
// value; it'd be ideal if we were given the val in cleanup
update(text => {
localStorage.setItem('text', text)
return text
})
}
) |
@rmunn was this implemented in v4 ? |
Closing since in Svelte 5 runes are the primary mechanism for universal reactivity, and it's somewhat straightforward to create a store wrapper in userland that does this. |
Describe the problem
This is similar to #6737 but 'different'.
When using the current setup to create a store, you are in the following situation:
You cannot use
get(store)
in the final return as this would trigger a sub/unsub cycle, which in turn would trigger this block again and cause an infinite loop. Not 100% sure if you could use it in the first part (the setter).Having this final value can be interesting if you want to make an api call storing the value but only when all subscribers are gone.
Describe the proposed solution
Get an extra argument to either signatures:
This will probably be a breaking change (maybe not if we only provide it on the
stop
function), so might be put as a consideration for Svelte v4.Alternatives considered
This is possible with a custom store, that holds an extra variable to store the current value.
Importance
would make my life easier
The text was updated successfully, but these errors were encountered: