Search code examples
angularobservablebehaviorsubject

Be notified when debounceTime has elapsed from an Observable


Say I am provided with an Observable on a Subject like below:

private subject: Subject<any> = new Subject<any>();
public observer: Observable<any>;
constructor() {
    this.observer = this.subject.debounceTime(300).switchMap(// do something);
}

and then in my code I have

otherComponent.observer.subscribe(// obtain new value);

I know that subscribing to this observer, I will get the new value once it's subject's value is changed (i.e. after "do something returns"). I wonder if I can know, via this observer, that the debounceTime 300ms has ended/the process to start changing the value is started (i.e. before "do something starts").

If not, how to you achieve the same effect (e.g. via changing another Subject as Observable before "do something" inside switchMap())?


Solution

  • You can add a side effect:

    this.subject.debounceTime(300)
      .do(value => /* Do something */)
      .switchMap(// do something);
    

    Or in newer version of rxjs when using lettable operators, do has been renamed to tap.