Last active
November 9, 2015 04:30
-
-
Save bouzuya/16bff4407bd356c8a202 to your computer and use it in GitHub Desktop.
Rx.Observable.prototype.distinctUntilChanged のおかしな挙動
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe 'distinctUntilChanged', -> | |
it 'comparer', -> | |
isEqual = require 'lodash.isequal' | |
subjectX = new Rx.Subject() | |
subjectX | |
.distinctUntilChanged null, isEqual | |
.toArray() | |
.subscribe (a) -> | |
# currentKey was updated by `o.value = 2` | |
assert a.length is 1 | |
[o1] = a | |
assert.deepEqual o1, value: 2 # oh... | |
o = {} | |
o.value = 1 | |
subjectX.onNext o | |
o.value = 2 | |
subjectX.onNext o | |
subjectX.onCompleted() | |
it 'key selector', -> | |
hash = require 'object-hash' | |
subjectX = new Rx.Subject() | |
subjectX | |
.distinctUntilChanged hash | |
.toArray() | |
.subscribe (a) -> | |
assert a.length is 2 | |
[o1, o2] = a | |
assert.deepEqual o1, value: 2 # oh... | |
assert.deepEqual o2, value: 2 | |
o = {} | |
o.value = 1 | |
subjectX.onNext o | |
o.value = 2 | |
subjectX.onNext o | |
subjectX.onCompleted() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
原因としては
currentKey
として保持している直前の値はその参照を書き換えられると、比較が正しく動かないため。