RxJS groupBy 和 combineAll 运算符似乎省略了输出 [英] RxJS groupBy and combineAll operators seem to omit output

查看:43
本文介绍了RxJS groupBy 和 combineAll 运算符似乎省略了输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When grouping output with the combination of .groupBy and .concatAll, some expected output is not generated.

示例代码:

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.)

Am I misunderstanding how these operators work? Or is this an RxJS bug? (Tentatively filed as a bug already at https://github.com/Reactive-Extensions/RxJS/issues/1264.)

推荐答案

想通了.这是的问题热与冷 observables.如下更改代码使其按预期工作:

Figured it out. This is a problem of hot vs cold observables. Changing the code as follows makes it work as expected:

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();

这篇关于RxJS groupBy 和 combineAll 运算符似乎省略了输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆