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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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( ...

  4. [rxjs] Demystifying Cold and Hot Observables in RxJS

    Cold: console.clear(); var Observable = Rx.Observable; var clock = Observable.interval(1000).take(10 ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. [RxJS] What RxJS operators are

    We have covered the basics of what is Observable.create, and other creation functions. Now lets fina ...

  9. [RxJS] Implement RxJS `concatMap` by Waiting for Inner Subscriptions to Complete

    Unlike mergeMap and switchMap, concatMap focuses on when "inner" subscriptions "compl ...

随机推荐

  1. 47.Android 自己定义PopupWindow技巧

    47.Android 自己定义PopupWindow技巧 Android 自己定义PopupWindow技巧 前言 PopupWindow的宽高 PopupWindow定位在下左位置 PopupWin ...

  2. 码农的救赎:使用Github Pages搭建博客

    人生若仅仅如初见,都恨太晚 据说有博客的人比没博客人的薪水要高非常多,相传写博客也是一个高手的标配,尽管之前一直有在写(在这里).可是孤既不是高手.薪水还比别人少.之前一直在CSDN上面写博客,那是一 ...

  3. 华为畅玩5 (CUN-AL00) 刷入第三方twrp Recovery 及 root

    华为畅玩5 (CUN-AL00) 刷入第三方twrp Recovery  及 root 下载地址    http://pan.baidu.com/s/1hsn6VzA 1. 在官网申请解锁码    申 ...

  4. php函数按地址传递参数(php引用)

    php函数按地址传递参数(php引用) 一.总结 1.php引用:php引用和c++一样,都是在变量前加&(取地址符号) 2.php函数按地址传递参数:php函数按地址传递参数(php引用)也 ...

  5. http 协议上传文件multipart form-data boundary 说明--转载

    原文地址:http://xixinfei.iteye.com/blog/2002017 含义 ENCTYPE="multipart/form-data" 说明: 通过 http 协 ...

  6. 【2017"百度之星"程序设计大赛 - 初赛(B)】Chess

    [链接]http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=776&pid=1001 [题意] 在这里写题意 [题 ...

  7. hiho 1182 : 欧拉路·三

    1182 : 欧拉路·三 这时题目中给的提示: 小Ho:是这种.每次转动一个区域不是相当于原来数字去掉最左边一位,并在最后加上1或者0么. 于是我考虑对于"XYYY",它转动之后能 ...

  8. Android基于xmpp的即时通讯应用

    xmpp是一个通信协议.因为这是个开放的协议,为了节俭开发成本,很多即时应用都采用了这个协议.Android上最常用的组合asmack +openfire.Asmack是smack的android版, ...

  9. vue 星星评分组件

    显示评分和打分组件,可现实半颗星星效果 效果图: 参数名 类型 说明 score Number 分数 ,默认0,保留一位小数 disabled Boolean 是否只读,默认false,鼠标点击可以打 ...

  10. 原生js大总结六

    051.如何打印当前浏览器的版本等信息   navigator.userAgent   返回包含浏览器版本等信息的字符串 ,常用于判断浏览器版本及使用设备(PC或者移动端   052 .在浏览器地址栏 ...