sveltejs / svelte

Cybernetically enhanced web apps
https://svelte.dev
MIT License
78.94k stars 4.15k forks source link

Provide current value of store in second argument of stores #7520

Open stephane-vanraes opened 2 years ago

stephane-vanraes commented 2 years ago

Describe the problem

This is similar to https://github.com/sveltejs/svelte/issues/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

rmunn commented 2 years ago

Similar to (but slightly different from) https://github.com/sveltejs/svelte/pull/6750, which was deemed a breaking change as it would change the Typescript signature of stores.

Also worth noting https://github.com/sveltejs/svelte/pull/6750#issuecomment-1122294203 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 commented 2 years ago

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

niedzielski commented 1 year ago

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 commented 1 year ago

@rmunn was this implemented in v4 ?