by Ahmed Rizwan
Rx — If the Operators could speak!
If the operators could talk, how exactly would they tell us what they do?
In order to take full advantage of Rx, you need a clear understanding of what Rx Operators are and what they do.
This is what the Operators would be telling the observables if they could talk when we use them.
For this article, I’ll assume that you already know what Rx is. If not, go read this. Or just simply google Rx and you’ll find a ton of helpful articles, tutorials, and videos.
I tell you what to emit, when to terminate, and what error to throw. ‘Cause I’m the boss.
You only get to “create” yourself once someone subscribes to you. And it’ll be a brand new version of yourself every single time.
Hm. Emit nothing. And then die, please.
Emit nothing. And don’t… ever… terminate.
Emit nothing, and then throw an error, OK?
I’ll give you some objects, then you emit them right back at me.
How about this: you emit items. But not immediately. Send them back, one by one, after certain “intervals.”
I need just one thing back from you. Just one.
I give you a range of integers, then you emit back all the values in that range.
How about you emit that same object repeatedly.
Ok. I have a function. When it returns, you start emitting. But only when it returns. Got it?
So you got an item. Don’t emit it just yet. I’ll tell you the exact time when you should emit it. Don’t jump the gun.
Ok, so here’s the deal. Whatever it is you normally emit, well don’t emit that. Instead ,collect the items into bundles over time. And send bundles instead. ‘Cause I want bundles!
So, like, if you have lists of items and there’s another observable that’s full of items, can you please “flatten” yourself and that observable so you can just send items?
Transform each item into another item.
Transform each item into another item, like you did with map. But also include the “previous” item when you get around to doing a transform.
Only emit if a certain amount of time is passed.
Emit only distinct items. All right?
I tell you the index. You emit the item at that index.
I give you a criteria. You give me items that pass the criteria.
Just give me back the first item.
Do not, I repeat, do not emit a single item. And then die.
Just give me back the last item.
I give you an interval. You give me only the most recent items from that that interval.
OK, skip the first n items, would you?
Skip the last n item s. Yeah, those ones.
Emit only the first n items.
Emit only the last n items.
Here are two observables. Let’s pretend they’re only one observable.
Here are two observables. But I get to tell you which one to start with.
Here are two observables. Between the two, make pairs of the latest items.
Here are two observables. But I tell you how to combine their items (through a function, of course).
After an error is thrown, continue on with the emits.
After an error is thrown, restart from the very beginning.
Just add a delay before you start emitting, OK?
“Observational” code should run on this particular thread.
“Subscription” code should run on this particular thread.
You can start emitting now. *music intensifies*
OK, so observables send back items, right? Instead, I want you to send the time intervals back. Like the time differences between each emission.
Set a TimeOut on each emission. And if an item doesn’t get emitted within that time, just throw an error ?
Conditional and Boolean
If all items fulfill a certain criteria, return true.
Here are at least two observables. Give me the one that starts emitting first.
If I ask for an item, can you tell me whether you already have it?
When you have nothing to emit, here’s a default value that you can send back.
Here are two observables. Return true if their items (and their sequence) are the same.
Here are two observables. Skip the items of the first one until the second one starts emitting.
I give you a condition. You emit items until that condition becomes false.
Here are two observables. Only give me the items of the first one until the second one starts emitting.
Give me an average of your Integer items.
Give me a count of your items.
Emit only the maximum-valued item.
Emit only the minimum-valued item.
Do a scan, but only emit the final value.
Return the sum of all your items.
Convert an observable into a List, Map or Array, or whatever I tell you to.
Anyway, thank you for reading. I hope the article helped you better understand what each of these commands does in a fun way.