Skip to content

Instantly share code, notes, and snippets.

@toan2406
Last active March 5, 2017 07:48
Show Gist options
  • Save toan2406/aff4deb4f76f2cd2972ea26a13f23de3 to your computer and use it in GitHub Desktop.
Save toan2406/aff4deb4f76f2cd2972ea26a13f23de3 to your computer and use it in GitHub Desktop.
/*
* 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