1

I'm struggling with something which I can't understand why it is happening .

Looking at this example :

 const source = Rx.Observable.of(1).share();

 source.subscribe(console.log); //1
 source.subscribe(console.log); //1

This prints "1" twice. AFAIK share looks at refCount. But if we look at it - refcount should be ZERO here :

const source = Rx.Observable.of(1).share();

 source.subscribe(console.log); 
             ^--  1)refCount=1
                  2)value emitted - closing subscription ( complete)
                  3)refCount=0

 source.subscribe(console.log);
            ^-- does refCount is 1 again or  is it Zero ?

DEMO 1

Also - Things get more complicated when the observer is not completed

const source = Rx.Observable.create((o)=>o.next(1)).share();

 source.subscribe(console.log); //1
 source.subscribe(console.log); //nothing

^This only yield one value

Demo2

Question

Is my refCount observation was correct and why there are different results between the two examples ?

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • 1
    good question. I believe the share operator in rxjs v5 reconnects after completion. That might explain what you see in Demo1. Demo2 is pretty much what it has always been in all versions of rxjs. To understand properly what is happening, have a look at https://stackoverflow.com/questions/35141722/how-does-the-rxjs-5-share-operator-work, and then https://stackoverflow.com/questions/32190445/hot-and-cold-observables-are-there-hot-and-cold-operators/34669444#34669444 – user3743222 Mar 29 '18 at 17:10
  • In short, in Demo2, when you subscribe, your `1` value is emitted **synchronously** and received by the first subscriber. When the second subscriber subscribes, well, that `1` value is already gone. If you would emit that `1` value asyncronously, both subscribers would see that value. – user3743222 Mar 29 '18 at 17:12
  • @user3743222 OK , so say I have a (closed) library which emits values synchronously. Is there any rxjs operator which I can chain to the results to make it asynchrounsly ? something like `myStream$.map(f=>makeASync(f))` ? – Royi Namir Mar 30 '18 at 11:25
  • in rxjs v4, you could do `myStream$.observeOn(Rx.Scheduler.async)` for instance. There are schedulers too in v5, but I am not cogniescent on what are the operators to use them, but I guess there should be some equivalent way to do that too. See [this](https://stackoverflow.com/questions/28145890/what-is-a-scheduler-in-rxjs) for an explanation on schedulers. – user3743222 Mar 30 '18 at 15:59

1 Answers1

2

Your refCount observation is correct.

On a shared Observable, if (and only if) the refCount resets to 0, then any new subscription would recreate the source Observable.

Demo 1: refCount resets after each subscription

Demo 2: the refCount would never reset since the subscriptions won't complete.

A third example:

const source = Rx.Observable.create((o)=>o.next(1)).share();

source.take(1).subscribe(console.log); //1
source.take(1).subscribe(console.log); //1
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.5.8/Rx.js"></script>
Audwin Oyong
  • 2,247
  • 3
  • 15
  • 32
ZahiC
  • 13,567
  • 3
  • 25
  • 27
  • So how can other consumers get the shared data in demo2? That's the idea, to share data no ? Idont see any reason why observalbe emit data, refcount>0 and the other subscriber wont get the data – Royi Namir Mar 30 '18 at 04:45
  • The terminology here is not "shared data" but "shared observable". The idea of shared Observable is to make sure up to one Observable is created simultaneously. If you want to get all the previously emitted data on subscribing, check the replay operators. – ZahiC Mar 30 '18 at 11:15
  • If so , what is happening here with refcount pov ?http://jsbin.com/venabadugu/1/edit?js,console - result is two values. – Royi Namir Mar 30 '18 at 11:20
  • The source observable is not immediately completed, so the second subscribe shares the same Observable. The refcount is 2 after the second subscribe – ZahiC Mar 30 '18 at 13:01
  • Zahi I believe this should be added : _On a shared Observable, if (and only if) the refCount resets to 0, **and source is completed** then any new subscription would recreate the source Observable._ – Royi Namir Oct 19 '18 at 14:25