RxJs: How to emit events at predefined times? -
i have pre-defined events set occur @ specific times. , have timer, this:
const timer = rx.observable.interval(100).timeinterval() .map(x => x.interval) .scan((ms, total) => total + ms, 0)
the timer emits close 100,200,300,400,500 (although in reality it's more 101,200,302,401,500...which totally fine) have stuff want @ times. example, let's want stuff @ following times:
const stuff = rx.observable.from([1000, 2000, 2250, 3000, 5000]);
what i'd combine "stuff" , "timer" in such way resulting stream emits value once per time defined in "stuff" @ time (or ever later). in case, t=1000 ms, 2000 ms, 2250 ms, 3000 ms , 5000 ms. note: 2250 guy should emit around time 2300 because of interval size. that's fine. can't come or more once.
i have 1 solution, it's not good. re-starts "stuff" every single step (every single 100 ms in case) , filters , takes 1. prefer that, once event emitted "stuff", gone, subsequent filters on don't have values.
in real application, there stuff , stuff2 , maybe stuff3...(but call them else!)
thanks in advance! hope clear.
if i've understood you're after correctly, should achievable simple projection:
const times$ = stuff.flatmap(x => rx.observable.timer(x));
here's working sample: https://jsbin.com/negiyizibu/edit?html,js,console,output
edit
for second requirement, try this:
const times$ = rx.observable .from([{"val":"jeff", "t": 1000}, {"val":"fred", "t": 2500}]) .flatmap(x => rx.observable.timer(x.t).map(y => x.val));
Comments
Post a Comment