unchangedIf<T>(newVal: T, equals?: ((v1: T, v2: T) => boolean)): T
Keep an expression's old value unless there's a semantic change
By default, reactive values (i.e. cached(), or value() with a
setf()) are considered to have "changed" (and thus
trigger recalculation of their dependents) when they are different according
to === comparison.
This works well for primitive values, but for arrays and objects it's not
always ideal, because two arrays can have the exact same elements and still
be different according to ===. So this function lets you substitute a
different comparison function (like a deep-equal or shallow-equal) instead.
(The default is arrayEq() if no compare function is supplied.)
Specifically, if your reactive expression returns unchangedIf(newVal, compare), then the expression's previous value will be kept if the compare
function returns true when called with the old and new values. Otherwise, the
new value will be used.
Keep an expression's old value unless there's a semantic change
By default, reactive values (i.e. cached(), or value() with a setf()) are considered to have "changed" (and thus trigger recalculation of their dependents) when they are different according to
===
comparison.This works well for primitive values, but for arrays and objects it's not always ideal, because two arrays can have the exact same elements and still be different according to
===
. So this function lets you substitute a different comparison function (like a deep-equal or shallow-equal) instead. (The default is arrayEq() if no compare function is supplied.)Specifically, if your reactive expression returns
unchangedIf(newVal, compare)
, then the expression's previous value will be kept if the compare function returns true when called with the old and new values. Otherwise, the new value will be used.