escouten
escouten

Reputation: 2973

RxJS groupBy and combineAll operators seem to omit output

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

Upvotes: 2

Views: 635

Answers (1)

escouten
escouten

Reputation: 2973

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

Upvotes: 1

Related Questions