Skip to content

Instantly share code, notes, and snippets.

@obenjiro
Last active August 16, 2018 13:05
Show Gist options
  • Save obenjiro/345ba0b785a663bd6b433dbbac07f279 to your computer and use it in GitHub Desktop.
Save obenjiro/345ba0b785a663bd6b433dbbac07f279 to your computer and use it in GitHub Desktop.
Naive of cancelable switchMap
const { interval, of, from, pipe, empty } = Rx;
const { take, tap, share, takeUntil, switchMap,startWith, withLatestFrom, filter, concatMap, delay, merge } = RxOperators;
const $refresh = interval(1000).pipe(
take(4),
concatMap((x) => of(x).pipe(delay(x <= 2 ? 2000 : 1000))),
share()
)
const cancel$ = of(-1).pipe(delay(5200), share());
$refresh.pipe(
switchMap((r) => of(r).pipe(
delay(1800),
takeUntil(cancel$)
))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment