Skip to content

Instantly share code, notes, and snippets.

@bouzuya
Last active November 9, 2015 04:30
Show Gist options
  • Select an option

  • Save bouzuya/16bff4407bd356c8a202 to your computer and use it in GitHub Desktop.

Select an option

Save bouzuya/16bff4407bd356c8a202 to your computer and use it in GitHub Desktop.
Rx.Observable.prototype.distinctUntilChanged のおかしな挙動
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()
@bouzuya
Copy link
Copy Markdown
Author

bouzuya commented Nov 9, 2015

原因としては currentKey として保持している直前の値はその参照を書き換えられると、比較が正しく動かないため。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment