[RxJS] Subscription add()
add Rxjs
2023-09-14 08:59:12 时间
You can use subsciprtion.add(anotherSubscription)
to combine subscriptions and cancel them at the same time.
import { Observable } from 'rxjs';
const observer = {
next: value => console.log('next', value),
error: error => console.log('error', error),
complete: () => console.log('complete!')
};
const observable = new Observable(subscriber => {
let count = 0;
const id = setInterval(() => {
subscriber.next(count);
count += 1;
}, 1000);
return () => {
console.log('called');
clearInterval(id);
};
});
const subscription = observable.subscribe(observer);
const subscriptionTwo = observable.subscribe(observer);
/*
* Subscriptions can be added together using the add method,
* you can then unsubscribe to multiple at the same time.
* This is simply personal preference, unsubscribing individually
* will produce the same result. Also, in future lessons, we will see how
* to automate this unsubscribe process with operators.
*/
subscription.add(subscriptionTwo);
setTimeout(() => {
/*
* Note: Calling unsubscribe will not fire your complete callback,
* but the returned function will be invoked cleaning up any
* resources that were created by the subscription - in this
* case the interval.
*/
subscription.unsubscribe();
}, 3500);
Notice that, when you call subscription.unsubscribe()
, the complete => ()
function won't be triggered.
complete: () => {}
only get triggered, when we call subscriber.complete()
相关文章
- phpstrom 取消创建一个文件自动就add提交到git
- [AST Babel] Add function name into the console log 'path.findParent(t.isFunctionDeclaration)'
- [ARIA] Add aria-expanded to add semantic value and styling
- [RxJS 6] The Retry RxJs Error Handling Strategy
- [MST] Create an Entry Form to Add Models to the State Tree
- [rxjs] Creating An Observable with RxJS
- [ARIA] Add aria-expanded to add semantic value and styling
- [RxJS] Add debug method to Observable in TypeScript
- [rxjs] Throttled Buffering in RxJS (debounce)
- [rxjs] Creating An Observable with RxJS
- [GIT]1. init, add, commit, log, status
- linux deepin sudo: add-apt-repository:找不到命令 解决办法
- SAP Fiori Elements - Smart table add button debug 2 setDirty
- 成功解决Value Error: Unable to add relationship because child variable ‘name‘ in ‘cats_df‘ is also its i
- 【异常】ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the
- mac关于 E45: ‘readonly‘ option is set (add ! to override)
- 从源码分析DEARGUI之add_menu
- git 如何删除已经 add 的文件 (如何撤销已放入缓存区文件的修改)
- 【Linux 内核 内存管理】memblock 分配器编程接口 ② ( memblock_add_range 函数分析 | memblock_insert_region 函数分析 )
- 如何在 Ubuntu 和 Debian 上修复“找不到 add-apt-repository 命令”
- sql date_sub 和 date_add
- 241. Different Ways to Add Parentheses——本质:DFS
- OpenCV-Python学习(6)—— OpenCV 图像算术操作(cv.add、cv.subtract、cv.multiply、cv.divide)
- entity framework add remove range