Created
February 24, 2016 04:35
-
-
Save jwthomp/ecb2d8b4ac4d2a5c063d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*---------------------------------- | |
* 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