Question
RxJS observable that fires if it's the first of a kind OR a certain time went by
Hey RxJS pros out there,
I have a stream that procs multiple times a second. Now I want an observer that procs if it's the first of a kind OR a certain time went by after the last proc. I want to achieve this with pure RxJS without additional "helper variables".
Scenario:
const list = ["foo", "foo", "bar", "foo",
"foo", "foo", "foo", "foo",
"foo", "foo", "bar", "foo"];
// in real world obs$ drops "foo" and "bar" randomly infinite times
const obs$ = timer(0, 100).pipe(take(12));
$obs
.pipe(map((v, i)=>list[i]+"#"+i))
.subscribe(console.log);
Below are all the values fired by the observable. I want to catch the green ones (✅), and ignore the red ones (❌).
// ⬇
"foo#1" // ✅ first of a kind
"foo#2" // ❌
"bar#3" // ✅ first of a kind
"foo#4" // ✅ first of a kind
"foo#5" // ❌
"foo#6" // ❌
"foo#7" // ❌
"foo#8" // ❌
"foo#9" // ✅ <-- I want this one too, because a certain time (0.5 seconds) went by
"foo#10" // ❌
"bar#11" // ✅ first of a kind
"foo#12" // ✅ first of a kind
So I want this output:
1#foo
3#bar
4#foo
9#foo
11#bar
12#foo
How?