tc39 / proposal-signals

A proposal to add signals to JavaScript.
MIT License
2.87k stars 54 forks source link

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

Open yw662 opened 1 month ago

yw662 commented 1 month ago

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(...))
titoBouzout commented 1 month ago

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)