javascript - Rx.js,使用Subject从Observable进行多播

标签 javascript rxjs

是否有 Rx.js 专家?我正在尝试按照任意数量的网站(包括 rx.js 文档)上的说明,通过使用主题来多播可观察量。

var mainDataSource = Rx.Observable.from(summaries[0].added)
  //add a timestamp to each live entry as it passes through
  .timestamp()
  .map(function(scriptElement) { 
     var array = [scriptElement, scriptElement.timestamp]; return array; 
  })
  //check contents of the array
  .do(function(array) { console.log(array); });

var multicaster = new Rx.Subject();
var subSource = mainDataSource.subscribe(multicaster);
//attach the inline observer to the multicaster subject
multicaster.subscribe(
    function (x) { console.log('Value published to inlineScriptObserver: ' + x); },
    function (e) { console.log('onError: ' + e.message); },
    function () { console.log('inlineScriptObserver onCompleted'); }
);
//attach the external observer to the multicaster subject
multicaster.subscribe(
    function (x) { console.log('Value published to externalScriptObserver: ' + x); },
    function (e) { console.log('onError: ' + e.message); },
    function () { console.log('externalScriptObserver onCompleted'); }
);

我得到的输出如下:

[Object, 1493425651491]
inlineScriptObserver onCompleted
externalScriptObserver onCompleted

因此,当 onCompleted 事件从一个对象传输到另一个对象时,Subject 和 Observable 之间就清楚地相连了。但是我没有收到任何数据。正确格式的数据位于 Observable 的末尾,但它没有从主题的观察者的控制台中打印。

我错过了什么?它让我难以捉摸。

最佳答案

好吧,回答你自己的问题可能不太好,但万一其他人也遇到同样的问题......

我读到的文档一定已经过时了,与 rx.js 4 而不是 5 或其他相关。截至今天,根据此页面,

https://github.com/ReactiveX/rxjs/blob/master/doc/subject.md

上述示例的正确语法如下:

var multicaster = new Rx.Subject();
var mainDataSource = Rx.Observable.from(summaries[0].added)

//add a timestamp to each live entry as it passes through
.timestamp()
//log the timestamp for testing purposes
.do(function(scriptElement) { console.log("mainDataSource Timestamp: " + scriptElement.timestamp); })
//include the timestamp in array and deliver that array to subscribers
.map(function(scriptElement) { var array = [scriptElement, scriptElement.timestamp]; return array; })
//check contents of the array
do(function(array) { console.log(array); });

var multicastedDataSource = mainDataSource.multicast(multicaster);
//attach the inline observer to the multicaster subject
multicastedDataSource.subscribe(val => console.log(val), null, () => console.log('inlineScriptObserver complete'));
//attach the external observer to the multicaster subject
multicastedDataSource.subscribe(val => console.log(val), null, () => console.log('externalScriptObserver complete'));
multicastedDataSource.connect();

主要区别是使用 multicast() 而不是订阅可观察量,然后需要 connect() 到主题管道多播可观察量就像让观察者订阅一样。

难怪我的旧 rx.js 书在亚马逊上这么便宜......

关于javascript - Rx.js,使用Subject从Observable进行多播,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43690688/

相关文章:

angular - 获取对象后更新 Angular http post 方法的对象

flutter - 是否有一个订阅流,不会引发异常。相反,它会自动取消旧的订阅吗?

javascript - 如何使用 Fabric.js 在 Canvas 上拉伸(stretch)文本?

javascript - 如何获取刚刚创建的用户的uId

javascript - 为什么我的 jquery 固定 div 不起作用?

javascript - 如何制作一个可追加的可观察队列?

angular - 在 rxjs 中条件化平面图的执行 - Angular 5

javascript - 模型对象构造函数中的 node-mysql 查询

javascript - Backbone 重新排序了一个集合,以便家始终是第一位的?

Angular2 可观察和 promise