I reviewed the following SO question:
What are the Hot and Cold observables?
To summarize:
- a cold observable emits its values when it has an observer to consume them, i.e. the sequence of values received by observers is independent of time of subscription. All observers will consume the same sequence of values.
- a hot observable emits value independently of its subscriptions, i.e. the values received by observers are a function of the time of subscription.
Yet, I feel like hot vs. cold is still a source of confusion. So here are my questions:
Are all rx observables cold by default (with the exception of subjects)?
I often read that events are the typical metaphor for hot observables, but I also read that
Rx.fromEvent(input, 'click')
is a cold observable(?).Are there/what are the Rx operators which turn a cold observables into a hot observable (apart from
publish
, andshare
)?
For instance, how does it work with Rx operator
withLatestFrom
? Letcold$
be a cold observable which has somewhere been subscribed to. Willsth$.withLatestFrom(cold$,...)
be a hot observable?
Or if I do
sth1$.withLatestFrom(cold$,...), sth2$.withLatestFrom(cold$,...)
and subscribe tosth1
andsth2
, will I always see the same value for bothsth
?I thought
Rx.fromEvent
creates cold observables but that is not the case, as mentioned in one of the answers. However, I am still baffled by this behaviour: https://codepen.io/anon/pen/NqQMJR?editors=101. Different subscriptions get different values from the same observable. Wasn't theclick
event shared?