throttleFirst()

The throttleFirst() operates almost identically to throttleLast(), but it will emit the first item that occurs at every fixed time interval. If we modify our example to throttleFirst() every 1 second, we should get an output like this:

Observable.concat(source1, source2, source3)
.throttleFirst(1, TimeUnit.SECONDS)
.subscribe(System.out::println);

The output is as follows:

SOURCE 1: 100
SOURCE 2: 300
SOURCE 3: 2000
SOURCE 3: 4000

Effectively, the first emission found after each interval starts is the emission that gets pushed through. The 100 from source1 was the first emission found on the first interval. On the next interval, 300 from source2 was emitted, then 2000, followed by 4000. The 4000 was emitted right on the cusp of the application quitting, hence we got four emissions from throttleFirst() as opposed to three from throttleLast().

Besides the first item being emitted rather than the last at each interval, all the behaviors from throttleLast() also apply to throttleFirst(). Specifying shorter intervals will yield more emissions, whereas longer intervals will yield less.

Both throttleFirst() and throttleLast() emit on the computation Scheduler, but you can specify your own Scheduler as a third argument.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.109.60