A Subject is a type that implements both Observer and Observable types. As an Observer, it can subscribe to Observables, and as an Observable it can produce values and have Observersw subscribe it.

First time I read

implements both Observer and Observable types

I was quite confused.

As a Observable:

var subject = new Rx.Subject();
var subscription = subject.subscribe(
function onNext(x) { console.log('onNext: ' + x); },
function onError(e) { console.log('onError: ' + e.message); },
function onCompleted() { console.log('onCompleted'); }
);
subject.onNext('Our message #1');
subject.onNext('Our message #2'); /*
"onNext: Our message #1"
"onNext: Our message #2"
*/

Every time we call onNext() message to yield the value.

As a Observer, so we use 'source' to sebscribe 'subject', then subscribe 'subject' again to get the side effect

var subject = new Rx.Subject();
var source = Rx.Observable.interval()
.map(function(v) { return 'Interval message #' + v; })
.take();
source.subscribe(subject);
var subscription = subject.subscribe(
function onNext(x) { console.log('onNext: ' + x); },
function onError(e) { console.log('onError: ' + e.message); },
function onCompleted() { console.log('onCompleted'); }
);
setTimeout(function() {
subject.onCompleted();
}, ); /*
"onNext: Interval message #0"
"onNext: Interval message #1"
"onNext: Interval message #2"
"onCompleted"
*/

[RxJS] Subject basic的更多相关文章

  1. RxJS - Subject(转)

    Observer Pattern 观察者模式定义 观察者模式又叫发布订阅模式(Publish/Subscribe),它定义了一种一对多的关系,让多个观察者对象同时监听某一个主题对象,这个主题对象的状态 ...

  2. import { Subject } from 'rxjs/Subject';

    shared-service.ts import { Observable } from 'rxjs/Observable'; import { Injectable } from '@angular ...

  3. [RxJS] Subject: an Observable and Observer hybrid

    This lesson teaches you how a Subject is simply a hybrid of Observable and Observer which can act as ...

  4. [RxJS] Subject asObservable() method

    You can create your own state store, not using any state management libraray. You might have seen th ...

  5. rxjs——subject和Observable的区别

    原创文章,转载请注明出处 理解 observable的每个订阅者之间,是独立的,完整的享受observable流动下来的数据的. subject的订阅者之间,是共享一个留下来的数据的 举例 这里的cl ...

  6. RxJS之Subject主题 ( Angular环境 )

    一 Subject主题 Subject是Observable的子类.- Subject是多播的,允许将值多播给多个观察者.普通的 Observable 是单播的. 在 Subject 的内部,subs ...

  7. [RxJS] Reusable multicasting with Subject factories

    The way we use publish() (or multicast with an RxJS Subject) makes the shared Observable not reusabl ...

  8. 【Rxjs】 - 解析四种主题Subject

    原文地址: https://segmentfault.com/a/1190000012669794 引言 开发ngx(angular 2+)应用时,基本上到处都会用到rxjs来处理异步请求,事件调用等 ...

  9. angular2 学习笔记 ( rxjs 流 )

    RxJS 博大精深,看了好几篇文章都没有明白. 范围牵扯到了函数响应式开发去了... 我对函数式一知半解, 响应式更是第一次听到... 唉...不过日子还是得过...混着过先呗 我目前所理解的很浅,  ...

随机推荐

  1. Sencha 基础Demo测试,三种showView的方法

    直接贴代码吧 Ext.define("build.controller.MainController",{ extend:"Ext.app.Controller" ...

  2. [POJ 2588] Snakes

    同swustoj 8 Snakes Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1015   Accepted: 341 ...

  3. [.NET WebAPI系列01] WebAPI 简单例子

    [源] 来自微软WebAPI官方视频,Introduction to the ASP.NET Web API --Uniform Interface -- Demo-Using convention ...

  4. Ejabberd源码解析前奏--集群

    一.如何工作 一个XMPP域是由一个或多个ejabberd节点伺服的. 这些节点可能运行在通过网络连接的不同机器上. 它们都必须有能力连接到所有其它节点的4369端口, 并且必须有相同的 magic ...

  5. 淘宝JAVA中间件Diamond详解(一)---简介&快速使用

    大家好,今天开始为大家带来我们通用产品团队的产品 —— diamond的专题,本次为大家介绍diamond的概况和快速使用. 一.概况 diamond是淘宝内部使用的一个管理持久配置的系统,它的特点是 ...

  6. Linux(ubuntu)下安装JDK、Tomcat

    一.安装jdk 1)首先以root用户登录进去,在根目录下建立opt的目录,我们将下载的东西都放到该目录下去. 2)下载j2sdk ,如jdk-6u31-linux-i586.bin 下载地址如下ht ...

  7. Integrating JavaScript into Native Applications

    JavaScriptCore 简介 iOS7 中新加入的 JavaScriptCore.framework 可能被大多数开发人员所忽略,但是如果你之前就在项目中用过自己编译JavaScriptCore ...

  8. python 网络编程 (二)---异常

    异常 python的socket模块实际上定义了4种可能出现的异常: 1)与一般I/O 和通信问题有关的socket.error; 2)与查询地址信息有关的socket.gaierror; 3)与其他 ...

  9. Eclipse关联JavaDoc和源代码

    1.Eclipse 关联 JavaDoc 1.在 Eclipse 菜单中点击 Window -> Preferences,在弹出框中左侧选择展开 Java 节点,点击 Installed JRE ...

  10. android 链接蓝牙不稳定的解决建议

    My workaround I scan BLE for a short period of time 3-4 seconds then I turn scan OFF for 3-4 seconds ...