0

I have an Observable that emits values about 10x a second, and I need this amount of data only when performing some operations in code, but on the UI I would like to slowdown and get values only once a second to the use have time to at least read those values, so I'm trying to implement my own operator that does this for me, it is the first time that I create an operator and I'm just worried about performance issues.

function pause<T>(pauseFor: number): OperatorFunction<T, T> {
  return (source: Observable<T>) => defer<Observable<T>>(() => {
      let lastObserved: T;
      let start = performance.now();
      return source.pipe(
          tap(v => lastObserved = v),
          switchMap(() => (performance.now() - start) >= pauseFor ? of(lastObserved) : EMPTY),
          tap(() => {
            lastObserved= null;
            start = performance.now();
          })
      );
  });
}

Is right to do it like this or is there an easiest way or a better one?

Leo Letto
  • 1,774
  • 1
  • 12
  • 17

0 Answers0