いわゆる入門。
よくあるincイベントとdecイベントを合成させて現在の値を取るってやつ。
ブラウザ立てるのめんどいので、coffeeでnode環境でイベントソースはEventEmitter。
Kefir = require 'kefir'
{EventEmitter} = require 'events'
emitter = new EventEmitter
inc = Kefir.fromEvents emitter, 'inc'
dec = Kefir.fromEvents emitter, 'dec'
Kefir.merge([
inc.map(-> 1)
dec.map(-> -1)
])
.scan ((acm, v) -> acm + v), 0
.onValue (i) -> console.log i
emitter.emit 'inc'
emitter.emit 'dec'
emitter.emit 'dec'
emitter.emit 'inc'
emitter.emit 'inc'
emitter.emit 'inc'
結果
0
1
0
-1
0
1
2