Last active
March 5, 2017 07:48
-
-
Save toan2406/aff4deb4f76f2cd2972ea26a13f23de3 to your computer and use it in GitHub Desktop.
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
/* | |
* Cold observable: | |
* produce values upon each subscription | |
*/ | |
const coldObs$ = Rx.Observable.interval(1000) | |
/* | |
* Warm observable: | |
* share the value source to all subscriptions, | |
* but doesn't start producing before subscriptions exist | |
*/ | |
const warmObs$ = Rx.Observable.interval(1000) | |
.publish() // convert into connectable Observable | |
.refCount() // automate the connecting process when the first observer subscribes | |
// can be replace with the .share shortcut | |
/* | |
* Hot observable: | |
* share the value source to all subscriptions, | |
* and produce values no matter if there is any subscription | |
*/ | |
const hotObs$ = Rx.Observable | |
.interval(1000) | |
.publish() | |
hotObs$.connect() | |
/* Test */ | |
console.clear() | |
setTimeout(() => { | |
hotObs$.subscribe(val => console.log('Subscriber 1: ' + val)) | |
setTimeout(() => | |
hotObs$.subscribe(val => console.log('Subscriber 2: ' + val)) | |
, 3000) | |
}, 3000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment