Skip to content

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

@stephane-vanraes

Description

@stephane-vanraes

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions