baconjs / bacon.js

Functional reactive programming library for TypeScript and JavaScript
https://baconjs.github.io
MIT License
6.47k stars 330 forks source link

unexpected behavior of bufferWithTimeOrCount(delay, count) #544

Closed simshi closed 9 years ago

simshi commented 9 years ago

"version": "0.7.43"

var Bacon = require('baconjs').Bacon
var s = Bacon.repeatedly(200,[1,2,3,4,5])
s.bufferWithTimeOrCount(1000, 5).onValue(function(v) {
    console.log(+Date.now() + ':' + v);
});

output:

1423644641372:1,2,3,4,5
1423644642386:1,2,3,4
1423644642593:5
1423644643391:1,2,3,4
1423644643598:5
1423644644395:1,2,3,4
1423644644611:5

expected:

1423647319092:1,2,3,4,5
1423647320117:1,2,3,4,5
1423647321148:1,2,3,4,5
1423647322174:1,2,3,4,5
1423647323203:1,2,3,4,5
1423647324232:1,2,3,4,5
1423647325262:1,2,3,4,5

It works as expected if make schedule do nothing while it's no longer scheduled (i.e. flushed already):

      schedule: function() {
        if (!this.scheduled) {
          this.scheduled = true;
          return delay((function(_this) {
            return function() {
              if (!this.scheduled) return; // <== hot fix, not a solution!
              return _this.flush();
            };
          })(this));
        }
      }

One more question, the timestamp jumps about 30ms between two flush actions, would it be a problem?

raimohanska commented 9 years ago

Good find! The method hasn't been used much I guess, having a bug this obivious. I made a fix that unschedules on flush. Would you care to have a look?

raimohanska commented 9 years ago

Released 0.7.44 with fix.

simshi commented 9 years ago

Thank you for quick response, looks fine.