博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[RxJS] Subject: an Observable and Observer hybrid
阅读量:4606 次
发布时间:2019-06-09

本文共 3335 字,大约阅读时间需要 11 分钟。

This lesson teaches you how a Subject is simply a hybrid of Observable and Observer which can act as a bridge between the source Observable and multiple observers, effectively making it possible for multiple observers to share the same Observable execution.

 

var observable = Rx.Observable.interval(1000).take(5);var observerA = {  next: function (x) { console.log('A next ' + x); },  error: function (err) { console.log('A error ' + err); },  complete: function () { console.log('A done'); },};var observerB = {  next: function (x) { console.log('B next ' + x); },  error: function (err) { console.log('B error ' + err); },  complete: function () { console.log('B done'); },};observable.subscribe(observerA);setTimeout(  () => {    observable.subscribe(observerB);  },2000)

In the code above, we have two 'observers', because we call subscribe twice:

observable.scbscribe(ObserverA);observable.scbscribe(ObserverB);

 

If we want to have one observer, so we need to call subscribe only once.

For that we can build a bridgeObservers, which will loop though the observers:

const observable = Rx.Observable.interval(1000).take(5);const ObserverA = {  next: function(x){    console.log("A next " + x)  },  error: function(x){    console.error("A error " + x)  },  complete: function(){    console.log("A Done")  },};const ObserverB = {  next: function(x){    console.log("B next " + x)  },  error: function(x){    console.error("B error " + x)  },  complete: function(){    console.log("B Done")  },};const BridgeObservers = {  next: function(x){    this.observers.forEach(      o => o.next(x)    )  },  error: function(x){    this.observers.forEach(      o => o.error(x)    )  },  complete: function(){    this.observers.forEach(      o => o.complete()    )  },  observers: [],  addObserver: function(observer){    this.observers.push(observer)  }};observable.subscribe(BridgeObservers);BridgeObservers.addObserver(ObserverA);setTimeout(function(){  BridgeObservers.addObserver(ObserverB);}, 2000)

 

And this partten:

observable.subscribe(BridgeObservers);BridgeObservers.addObserver(ObserverA); // BirdegeObservers.subscribe(ObserverA)

is actually 'subject' partten, works both as Observer and Observable.

 

Subject:

const observable = Rx.Observable.interval(1000).take(5);const ObserverA = {  next: function(x){    console.log("A next " + x)  },  error: function(x){    console.error("A error " + x)  },  complete: function(){    console.log("A Done")  },};const ObserverB = {  next: function(x){    console.log("B next " + x)  },  error: function(x){    console.error("B error " + x)  },  complete: function(){    console.log("B Done")  },};const subject = new Rx.Subject();/*const BridgeObservers = {  next: function(x){    this.observers.forEach(      o => o.next(x)    )  },  error: function(x){    this.observers.forEach(      o => o.error(x)    )  },  complete: function(){    this.observers.forEach(      o => o.complete()    )  },  observers: [],  subscribe: function(observer){    this.observers.push(observer)  }};*/observable.subscribe(subject);subject.subscribe(ObserverA);//BridgeObservers.subscribe(ObserverA);setTimeout(function(){  subject.subscribe(ObserverB); // BridgeObservers.subscribe(ObserverB);}, 2000)

 In the end, ObserverA and ObserverB share one single observer. 

转载于:https://www.cnblogs.com/Answer1215/p/5932638.html

你可能感兴趣的文章
461. Hamming Distance
查看>>
Python垃圾回收机制详解
查看>>
jquery 编程的最佳实践
查看>>
MeetMe
查看>>
IP报文格式及各字段意义
查看>>
(转载)rabbitmq与springboot的安装与集成
查看>>
C2. Power Transmission (Hard Edition)(线段相交)
查看>>
STM32F0使用LL库实现SHT70通讯
查看>>
Atitit. Xss 漏洞的原理and应用xss木马
查看>>
MySQL源码 数据结构array
查看>>
(文件过多时)删除目录下全部文件
查看>>
T-SQL函数总结
查看>>
python 序列:列表
查看>>
web移动端
查看>>
pythonchallenge闯关 第13题
查看>>
linux上很方便的上传下载文件工具rz和sz使用介绍
查看>>
React之特点及常见用法
查看>>
【WEB前端经验之谈】时间一年半,或沉淀、或从零开始。
查看>>
优云软件助阵GOPS·2017全球运维大会北京站
查看>>
linux 装mysql的方法和步骤
查看>>