Unexpected equalityComparer behavior

91 views
Skip to first unread message

Antonio Salazar Cardozo

unread,
May 19, 2016, 6:31:34 PM5/19/16
to KnockoutJS
Hey there,
We ran into some unexpected behaviors with equalityComparer in Knockout 3.4
both with deferred updates on and off, and realized this was actually a combination
of unexpected behavior from pre-3.4 and new unexpected behavior from 3.4 with
deferred updates.

These are exemplified here:
 - https://jsfiddle.net/r39vwz3z/4/#run (3.4, deferred updates off)
 - https://jsfiddle.net/r39vwz3z/5/#run (3.4, deferred updates on)

The fiddle in question sets up an observable array and binds to it with the `checked`
binding. It also creates a computed that depends on the list, and subscribes directly
to the list. Each one of these appends to a visible log to indicate that it has fired and
what value of the list it's seeing. Finally, we set a custom equality comparer and log
what it receives.

In 3.3 and 3.4 with no deferred updates, the equality comparer is never invoked. My
suspicion is that we've actually just been setting the comparer incorrectly all along.

In 3.4 with deferred updates, the comparer is used—but both its parameters are the
final version of the array, rather than having the version before the check that's currently
going into effect and the version after. So no matter how clever one wants to get, the
equality comparer will always return true for a deep comparison.

Not sure what the right move is here. Should I file an issue? Or is this expected
behavior? Is the setup of equalityComparer that I use incorrect anyway, regardless
of how deferred updates affect it?
Thanks,
Antonio

PS: We used to use knockout-deferred-updates with 3.3, which behaves the same
as 3.3 and 3.4 with no deferred updates; see https://jsfiddle.net/r39vwz3z/6/#run .

Michael Best

unread,
May 20, 2016, 10:04:35 PM5/20/16
to KnockoutJS
To get notified of the changes to an array, you can use the arrayChange event. Unfortunately, this isn't documented on knockoutjs.com, but it is described on Steve's blog: http://blog.stevensanderson.com/2013/10/08/knockout-3-0-release-candidate-available/

equalityComparer cannot be used to track array changes as you've discovered. The fact that it is called in some instances is just an artifact of the implementation.

-- Michael

Antonio Salazar Cardozo

unread,
Jul 17, 2016, 9:14:38 PM7/17/16
to knock...@googlegroups.com
Sorry for the absurdly late reply here—I see what you’re saying. Thanks for
the extra details. For what it’s worth, I think the most confusing thing about
this is that it works inconsistently—it would be useful for the discovery process
if it at least consistently didn’t work :)

Thanks again for the detailed reply!

--
Antonio Salazar Cardozo
On twitter @lightfiend

--
You received this message because you are subscribed to a topic in the Google Groups "KnockoutJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/knockoutjs/2lj89SXHGJ8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to knockoutjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

signature.asc

Michael Best

unread,
Jul 20, 2016, 2:12:42 PM7/20/16
to KnockoutJS
I've created an issue in Github: https://github.com/knockout/knockout/issues/2098
Reply all
Reply to author
Forward
0 new messages