-
-
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 update as second argument parameter to stores #6737
Comments
Another possible non-breaking-change-way: |
The example use cases look like something that would be easier with derived stores, at least if derived stores also got the same const store1 = writable()
const store2 = derived(store1, (value, (set, update)) => {
update(current => [...current, value])
}, []); |
Not really, a derived store just maps one thing in another, it doesn't hold it's own state. |
Derived stores do hold their own state, and if you use the const even = derived(count, (value, set) => {
if (value % 2 === 0) {
set(value);
}
}); This isn't a simple map, because it's dropping half the values (assuming Derived stores using import { derived } from 'svelte/store'
const noop = (x) => x;
export function debounce(input, timeoutMs, fn = noop) {
return derived(input, (newValue, set) => {
const timeout = setTimeout(() => set(fn(newValue)), timeoutMs);
return () => clearTimeout(timeout);
});
} |
The (non-breaking) solution I came up with for my store library was to have the Would be great if svelte adopted the same syntax, but I'm obviously biased 😊 See: https://whenderson.github.io/stores-mono/modules/_crikey_stores_svelte.html#derive |
@WHenderson just a heads-up you should check Svelte v4, this issue has been fixed as a "breaking change" and your custom store is likely no longer compatible with the expectation users might have. Relevant PR: #6750 |
Thanks @stephane-vanraes , I appreciate the heads up :-) |
Describe the problem
Currently the constructor for a writable store takes as a second argument function that is fired when the store gets a first subscriber, the return of this is fired when the store goes from 1 to 0 subscriber.
This function gives you access to the
set
function of the store, but in some cases it might be beneficial to actually have access to theupdate
instead.This would allow to do something like for example this
Describe the proposed solution
Not 100% sure
Alternatives considered
Use some kind of construction with a temporary variable
Importance
nice to have
The text was updated successfully, but these errors were encountered: