Skip to content

Instantly share code, notes, and snippets.

@caroso1222
Last active December 15, 2018 17:16
Show Gist options
  • Save caroso1222/0fb6a3c68d3b33c7a62ac43c52ab5ec0 to your computer and use it in GitHub Desktop.
Save caroso1222/0fb6a3c68d3b33c7a62ac43c52ab5ec0 to your computer and use it in GitHub Desktop.
pausable observable RxJS
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