With the Connect () method on a connectableobservable, the programmer are responsible for avoiding leaked executions of Shar Ed RxJS observables. This lesson'll teach you on RefCount (), a handy operator that creates an automatically connected Observable, to avoid Manually using Connect ().
After multicast (new Rx.subject ()), we call RefCount (), so it would help us to manage the connections, so we don ' t need to W Orry about the memory leak.
varShared = Rx.Observable.interval ( +) . Do(x = Console.log ('Source'+x). Multicast (NewRx.subject ()). RefCount ();varObservera ={next:function (x) {Console.log ('A Next'+x); }, Error:function (err) {Console.log ('A Error'+err); }, Complete:function () {Console.log ('A Done'); },};varSubA = Shared.subscribe (Observera);//StartvarObserverb ={next:function (x) {Console.log ('B Next'+x); }, Error:function (err) {Console.log ('B Error'+err); }, Complete:function () {Console.log ('B Done'); },};varsubb;settimeout (function () {Subb= Shared.subscribe (Observerb);//1 = 2}, -); SetTimeout (function () {suba.unsubscribe ();//2 = 1Console.log ('unsubscribed A');}, the); SetTimeout (function () {subb.unsubscribe ();//1 = 0 (stop)Console.log ('unsubscribed B');}, 7000);/*"Source 0" "A Next 0" "Source 1" "A Next 1" "Source 2" "A Next 2" "B Next 2" "Source 3" "A Next 3" "B Next 3" "Source 4" "a Next 4 "" B Next 4 "" Unsubscribed A "" Source 5 "" B Next 5 "" Source 6 "" B Next 6 "" Unsubscribed B "*/
[RxJS] Refcount:automatically starting and stopping an execution