Skip to content
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

Closed
stephane-vanraes opened this issue May 9, 2022 · 5 comments
Closed
Labels
feature request runtime Changes relating to runtime APIs

Comments

@stephane-vanraes
Copy link
Contributor

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:

const store = writable(0, (set) => {
  // current store value not available here
  return () => {
    // final store value not available here
  }
}

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:

const store = writable(0, (set, current) => {
  return (current) => {
  }
})

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

@dummdidumm dummdidumm added feature request runtime Changes relating to runtime APIs labels May 9, 2022
@rmunn
Copy link
Contributor

rmunn commented Jul 26, 2022

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, set among them, so that future changes to the API can be made in a backwards-compatible way by just adding another property. I agree that that seems the best approach for stores in Svelte v4.

@rmunn
Copy link
Contributor

rmunn commented Jul 26, 2022

Oh, of course: #6750 was a PR to implement the suggestion in #6737. I knew that looked familiar... 😊

@niedzielski
Copy link

niedzielski commented Jul 19, 2023

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
      })
  }
)

@stephane-vanraes
Copy link
Contributor Author

@rmunn was this implemented in v4 ?

@dummdidumm
Copy link
Member

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.

@dummdidumm dummdidumm closed this as not planned Won't fix, can't repro, duplicate, stale Nov 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request runtime Changes relating to runtime APIs
Projects
None yet
Development

No branches or pull requests

4 participants