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. 13.AxisUtil

    1. package com.glodon.gspm.adapter.plugin.common; import lombok.SneakyThrows; import org.apache.axis ...

  2. 1.24 Python知识进阶 - 类与对象

    类 语法格式: class Dog(object): print("the dog is barking ...") Dog为类名,object为要继承的基类,Dog类会从基类ob ...

  3. 轻松学习Linux之详解系统引导过程

    轻松学习Linux之详解系统引导过程-1 轻松学习Linux之详解系统引导过程-2 本文出自 "李晨光原创技术博客" 博客,谢绝转载!

  4. codeforces 666E. Forensic Examination(广义后缀自动机,Parent树,线段树合并)

    传送门: 解题思路: 很坑的一道题,需要离线处理,假如只有一组询问,那么就可以直接将endpos集合直接累加输出就好了. 这里就要将询问挂在树节点上,在进行线段树合并时查询就好了. 代码超级容易写挂的 ...

  5. NOIP2016 天天爱跑步(线段树/桶)

    题目描述 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.天天爱跑步是一个养成类游戏,需要 玩家每天按时上线,完成打卡任务. 这个游戏的地图可以看作一一棵包含 N个结点 ...

  6. Struts1 的html标签的具体解说与使用

    <html:form> 标签 <html:form>用来创建表单.<html:form>必须包括一个action属性,否则JSP会抛出一个异常. 经常使用的属性有下 ...

  7. Apple Watch 集成环信SDK

    本文简单的讲述下怎样用Apple Watch Kit集成环信SDK. 升级xcode到version 6.2,和 IOS SDK8.2 下载环信SDK从官网 打开XCode->new proje ...

  8. CentOS搭建xfce桌面+VNC教程

    CentOS搭建xfce桌面+VNC教程 Linux的安全与性能向来为开发者所称道,你可以轻松地在搜索引擎中找到各种Linux优越性的说辞,其中不乏Linux的激进者.特别是当你步入VPS领域,更多地 ...

  9. 89.hash算法实现CSDN密码处理

    初始化,数据的行数,hash链表结构体,存储头结点 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdl ...

  10. Flume Channel Selectors官网剖析(博主推荐)

    不多说,直接上干货! Flume Sources官网剖析(博主推荐) Flume Channels官网剖析(博主推荐) 一切来源于flume官网 http://flume.apache.org/Flu ...