Created
March 15, 2017 16:27
-
-
Save dotproto/50689b584e97e260d2daae1e1cfffa52 to your computer and use it in GitHub Desktop.
Observable -> BehaviorSubject
This file contains 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
// 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