Skip to content

Instantly share code, notes, and snippets.

@dotproto
Created March 15, 2017 16:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dotproto/50689b584e97e260d2daae1e1cfffa52 to your computer and use it in GitHub Desktop.
Save dotproto/50689b584e97e260d2daae1e1cfffa52 to your computer and use it in GitHub Desktop.
Observable -> BehaviorSubject
// Open http://reactivex.io/rxjs/manual/overview.html to run this code
var timer = new Rx.Observable((obs) => {
var counter = 0;
var int = setInterval(() => obs.next(++counter), 1000);
return () => {
clearInterval(int);
obs.complete();
}
});
var done = new Rx.Subject();
var subject = new Rx.BehaviorSubject();
timer.subscribe(subject);
var counter = 0;
subject.takeUntil(done).subscribe({
next: (v) => console.log('observerA: ' + v)
});
setTimeout(function() {
subject.takeUntil(done).subscribe({
next: (v) => console.log('observerB: ' + v)
});
}, 2500);
function kill() {
done.next(true);
done.complete();
return '-- done --'
}
setTimeout(function() {
kill();
}, 5000);
'-- starting --'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment