[RxJS] RefCount: automatically starting and stopping an execution
2016-10-23 02:05
330 查看
With the connect() method on a ConnectableObservable, the programmer is responsible for avoiding leaked executions of shared RxJS Observables. This lesson will teach you about 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 will help us to manage the connections, so we don't need to worry about the memory leak.
After multicast(new Rx.Subject()), we call refCount(), so it will help us to manage the connections, so we don't need to worry about the memory leak.
var shared = Rx.Observable.interval(1000) .do(x => console.log('source ' + x)) .multicast(new Rx.Subject()) .refCount(); 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 subA = shared.subscribe(observerA); // start 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 = shared.subscribe(observerB); // 1 => 2 }, 2000); setTimeout(function () { subA.unsubscribe(); // 2 => 1 console.log('unsubscribed A'); }, 5000); 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] Subject: an Observable and Observer hybrid
- [RxJS] Stopping a shared observable execution
- execution time between count(id) and * under condition - not in (null)
- Ext JS 4的倒数:动态加载和新的类机制 Count down to Ext JS 4: Dynamic Loading and New Class System (下)
- R12.2——Starting & Stopping Admin and Managed server in FMW
- [rxjs] Creating An Observable with RxJS
- [Javascript + rxjs] Simple drag and drop with Observables
- PIVOT and UNPIVOT: Optimization and Execution Strategies in an RDBMS
- [RxJS + AngularJS] Sync Requests with RxJS and Angular
- pointer to ref of an array, and to the func that return a ref of an array
- Ruby: Count unique elements and their occurences in an array
- [rxjs] Demystifying Cold and Hot Observables in RxJS
- Ext JS 4的倒数:动态加载和新的类机制 Count down to Ext JS 4: Dynamic Loading and New Class System (下)
- GSR :the meaning of <load sharing elements> <refernce><refcount> in CEF and how to calculate them
- An innovative way to replace AJAX and JSONP using node.js and socket.io
- UI Design Basic :0.2 starting and stopping
- Ext JS 4的倒数:动态加载和新的类机制 Count down to Ext JS 4: Dynamic Loading and New Class System (上)
- [Yii Framework] How to develop an extension with image, css and js
- [Android Training视频系列]2.3 Stopping and Restarting an Activity
- Starting and Stopping Oracle Fusion Middleware