withDiff()
When writing incrementally-computed signals it is convenient (and usually more performant) to incrememtally compute the diff too.
You can use this function to wrap the return value of a computed signal function to indicate that the diff should be used instead of calculating a new one with AtomOptions.computeDiff.
Example
const count = atom('count', 0)
const double = computed('double', (prevValue) => {
const nextValue = count.value * 2
if (isUninitialized(prevValue)) {
return nextValue
}
return withDiff(nextValue, nextValue - prevValue)
}, { historyLength: 10 })
Signature
withDiff<Value, Diff>(value: Value, diff: Diff): WithDiff<Value, Diff>;
Type parameters
Value
Diff
Parameters
Name | Type | Description |
---|---|---|
value | Value | The value. |
diff | Diff | The diff. |
Returns
WithDiff
<Value
, Diff
>
Defined in: signia/src/Computed.ts:69