What you're describing is bufferWithCount
. In reality, windowWithCount
yields an observable immediately, and that observable will begin yielding it's items immediately. Presumable, you are doing a toArray
(or something similar) somewhere down the line, which is forcing the window to complete before yielding it's items. This is most likely because you are trying to synchronize all of your points before attempting to draw the image. Instead, you should be using the events as they come. However, if you were to do this, you would end up with duplicate draws, as the windows definitely overlap.
In reality, what you probably want is something more like scan
...
var Rx = require('rx'),
Observable = Rx.Observable,
log = console.log.bind(console),
source = Observable.interval(25),
points = source.scan([], function (acc, point) {
if (acc.length === 50) {
// Remove the last item
acc.shift();
}
// Add the next item
acc.push(point);
return acc;
}),
subscription = points.subscribe(log);
This approach is also more effecient, as you're only creating one observable, rather than a new observable per each item.
If you wanted to generalize the approach, you could create an operator:
Rx.Observable.prototype.rollingBuffer = function (count) {
return this.scan([], function (acc, point) {
var length = acc.length,
start = (length >= count)
? (length - count + 1)
: 0;
return acc
.slice(start)
.concat([point]);
});
};