proposal-signals icon indicating copy to clipboard operation
proposal-signals copied to clipboard

[FER] change list, diff and previous values in Computed and watch

Open yw662 opened this issue 1 year ago • 1 comments

Computed signals may depend on not only the current values of other signals, but also the previous value of itself. Eg.

const currentMsg = new Signal.State({ sender: 'foo', msg:'bar' })
const chatHistory = new Signal.Computed(prev => [...prev ?? [], currentMsg.get()])

It may also use a change list to reduce calculation:

const a = new Signal.State...
const b = new Signal.State...
const computed = new Signal.Computed((prev, changes) => ({...prev, ...changes.includes(a) ? ... :{},  ...changes.includes(b) ? ... :{}))

And a diff of the changes:

const a = new Signal.State([1, 2, 3])
const computed = new Singal.Computed((prev, changes, {a:diff_a}) => prev ? applyDiff(prev, diff_a) : a.map(...))

yw662 avatar Apr 15 '24 06:04 yw662

In Solid, effects and computeds have an argument to use the last returned value. related https://github.com/tc39/proposal-signals/issues/92 (signal setter using previous value)

titoBouzout avatar Apr 18 '24 18:04 titoBouzout