Why does the second subscription not get any values ​​when subscribing twice to the one observed in rx 2.3?

I have this barebones example that does not behave as I expect based on rxjs documentation . I expect subscriptions to receive all values.

The docs mention:

Then two observers subscribe to this sequence and print out its values. You will notice that the reset sequence for each subscriber in which the second subscription will restart the sequence from the first value.

let s1 = rx.Observable.from([1, 2, 3, 4, 9, 11]) s1.subscribe( x => console.log(x), x => console.log(x), x => console.log('complete')) s1.subscribe( x => console.log(x), x => console.log(x), x => console.log('complete')) 

However, the second subscription simply registers 'complete'

As it turned out, the example works as expected in rxjs 2.4, but not in 2.3. Does anyone know what has changed? I can not define this in the release notes

Here is jsfiddle with 2.3.20: fiddle

and here is one of them: 2.4.1: fiddle

+5
source share
1 answer

This is the wrong behavior for the observed cold. An observable created from an array is a cold observable and cannot share a subscription with more than one observer. For proper operation, you can turn the observable into the hot observable. You can see this documentation http://xgrommx.imtqy.com/rx-book/content/observable/observable_instance_methods/publish.html . In your case, you can use something like http://jsbin.com/mowaco/edit?js,console . Now both subscriptions work in parallel.

+2
source

Source: https://habr.com/ru/post/1215515/


All Articles