Last active
December 15, 2018 17:31
-
-
Save caroso1222/33f0899ee3e0eaabb026dc9fdc0f0476 to your computer and use it in GitHub Desktop.
rxjs-pausable-operator
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
class PausableObservable<T> extends Observable<T> { | |
private pauser: BehaviorSubject<boolean>; | |
pause(){ | |
this.pauser.next(true); | |
} | |
resume(){ | |
this.pauser.next(false); | |
} | |
} | |
function pausable() { | |
return function pauseFn<T>(source: Observable<T>): PausableObservable<T> { | |
const pausableProto = PausableObservable.prototype; | |
const pauser = new BehaviorSubject(false); | |
const newSource = pauser.pipe( | |
switchMap(paused => paused ? NEVER : source.pipe(materialize())), | |
dematerialize() | |
) | |
const pausable = Object.create(newSource, { | |
pause: { value: pausableProto.pause }, | |
resume: { value: pausableProto.resume }, | |
pauser: { value: pauser } | |
}); | |
return pausable as PausableObservable<T>; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment