[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 ...
随机推荐
- Lightoj 1127 - Funny Knapsack 【二分】
题目链接:problem=1127">http://www.lightoj.com/volume_showproblem.php?problem=1127 题意:有n个物体(n< ...
- 辛星跟您玩转vim第三节之程序猿特须要的移动方式
前面第二节我首先值得一提的是,我的vim教程pdf版本号已经写完了.大家能够去下载,这里是csdn的下载地址:csdn下载.假设左边的下载地址挂掉了.也能够自行在浏览器以下输入例如以下地址进行下载:h ...
- android图像处理(3)浮雕效果
这篇将讲到图片特效处理的浮雕效果.跟前面一样是对像素点进行处理,算法是通用的. 算法原理:用前一个像素点的RGB值分别减去当前像素点的RGB值并加上127作为当前像素点的RGB值. 例: ABC 求B ...
- $routeParams 实现路由指定参数
[摘要]后台管理系统权限控制到按钮级别,将每一个资源的key绑定在url中,渲染页面的时候去根据key来获取当前页面的按钮列表. router.js angular.module("app. ...
- 词向量 word2vec
看的这一篇的笔记 http://licstar.net/archives/328 看不太懂. 要学的话,看这里吧,这里把一些资料做了整合: http://www.cnblogs.com/wuzhitj ...
- VC 常见问题百问
http://www.cnblogs.com/cy163/archive/2006/06/19/429796.html 经典Vc书 Charles Petzold 的<Programming W ...
- 关于C++中用两个迭代器方式初始化string的知识
string(iter1, iter2); 第一点:两个迭代器必须指向同一个容器. 第二点:iter2必须>=iter1. 第三点:假设iter1等于iter2,那么结果为空[] 另外一个比較特 ...
- 使用H5 formData对象上传图片和视频的文件时,必填的属性
async : false,cache : false,contentType : false,// 告诉jQuery不要去设置Content-Type请求头processData : false,/ ...
- javascript的组成
ECMAScript:(3/5/6/7) 它是JS语言的标准,规定了JS的编程语法和基础核心知识. DOM:document object model 文档对象模型,提供给JS很多的操作页面中元素的属 ...
- 关于DOM的有关总结
1.获取DOM元素 document.getElementById() 通过id获取DOM元素 document.getElementsByClassName() 通过类名获取DOM元素 docum ...