RXJS: throttleTime plus last value

In RxJS 6 there are some additional options for the throttleTime operator that are undocumented right now and where you can make it to emit at both start and end of the duration. So maybe this could help you.

https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/throttleTime.ts#L55

https://github.com/ReactiveX/rxjs/blob/master/src/internal/operators/throttle.ts#L12

However, since you want to reset the timeout on every emission it'll be more complicated. This should be simplified example with random emits but I wonder if there's an easier way to do it:

const shared = source.pipe(shareReplay(1))

shared
  .pipe(
    exhaustMap(val => shared.pipe(
      timeout(1000),
      catchError(() => empty()),
      debounceTime(1000),
      take(1),
      startWith(val),
    ))
  )
  .subscribe(v => console.log(v))

This demo debounces after 175ms gap. I hope it makes sense to you.

Demo: https://stackblitz.com/edit/rxjs6-demo-ztppwy?file=index.ts


This operator should give you the first value, the throttled stream and the last value:

export function throunceTime<T>(duration: number): MonoTypeOperatorFunction<T> {
  return (source: Observable<T>) =>
    merge(source.pipe(throttleTime(duration)), source.pipe(debounceTime(duration)))
      .pipe(throttleTime(0, undefined, { leading: true, trailing: false }));
}

Tags:

Rxjs