When a subscriber modifies another subscriber's value, the second subscriber processes its results out of order.
Sample code:
subject = new Rx.BehaviorSubject()
subject.next({value: 'Test1'})
v1 = subject.pluck('value').distinctUntilChanged()
v2 = subject.pluck('value2').distinctUntilChanged()
h1 = v1.subscribe(function(it) { if (it == 'Test2') subject.next({value: it, value2: true}) })
h2 = v2.subscribe(function(it) { console.log('v2', it) })
subject.next({value: 'Test2'})
Expected results:
v2 undefined
v2 true
Actual results:
v2 undefined
v2 true
v2 undefined
What did I do wrong?
from v5, rxjs does not gaurantee reentrancy (trying to update values of source in your subscribe) works in order. this is due to observable sources are not do specific scheduling by default, so most of synchronous sources's subscription runs synchronously, while some others don't. If you'd like to achieve certain execution order with reentrancy, you should scheduler observable accordingly for your desired behavior, either creating observable with scheduler (static creation method usually accept scheduler) or use observeOn
/ subscribeOn
.