[RxJS] Convert RxJS Subjects to Observables
The use of RxJS Subjects is common, but not without problems. In this lesson we will see how they can be usually safely replaced with plain Observables.
Check the follow code:
const click$ = new Rx.Subject(); document.addEventListener('click', function(e) {
click$.next(e)
}); click$.subscribe(function(v) {
console.log(v)
});
One problem for this is that 'click$' become a global variable, not easy for maintance.
Not so sure how it will impact Angular, because Angular use component anyway, the subject only available to the component, maybe have low impact.
But let's see if you are not using Angular, how to conver a Subject to Observable.
const click$ = Rx.Observable.create(function(observer) {
const handler = (e) => {
observer.next(e)
}; document.addEventListener('click', handler); return function unsubscribe(){
document.removeEventListener('click', handler)
} }); const subscription = click$.subscribe(function (ev) {
console.log(ev.clientX);
}); setTimeout(function () {
subscription.unsubscribe();
}, );
[RxJS] Convert RxJS Subjects to Observables的更多相关文章
- [RxJS] Implement RxJS `mergeMap` through inner Observables to Subscribe and Pass Values Through
Understanding sources and subscribers makes it much easier to understand what's going on with mergeM ...
- [RxJS] Use RxJS concatMap to map and concat high order observables
Like switchMap and mergeMap, concatMap is a shortcut for map() followed by a concatAll(). In this le ...
- [RxJS] Use RxJS mergeMap to map and merge high order observables
Like RxJS switchMap() is a shortcut for map() and switch(), we will see in this lesson how mergeMap( ...
- [rxjs] Demystifying Cold and Hot Observables in RxJS
Cold: console.clear(); var Observable = Rx.Observable; var clock = Observable.interval(1000).take(10 ...
- [RxJS] Convert a Node.js style callback to Observable: bindNodeCallback
It's just like bindCallback, but the callback is expected to be of type callback(error, result). imp ...
- [RxJS] Implement RxJS `switchMap` by Canceling Inner Subscriptions as Values are Passed Through
switchMap is mergeMap that checks for an "inner" subscription. If the "inner" su ...
- [RxJS] Chain RxJS Operators Together with a Custom `pipe` Function using Array.reduce
Instead of writing complex operators, it's usually best to write simple, single-purpose operators th ...
- [RxJS] What RxJS operators are
We have covered the basics of what is Observable.create, and other creation functions. Now lets fina ...
- [RxJS] Implement RxJS `concatMap` by Waiting for Inner Subscriptions to Complete
Unlike mergeMap and switchMap, concatMap focuses on when "inner" subscriptions "compl ...
随机推荐
- 终结者:借助pinyin4j相关jar包提取汉字的首字母
import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinCase ...
- php资源类型变量
php资源类型变量 一.总结 1. php资源类型变量:用来打开文件.数据库连接.图形画布区域等的一种特殊变量,比如FILE *fp; 二.PHP: Resource 资源类型 Resource 资 ...
- 错误 make: Nothing to be done for 'default'
Makefile书写格式非常严格,all:<TAB缩进>make -C $(KDIR) M=$(PWD) $(EXTRA_CFLAGS) modulesdefault:<TAB缩进& ...
- Direct2D开发:Direct2D 和 GDI 互操作性概述
本主题说明如何结合使用 Direct2D 和 GDI(可能为英文网页).有两种方法可以结合使用 Direct2D 和 GDI:您可以将 GDI 内容写入与 Direct2D GDI 兼容的呈现器目标, ...
- 【例题 8-3 UVA - 1152】4 Values whose Sum is 0
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 显然中间相遇. 自己写了个hash处理一下冲突就可以了. [代码] /* 1.Shoud it use long long ? 2. ...
- iOS关闭键盘简单实现(objc/swift)
Objective-C 代码实例方式一 [[[UIApplication sharedApplication] keyWindow] endEditing:YES]; 假设一个view上有很多Text ...
- diff命令具体解释
diff命令參数: diff - 找出两个文件的不同点 总览 diff [选项] 源文件 目标文件 以下是 GNU所接受的 diff 的全部选项的概要. 大多数的选项有两个同样的名字,一个是单个的跟在 ...
- 转linux文件的读写
转自 http://www.open-open.com/lib/view/open1474356438277.html 缓存 缓存是用来减少高速设备访问低速设备所需平均时间的组件,文件读写涉及到计算机 ...
- JS如何动态生成变量名[重点]
解决方案: function create_variable(num){ var name = "test_"+num; //生成函数名 ...
- 关于ES6(ES2015)的知识点详细总结
ECMAScript6 ECMAScript简称就是ES,你可以把它看成是一套标准,JavaScript就是实施了这套标准的一门语言,现在主流浏览器使用的是ECMAScript5. http://ba ...