• 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.

    Type Parameters

    • T

    Parameters

    • newVal: T
    • equals: ((v1: T, v2: T) => boolean) = arrayEq
        • (v1: T, v2: T): boolean
        • Parameters

          • v1: T
          • v2: T

          Returns boolean

    Returns T

    Remarks

    • If the reactive expression's last "value" was an error, the new value is returned
    • An error will be thrown if this function is called outside a reactive expression or from within a peek() call or action wrapper.