derivablejs icon indicating copy to clipboard operation
derivablejs copied to clipboard

Second withEquality argument is wrong on first call (Bug?)

Open zerkalica opened this issue 7 years ago • 3 comments

First case:

        const a = atom({p: 1})
        const c = a.derive(o => o)
            .withEquality((oldObj, newObj) => {
                console.log(oldObj, newObj)
                return oldObj === newObj
            })

        c.react((cObj) => {
            // console.log(cObj)
        })
        a.set({p: 2})

console output:

{ p: 1 } { equals: [Function: equals] }
{ p: 2 } { p: 1 }

Why { equals: [Function: equals] } ?

Second case:

        const a = atom({p: 1})
        const c = a
            .withEquality((oldObj, newObj) => {
                console.log(oldObj, newObj)
                return oldObj === newObj
            })

        c.react((cObj) => {
            // console.log(cObj)
        })
        a.set({p: 2})

No console output: withEquality callback never calls.

zerkalica avatar Oct 02 '16 10:10 zerkalica

Why { equals: [Function: equals] } ?

That second argument is the 'initial' value of the derivation, before it has actually been calculated once. It is a private object with a .equals method that always returns false.

Second case [...] No console output [...]

That is a bug. Thanks for the report :) Is this with the latest beta release?

ds300 avatar Oct 02 '16 10:10 ds300

No, it's stable version 0.12.1

In first case: {equals} object is strange here, may be pass null?

export type WithEqualsCb<V> = (a: V, b: ?V) => boolean

zerkalica avatar Oct 02 '16 11:10 zerkalica

I think, first call of withEquality callback is unnecessary in all cases. Call it only when value changed, just skip first.

zerkalica avatar Oct 02 '16 11:10 zerkalica