【问题标题】:RxJS groupBy and combineAll operators seem to omit outputRxJS groupBy 和 combineAll 运算符似乎省略了输出
【发布时间】:2016-06-26 22:18:15
【问题描述】:

使用 .groupBy 和 .concatAll 的组合对输出进行分组时,不会生成一些预期的输出。

示例代码:

var Rx = require('rx');

var source = Rx.Observable.from(['a1', 'a2', 'b1', 'b2', 'a3', 'a4', 'b3', 'b4'])
  .groupBy(function (item) { return item.substr(0, 1); })
  .concatAll();

var subscription = source.subscribe(
  function (x) {
    console.log('Next: %s', x);
  },
  function (err) {
    console.log('Error: %s', err);
  },
  function () {
    console.log('Completed');
  });

实际输出:

$ node index.js
Next: a1
Next: a2
Next: a3
Next: a4
Completed

预期输出:

$ node index.js
Next: a1
Next: a2
Next: a3
Next: a4
Next: b1
Next: b2
Next: b3
Next: b4
Completed

我是否误解了这些运算符的工作原理?或者这是一个 RxJS 错误? (已在https://github.com/Reactive-Extensions/RxJS/issues/1264 暂时提交为错误。)

【问题讨论】:

    标签: javascript rxjs frp reactivex


    【解决方案1】:

    想通了。这是hot vs cold observables 的问题。如下更改代码使其按预期工作:

    var source = Rx.Observable.from(['a1', 'a2', 'b1', 'b2', 'a3', 'a4', 'b3', 'b4'])
      .groupBy(function (item) { return item.substr(0, 1); })
      .map(function (obs) {  // <<<<< ADD THIS .map clause to fix
        var result = obs.replay();
        result.connect();
        return result;
      })
      .concatAll();
    

    【讨论】:

      猜你喜欢
      • 2017-12-24
      • 2020-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-25
      相关资源
      最近更新 更多