Cold & Hot Observable
|
Single Value |
Multiple Value |
Synchronous |
COLD Observable |
COLD Observable |
Asynchronous |
COLD Observable (Ajax) | HOT Observable (Promise) |
HOT Observable |
We can categorize by native type behind but there is also the possibility to heat up a Cold Observable
Cold Observable
- Lazy Observable
- Pure Function => referential transparency
- Independent subscription
Like a synchronous code behaviour
Hot Observable
- Keep Lazy Observable data are ignored without subscription
- a hot observable shares the same subscription to all observers => Dependent subscription
Like a asynchronous code behaviour
Replay vs Resubscribe
Observable replay the pipeline can be considered like Hot events are buffered internally
Promise Hot usual caveat:
Here
=> Solution consist to wrap the [Fetch|XHR] in a higher order Observable:
Here
What's the temperature today?
Temperature depends on:
- The data source which by design are cold or hot
- The context where data come from inside (cold) or outside (hot) Observable context
=> Control the temperature by wrapping or not the data source
COLD is when your observable creates the producer.
HOT is when your observable closes over the producer.
Ben Lesh on Medium
Cool Down Hot Observable
Avoid the side effect by wrapping and instantiating a new [Promise|WebSocket] inside the "Producer" function
Here
Heat Up Cold Observable
Outside the data source from the Producer.
Decoupled the subscription from the activation of the event source
const source$ = Rx.Observable.timer(0, 1000)
.take(10)
.do(num => {
console.log(`Running some code with ${num}`);
});
// Here stream become hot-by-operator
const shared$ = source$.share();
shared$.subscribe(myObserver('ObsA'));
shared$.subscribe(myObserver('ObsB'));
const myObserver = (name) => {
return {
next: x => {
console.log(`Next: ${name} ${x}`)
},
error: err => {
console.log(`Error: ${err}`)
},
complete: () => {
console.log('Completed')
}
}
}
Warm Observable vs Hot Observable
Unsubscription liability?
|
COLD |
WARM |
HOT |
RxJS liability? |
True |
True (share, fromEvent) |
False => memory leak Only concern manual Multicasting operator (publish) |
Numbers of pipeline
|
COLD |
WARM |
HOT |
0 |
0 |
0 |
1 |
1 |
1 |
1 |
1 |
X |
X |
1 |
1 |