[RxJS] Subject basic
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的更多相关文章
- RxJS - Subject(转)
Observer Pattern 观察者模式定义 观察者模式又叫发布订阅模式(Publish/Subscribe),它定义了一种一对多的关系,让多个观察者对象同时监听某一个主题对象,这个主题对象的状态 ...
- import { Subject } from 'rxjs/Subject';
shared-service.ts import { Observable } from 'rxjs/Observable'; import { Injectable } from '@angular ...
- [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 ...
- [RxJS] Subject asObservable() method
You can create your own state store, not using any state management libraray. You might have seen th ...
- rxjs——subject和Observable的区别
原创文章,转载请注明出处 理解 observable的每个订阅者之间,是独立的,完整的享受observable流动下来的数据的. subject的订阅者之间,是共享一个留下来的数据的 举例 这里的cl ...
- RxJS之Subject主题 ( Angular环境 )
一 Subject主题 Subject是Observable的子类.- Subject是多播的,允许将值多播给多个观察者.普通的 Observable 是单播的. 在 Subject 的内部,subs ...
- [RxJS] Reusable multicasting with Subject factories
The way we use publish() (or multicast with an RxJS Subject) makes the shared Observable not reusabl ...
- 【Rxjs】 - 解析四种主题Subject
原文地址: https://segmentfault.com/a/1190000012669794 引言 开发ngx(angular 2+)应用时,基本上到处都会用到rxjs来处理异步请求,事件调用等 ...
- angular2 学习笔记 ( rxjs 流 )
RxJS 博大精深,看了好几篇文章都没有明白. 范围牵扯到了函数响应式开发去了... 我对函数式一知半解, 响应式更是第一次听到... 唉...不过日子还是得过...混着过先呗 我目前所理解的很浅, ...
随机推荐
- 大四实习准备2_java异常处理_android控件练习
2015-4-24 Java 异常处理 可以有多个catch;ArrayIndexOutOfBoundsException类是Exception类的子类RuntimeException类的一个间接子类 ...
- 各种html5 的 polyfill
https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-browser-Polyfills 配合 Modernizr
- java中线程队列BlockingQueue的用法
在新增的Concurrent包中,BlockingQueue很好的解决了多线程中,如何高效安全“传输”数据的问题.通过这些高效并且线程安全的队列类,为我们快速搭建高质量的多线程程序带来极大的便利.本文 ...
- 学习面试题Day03
1.Java中的注释有哪些? 如果不算Annotation,Java的注释有3种,即行注释.块注释和文档注释.它们往往适合于不同地方的注释,其中文档注释比较特殊,它的注释信息可以进入到javadoc文 ...
- ExecuteStoreQuery
using (var webdb = new kyj_NewHouseDBEntities()) { string sql = "select * from developer where ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.2.6
If $\sen{A}<1$, then $I-A$ is invertible, and $$\bex (I-A)^{-1}=I+A+A^2+\cdots, \eex$$ aa converg ...
- 线性表-串:KMP模式匹配算法
一.简单模式匹配算法(略,逐字符比较即可) 二.KMP模式匹配算法 next数组:j为字符序号,从1开始. (1)当j=1时,next=0: (2)当存在前缀=后缀情况,next=相同字符数+1: ( ...
- 大数据架构师基础:hadoop家族,Cloudera产品系列等各种技术
大数据我们都知道hadoop,可是还会各种各样的技术进入我们的视野:Spark,Storm,impala,让我们都反映不过来.为了能够更好的架构大数据项目,这里整理一下,供技术人员,项目经理,架构师选 ...
- HDU-4662 MU Puzzle 水题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4662 倒推考虑长度就可以了. //STATUS:C++_AC_31MS_240KB #include ...
- Mongodb 安装 以及 问题解决-摘自网络
一,下载 1.官网为:http://www.mongodb.org/ :下载安装程序的地址为:http://www.mongodb.org/downloads ,选择选择的是Windows 32-bi ...