RxJS groupBy and combineAll operators seem to omit output

不打扰是莪最后的温柔 提交于 2019-12-11 13:56:11

问题


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

Sample code:

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');
  });

Actual output:

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

Expected output:

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

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


回答1:


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


来源:https://stackoverflow.com/questions/38043728/rxjs-groupby-and-combineall-operators-seem-to-omit-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!