Skip to content

Proposal: pass invalidate function to derived function handler #3233

@btakita

Description

@btakita

This feature would allow async derived handlers to determine when they are invalid:

derived([a, b], async ([$a, $b], set, invalidate) => {
  invalidate()
  const [$a_, $b_] = await Promise.all([a_fn($a), b_fn($b)])
  set($a_ + $b_)
})

This would also enable multi-step derived state machines without diamond dependency issues:

derived([a, b], async ([$a, $b], set, invalidate) => {
  invalidate()
  const $a_ = await a_fn($a)
  set({ step: 1, $a_ })
  invalidate()
  const $b_ = await b_fn($b)
  set({ step: 2, $a_, $b_ }
})

A con against this is that it would add an additional api item & will only be useful for async derived stores.

This idea originated from #3219 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions