[RxJS] RefCount: automatically starting and stopping an execution
and an Rxjs execution starting
2023-09-14 08:59:19 时间
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.
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" */
相关文章
- QT-找开工程后,最上方提示the code model could not parse an included file, which might lead to incorrect code completion and highlighting, for example.
- Android问题-selection contains a component,button7,introduced in an ancestor and cannot be deleted.
- Automatically generating nice graphs at end of your Load Test with Apache JMeter and JMeter-Plugins
- [Javascript] Create an Async Generator and Loop Through Generated Promises with "For Await Of" Loops
- [ES2019] Use JavaScript ES2019 flatMap to Map and Filter an Array
- [Django] Creating an app, models and database
- [React Native] Writing Platform-Specific Components for iOS and Android in React Native
- [Docker] Run, Stop and Remove Docker Containers
- [rxjs] Demystifying Cold and Hot Observables in RxJS
- [rxjs] Creating An Observable with RxJS
- 网络号 = IP和子网掩码的每位数AND 主机号 掩码取反与IP地址and运算
- [Transducer] Make an Into Helper to Remove Boilerplate and Simplify our Transduce API
- [RxJS] RefCount: automatically starting and stopping an execution
- [rxjs] Demystifying Cold and Hot Observables in RxJS
- An example of EXPORT / IMPORT usage - in office integration and odata metadata model cache
- 【Codeforces Round #421 (Div. 2) A】Mister B and Book Reading
- 成功解决Both binary classification-only and multiclassification-only loss function or metrics specified
- leetcode - Construct Binary Tree from Inorder and Postorder Traversal
- Android12安装报错:Targeting S+ (version 31 and above) requires that an explicit value for android:export
- 《论文阅读》EmotionIC: Emotional Inertia and Contagion-driven Dependency Modeling for Emotion Recognition
- 22.7 for Aspose.Words and Aspose.Cells.NET