RxJS simulates App to exit the function. If you click twice in two seconds, you will exit.

problem description

use RxJS to simulate the two-click exit function of App.
uses two methods, both of which are not very satisfactory. Is there a better way?

related codes

method 1: the problem with using the bufferTime operator
is that the buffer time of bufferTime does not start with a click, which may cause a value to be issued immediately after a click

click$.pipe(
  throttleTime(2000),
  tap(() => console.log("one")),
  concatMap(() => click$.pipe(
    tap(() => console.log("two")),
    take(1),
    takeUntil(of(1).pipe(delay(2000)))
  ))
).subscribe(v => console.log("success"))
< hr >

finally see my supplement downstairs

Jul.14,2021

click$
  .map(t => Date.now())
  .scan((prev, cur) => ({
    cur,
    result: cur - prev.cur
  }), {
    cur: 0
  })
  .map(t => t.result / 1000)
  .filter(t => t < 2)
  .take(1)
  .subscribe(t => {
    console.log(t)
  });
Menu