Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does second subscribtion not receive any values when subscribing twice to observable in rx 2.3?

I have this barebones example not behaving as I expect based on the documentation of rxjs. I would expect the subscriptions to both receive all values.

The docs mention:

Two observers then subscribe to this sequence and print out its values. You will notice that the sequence is reset 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 just logs 'complete'

As it turns out the example works as expected in rxjs 2.4, but not in 2.3. Does anyone know what changed? I cannot spot it in the release notes

Here is a jsfiddle with 2.3.20: fiddle

and here is one with 2.4.1: fiddle

like image 319
Remko Avatar asked Nov 01 '22 08:11

Remko


1 Answers

This not right behavior for a cold observable. An observable which created from an array is cold observable and cannot share subscription more than on one observer. For correct work you can transform your observable to a hot observable. You can look on this documentation http://xgrommx.github.io/rx-book/content/observable/observable_instance_methods/publish.html. In your case you could use something like this http://jsbin.com/mowaco/edit?js,console. Now both subscriptions working parallel.

like image 138
xgrommx Avatar answered Nov 08 '22 09:11

xgrommx