i have 2 independent observables i'd couple in such manner every time first emits value, causes second emit value.
in scenario, first observable infinite sequence based on async node.js-style method variable callback timing (no usage of delay
or timer
allowed). second observable finite sequence of independent values.
perhaps diagram can express i'm looking for:
i: ---a--d-----q-g---b-> (i infinite) f: -3512-|-> (f finite) o: ---3--5-----1-2-|-> (o finite)
the output sequence (o) finite values (f), based on timing of (i).
here's (i):
// i'm stuck this, cannot change... function asyncoperation(callback) { const delaymsec = math.floor(math.random() * 1000); timers.settimeout(callback, delaymsec, null, delaymsec); } const callbackobservable = rx.observable.bindnodecallback(asyncoperation); const infinitecallbacksequence = rx.observable .defer(callbackobservable) .expand(x => callbackobservable());
and simplicity, here's (f):
const finitesequence = rx.observable.from([3, 5, 1, 2]);
(this build upon previous question of mine here.)
i don't understand things enough make combine*
, merge*
, or *map
work generate want -- assuming can. seems want take(1)
on (i) each time (o) emits.
how behavior described?
would observable.zip
work usecase?
const rx = require('rxjs'); const timers = require('timers'); const finite = rx.observable.from([3, 5, 1, 2]); // i'm stuck this, cannot change... function asyncoperation(callback) { const delaymsec = math.floor(math.random() * 1000); timers.settimeout(callback, delaymsec, null, delaymsec); } const callbackobservable = rx.observable.bindnodecallback(asyncoperation); const infinitecallbacksequence = rx .observable .defer(callbackobservable) .expand(x => callbackobservable()); const combined = rx.observable.zip( finite, infinitecallbacksequence ).foreach(v => console.log(v));
running on node 6:
➜ $ node test.js [ 3, 816 ] [ 5, 297 ] [ 1, 95 ] [ 2, 677 ] ➜ $
Comments
Post a Comment