Last active
December 15, 2018 17:16
-
-
Save caroso1222/0fb6a3c68d3b33c7a62ac43c52ab5ec0 to your computer and use it in GitHub Desktop.
pausable observable RxJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Subject, NEVER, interval } from 'rxjs'; | |
import { switchMap, materialize, dematerialize } from 'rxjs/operators'; | |
const source = interval(1000); | |
const pauser = new Subject(); | |
// switch between source and 'NEVER' depending on our 'control' pauser | |
pauser | |
.pipe( | |
switchMap(paused => paused ? NEVER : source.pipe(materialize())), | |
dematerialize() | |
) | |
.subscribe(console.log); | |
pauser.next(true); // pause | |
pauser.next(false); // resume |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment