Sunday, September 22, 2019

RxJava


Learning RxJava: a good book published in 2017


Cold Observables: pull; replay the emissions to each Observer; most likely for finite dataset.

Hot Observables: broadcast the same emission to all observers at the same time; most likely for infinite dataset.

ConnectableObservable is hot.  publish() or replay() will convert any observable to hot observable. connect() will fire emissions.

Using ConnectableObservable to force each emission to go to all Observers simultaneously is known as multicasting

Subject: is both Observable and Observer. acting as a proxy mulitcasting device (kind of like an event bus).  It is "mutable variables of reactive programming"

The simplest Subject type is the PublishSubject, which, like all Subjects, hotly broadcasts to its downstream Observers.

Subjects act like magical devices that can bridge imperative programming with reactive programming.

you will most likely use Subjects for infinite, event-driven (that is, user action-driven) Observables.

the onSubscribe(), onNext(), onError(), and onComplete() calls are not thread safe!

Following will safely sequentialize concurrent event calls so no train wrecks occur downstream:
            Subject<String> subject =
                    PublishSubject.<String>create().toSerialized();

Use subscribeOn(Schedulers.computation()) to use a thread pool to emit concurrently.
use observeOn() to intercept each emission and push them forward on a different Scheduler

when you have a flow of 10,000 emissions or more, you will definitely want to use a Flowable (which supports backpressure) instead of an Observable.

.buffer()
.window() for batch
.sample(, TimeUnit.MILLISECONDS) for sampling
.throttleFirst()
.onBackpressureBuffer(16, () -> {}, BackpressureOverflow.ON_OVERFLOW_DROP_OLDEST) for customized action when the buffer fills up
.onBackpressureDrop for dropping


No comments:

Post a Comment