[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 ...
随机推荐
- 洛谷——P1209 [USACO1.3]修理牛棚 Barn Repair
https://www.luogu.org/problem/show?pid=1209 题目描述 在一个夜黑风高,下着暴风雨的夜晚,farmer John的牛棚的屋顶.门被吹飞了. 好在许多牛正在度假 ...
- UVA 10306 e-Coins(全然背包: 二维限制条件)
UVA 10306 e-Coins(全然背包: 二维限制条件) option=com_onlinejudge&Itemid=8&page=show_problem&proble ...
- ArcGIS中数据之间的转换接口IFeatureDataConverter2
之前我写过一篇文章关于ArcGIS各种空间数据格式之间转换的通用方法:ArcGIS中sde,mdb,shp数据之间的转换.这里使用的主要接口方法就是用到了IFeatureDataConverter接口 ...
- 让JavaScript在Visual Studio 2015中编辑得更easy
微软公布的Visual Studio 2015展示了该公司对于让该开发工具更好的支持主流的开发语言的工作.微软项目经理Jordan Matthiesen已经具体列出了一些具体处理JavaScript开 ...
- 7lession-基础数据使用介绍
1.数值 这个使用比较简单 a = 1 b = 3.2 c = 12.5+4j d = 20L 2.字符串 代码 s = "hello world,i am comming" pr ...
- IIS6下AD域设置
简介:IIS6下AD域设置 IIS6下AD域设置 http://files.cnblogs.com/files/KingUp/AD%E5%9F%9F%E8%AE%BE%E7%BD%AE.rar
- Zabbix主动代理模式 + 主动模式agent客户端
2.1.1 安装软件 ]# rpm -qa zabbix* zabbix-proxy-sqlite3-3.4.15-1.el7.x86_64 zabbix-proxy-mysql-3.4.15-1.e ...
- 初学WCF需要注意的地方
1.WCF的元数据发布有两种方式: a.HTTP-GET方式发布数据:让客户端使用HTTP-GET方式来获取数据是比较常见的方式.所谓HTTP—GET方式,是指当客户端发送一个HTTP-GET请求时, ...
- 淘宝分类导航条;纯css实现固定导航栏
效果例如以下: 页面例如以下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "ht ...
- elasticsearch cluster 概述
在源码概述中我们分析过,elasticsearch源码从功能上可以分为分布式功能和数据功能,接下来这几篇会就分布式功能展开.这里首先会对cluster作简单概述,然后对cluster所涉及的主要功能详 ...