derivablejs
derivablejs copied to clipboard
Second withEquality argument is wrong on first call (Bug?)
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.
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?
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
I think, first call of withEquality callback is unnecessary in all cases. Call it only when value changed, just skip first.