Skip to content

Instantly share code, notes, and snippets.

@jwthomp
Created February 24, 2016 04:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwthomp/ecb2d8b4ac4d2a5c063d to your computer and use it in GitHub Desktop.
Save jwthomp/ecb2d8b4ac4d2a5c063d to your computer and use it in GitHub Desktop.
/*----------------------------------
* Kefir.Bus
*/
var Bus = function() {
Kefir.Pool.call(this);
}
Bus.prototype = Object.create(Kefir.Pool.prototype);
Bus.prototype.constructor = Bus;
Bus.prototype.push = function(x) {
this.plug(Kefir.constant(x));
};
/*----------------------------------*/
var bus = new Bus();
var rate = bus.scan(function(acc, next) {
var prevTime = acc[0];
var prevRate = acc[1];
var prevTotal = acc[2];
var newTime = next[0];
var newRate = next[1];
var newTotal = prevTotal + prevRate * (newTime - prevTime);
return [newTime, newRate, newTotal];
}, [0, 0, 0]);
var value = rate.map(x => x[2]);
bus.push([1,2]);
bus.push([3,4]);
var c = new Bus();
var result = value.sampledBy(c);
result.onValue(x => console.log("Value: ", x));
c.push(1);
bus.push([10,5]);
c.push(1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment