Skip to content

Provide update as second argument parameter to stores #6737

@stephane-vanraes

Description

@stephane-vanraes

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 the update instead.

This would allow to do something like for example this

const store1 = writable()
const store2 = (() => {
   const { subscribe } = writable([], update=> {
     return store1.subscribe(value => {
       update(current => [...current, value])
     })
   })
   return { subscribe }
})()

Describe the proposed solution

Not 100% sure

  • change the signature ? the update function can be used a set as well (breaking change though)
  • add a third argument ?

Alternatives considered

Use some kind of construction with a temporary variable

const store1 = writable()
const store2 = (() => {
   let arr = []
   const { subscribe } = writable(arr, set=> {
     return store1.subscribe(value => {
      arr = [...arr, value]
       set(arr)
     })
   })
   return { subscribe }
})()

Importance

nice to have

Metadata

Metadata

Assignees

No one assigned

    Labels

    runtimeChanges relating to runtime APIs

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions