system.reactive - Is there an Rx operator for combining the latest from streams 1 and 2 only when stream 2 emits things? -
here's attempt @ drawing marble diagram --
stream 1 = a----b----c---------d------> (magical operator) stream 2 = 1----------2-----3-----4---> stream 3 = 1a---------2c----3c----4d-->
i looking generates stream 3 streams 1 , 2. basically, whenever emitted stream 2, combines latest stream 1. combinelatest similar want want things emitted stream 3 when emitted stream 2, not stream 1. operator exist?
there operator need: 1 overload of sample takes observable instead of duration parameter. documentation here: https://github.com/reactivex/rxjava/wiki/filtering-observables#sample-or-throttlelast
the usage (i'll give examples in scala):
import rx.lang.scala.observable import scala.concurrent.duration import duration._ def o = observable.interval(100.milli) def sampler = observable.interval(180.milli) // often, need sampled observable o.sample(sampler).take(10).subscribe(x ⇒ println(x + ", ")) thread.sleep(2000) // or, use case o.combinelatest(sampler).sample(sampler).take(10).subscribe(x ⇒ println(x + ", ")) thread.sleep(2000) the output:
0, 2, 4, 6, 7, 9, 11, 13, 15, 16, (2,0), (4,1), (6,2), (7,3), (9,4), (11,5), (13,6), (15,7), (16,8), (18,9), there slight catch in duplicate entries sampled observable swallowed (see discussion @ https://github.com/reactivex/rxjava/issues/912). other that, think looking for.
Comments
Post a Comment