首页 > 代码库 > [RxJS] Stopping a shared observable execution
[RxJS] Stopping a shared observable execution
ConnectableObservable has the connect() method to conveniently dictate the start of the shared execution of the source Observable. However, we need a mechanism to dictate the stop of the shared execution, otherwise a leak happens. This lesson will teach you how to do that, and it‘s all about Subscriptions.
var connectableObservable = Rx.Observable.interval(1000) .do(x => console.log(‘source ‘ + x)) .multicast(new Rx.Subject());var observerA = { next: function (x) { console.log(‘A next ‘ + x); }, error: function (err) { console.log(‘A error ‘ + err); }, complete: function () { console.log(‘A done‘); },};var sub = connectableObservable.connect(); // startvar subA = connectableObservable.subscribe(observerA);var observerB = { next: function (x) { console.log(‘B next ‘ + x); }, error: function (err) { console.log(‘B error ‘ + err); }, complete: function () { console.log(‘B done‘); },};var subB;setTimeout(function () { subB = connectableObservable.subscribe(observerB);}, 2000);setTimeout(function () { sub.unsubscribe(); // stop console.log(‘unsubscribed shared execution‘);}, 5000);
Just remember that with connect
we are manually controlling the start of the shared execution, and then we keep a subscription in order to manually control the stop of the shared execution. All of this is in order to avoid leaks.
[RxJS] Stopping a shared observable execution
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。