Wednesday, June 5, 2019

Learning RxJava: Thoughts on Chapter 7

Buffering

The buffer() operator will gather emissions into collections according to a specified criteria. The default collection type is as a list, but a different collection type can be specified.

You can specify a count, and the buffer operator will group emissions into lists with a size equal to the count, with the exception of the last list, which will contain the remainder of what couldn't be divided equally.

You can additionally specify a skip amount as well, which determines how far to move forward for the start of each list. If no skip is given it defaults to be the same as the count operator so as to break them into distinct groups. But if, for instance, you specify a count of 2 and a skip of 1, then each list will have 2 elements, but the start of a list will only be one more than the start of the previous list, which will cause the last element of the previous list the first element of the current list to be the same. This can be very useful for operations where you need to know both the current emission and the previous emission to do something.

You can also buffer based off of time, so that all emissions within a specified time are grouped together. There is also a timeSkip option, which is the time based equivalent of skip. There is also an optional count operation, so that it will group based off of whichever is reached first, the time or the count.

Additionally, buffer can take another Observable as a parameter, and anytime that Observable emits serves as the cutoff point for grouping.

Windowing

Windowing works the exact same as buffering, but instead of grouping the emissions into collections, the emissions are grouped into Observables. This can be useful in that it will allow you to work with the emissions coming in immediately, instead of waiting for the last one to be available before you can look at any of them.

Throttling

Throttling will throw away emissions when they are coming too fast. Variants include throttleLast(), throttleFirst(), and throttleWithTimeout(). throttleLast()/sample() will only emit the last item from a fixed time interval. throttleFirst() will only emit the first item from a fixed time interval. throttleWithTimeout()/debounce() will wait until there is a pause of a specified length, and then it will send the last emission from before the pause. The downside is that emissions are delayed until the end of the specified time period.

Switching

switchMap() is similar to flatMap, in that it maps an emission to an observable. The difference between them being that while flatMap will combine the emissions from all Observables into a single Observable emiting all emissions, switchMap will unsubscribe from an Observable as soon as an emission comes in and creates a new Observable. So at any given point, it is only passing on the emissions of one Observable, and that one Observable is the one created by the most recently received emission. This can be useful in cancelling and restarting expensive operations that are kicked off by user events.