Principals Workshop in Allahbad

Pradeep Richa Educare Foundation (PREF), a social non-profit organization, based in Delhi and a CSR initiative of REPL, is a well-known consultancy company in the real estate sector. PREF was founded…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Throttling notifications from multiple users with RxJS

Or how the groupBy’ operator works under-the-hood.

Quite interesting, isn’t it? :-)

*In a config param we can also specify whether throttling will be leading(default) or trailing, but here we will talk about leading throttling.

Now, with that in mind, we can start.

Without any throttling, our code will look like this:

And work like this:

So what we need to do is to implement throttling for each separate user notification! This means using the only throttleTime operator will not help us here because it doesn’t recognize different users and handle all the notifications in the same manner.

So for our case it will work like this:

2. Then we should check in an internal cache if we have respective Observable for this GroupedObservablesIdentifier. If not — create one and emit, if yes — just get it and emit to subscribers. Since groupBy emits Observables — we should use mergeMap to handle them.

With all that our solution will look like:

And it works as expected:

Our first assumption was that the operator should calculate GroupedObservablesIdentifier using source value and keySelector. And here it is:

Let's check our second assumption — that operator should have a cache where it saves groupObservables and emits them or create new ones if needed:

OK, here is the cache — groupBy uses Map for it. And in line 170 it checks if we already have groupObservables for a respective group:

But what if groupObservables is not yet created?

If we have group Subject in a cache — in that case we emit source value with this Subject (So respective groupObservable re-emit it to mergeMap operator(in our solution), and mergeMap operator will re-emit it to final subscribers).

Add a comment

Related posts:

Exploring the new look Oklahoma City Thunder

The Oklahoma City Thunderadded more shockwaves to what has proven to be one of the more entertaining off-season’s in the NBA by acquiring Carmelo Anthony from the New York Knicks for Enes Kanter…

Make Yourself More Likeable Without Saying a Word

I was surprised by the question. In my opinion, my client was very warm and quite likeable. It was my initial impression when we first met. When he described his interactions with his clients, it was…

the fear of being judged.

Two days ago I shared something very personal and immediately after I published the story, that same old fear of being judged started invading my soul and my mind. This afternoon I could barely…