Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RXJS: Skipping values from observable triggered by other observable

I am trying to achieve the following behavior: I have a source observable that emits every second. I am trying to ignore values for some period(10 seconds) of time if another observable(mySubject) emitted value. This is what I came for:

this.source.pipe(
      takeUntil(this.mySubject),
      repeatWhen((observable) => observable.pipe(delay(10000))),
      tap((x) => console.log(x)),
).subscribe();

Now it's stoping the emitting of the source for 10 seconds on every mySubject emission.

The problem is that I need it if another emission of mySubject to reset the "count" of the 10 seconds and ignore for another 10 seconds without emitting anything meanwhile.

How can I achieve this?

like image 562
Moshe Yamini Avatar asked Jan 24 '26 01:01

Moshe Yamini


1 Answers

I'm afraid this requires a little more complicated solution:

const ignore$ = this.mySubject.pipe(
  switchMap(() => merge(of(true), of(false).pipe(delay(10 * 1000)))),
);

this.source.pipe(
  withLatestFrom(ignore$),
  filter(([value, ignore]) => !ignore),
  map(([value]) => value),
).subscribe(...);
like image 189
martin Avatar answered Jan 25 '26 13:01

martin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!