kefirjs / kefir

A Reactive Programming library for JavaScript
https://kefirjs.github.io/kefir/
MIT License
1.87k stars 97 forks source link

.bufferBy emitting empty arrays #113

Closed Macil closed 9 years ago

Macil commented 9 years ago

This code worked in 2.5.0 and stopped working in 2.6.0.

var assert = require('assert');
var Kefir = require('kefir');

var emitter = new Kefir.Bus();
var step = 0;
emitter.bufferBy(emitter.flatMap(function(x) {
  return Kefir.later(10, x);
})).onValue(function(x) {
  console.log('got value', x);
  switch(step) {
    case 1:
      assert.deepEqual(x, [5, 6]);
      break;
    case 2:
      assert.deepEqual(x, [7, 8]);
      console.log('done');
      break;
    default:
      throw new Error("Should not happen");
  }
});
console.log('about to emit 5, 6');
emitter.emit(5);
emitter.emit(6);
step = 1;
setTimeout(function() {
  console.log('about to emit 7, 8');
  emitter.emit(7);
  emitter.emit(8);
  step = 2;
}, 100);

(It's adapted from a unit test I had in a project. The original unit test called bufferBy on a function which took a value, and returned a stream which emitted the value after an asap delay. I tweaked the code to use Kefir.later in this example.)

It fails with this:

$ node test.js 
about to emit 5, 6
got value [ 5, 6 ]
got value []

assert.js:93
  throw new assert.AssertionError({
        ^
AssertionError: [] deepEqual [5,6]
    at step (/Users/chris/Coding/kefirtest/test.js:10:14)
    at callSubscriber (/Users/chris/Coding/kefirtest/node_modules/kefir/dist/kefir.js:841:8)
    at Dispatcher.dispatch (/Users/chris/Coding/kefirtest/node_modules/kefir/dist/kefir.js:868:8)
    at AnonymousObservable._emitValue (/Users/chris/Coding/kefirtest/node_modules/kefir/dist/kefir.js:644:25)
    at AnonymousObservable._flush (/Users/chris/Coding/kefirtest/node_modules/kefir/dist/kefir.js:4414:13)
    at AnonymousObservable._handleSecondaryValue (/Users/chris/Coding/kefirtest/node_modules/kefir/dist/kefir.js:4438:11)
    at AnonymousObservable._handleSecondaryAny (/Users/chris/Coding/kefirtest/node_modules/kefir/dist/kefir.js:4243:24)
    at AnonymousObservable._$handleSecondaryAny (/Users/chris/Coding/kefirtest/node_modules/kefir/dist/kefir.js:4198:21)
    at callSubscriber (/Users/chris/Coding/kefirtest/node_modules/kefir/dist/kefir.js:838:6)
    at Dispatcher.dispatch (/Users/chris/Coding/kefirtest/node_modules/kefir/dist/kefir.js:868:8)
rpominov commented 9 years ago

Yep, I introduced that change in 2.6.0 on purpose (see #108). Sorry for inconvenience. You need to add .filter(arr => arr.length > 0) after .bufferBy() in places where array size matters.