javascript - Reactive programming - value is greater than X for N seconds -
i have stream randstream emit random value every half second , boolstream converts value randstream boolean.
let randstream = kefir.frompoll(500, () => math.random()) let boolstream = kefir.map((rand) => rand > 0.5) i want emit true when boolstream emits true 5 seconds (in row). otherwise emit false.
i'm using kefir.js library.
do have ideas? thanks.
with given conditions when know exact rate @ randstream emit numbers, pretty easy achieve .slidingwindow:
let result = boolstream .slidingwindow(10, 10) .map(items => _.every(items)) .skipduplicates(); if want work rate of events, can try like:
let result = boolstream .scan(({mostrecentfalse, latestvalue}, bool) => { return bool ? {mostrecentfalse, latestvalue: true} : {mostrecentfalse: date.now(), lastvalue: false} }, {mostrecentfalse: date.now()}) .changes() .map(({mostrecentfalse, latestvalue}) => latestvalue && (date.now() - mostrecentfalse > 5000)) .skipduplicates();
Comments
Post a Comment