Note: I will add a topic on TestSchedulers prior to this topic.
Cold Vs Hot !
Let's first discuss a scenario of Enumeration world. We need a function that bumps out 5 integers from 1 to 5 . We can write them in two ways:-
public IEnumerable<int> GetMeValue()
{
return new List<int> { 1, 2, 3, 4, 5 };
}
AND
public IEnumerable<int> GetMeValue()
{
for (int i = 1; i <= 5; i++)
yield return i;
}
Difference between these two implementation is that in the second implementation, values are created and returned on the fly. Unless we use this, values are not created or we can say this is COLD. But in the first implementation, all values are created as soon as this method is called. We can say this is HOT!
Similarly for observables we can say that they are HOT when they are being produced regardless of whether some subscriber is there to consume them, e.g. MouseMove event. On the other hand, if they are only published if subscribers exist, they are COLD observable.
LeeCampbell explains:-
COLD Observable - Streams that are passive and start publishing on request,
HOT Observable - Streams that are active and publish regardless of subscriptions.
Observable.Defer method can be used to convert a hot observable to cold ans Observable.Publish can be used to convert a cold observable to hot.
Copied from http://stackoverflow.com/questions/2521277/what-are-the-hot-and-cold-observables
Cold Vs Hot !
Let's first discuss a scenario of Enumeration world. We need a function that bumps out 5 integers from 1 to 5 . We can write them in two ways:-
public IEnumerable<int> GetMeValue()
{
return new List<int> { 1, 2, 3, 4, 5 };
}
AND
public IEnumerable<int> GetMeValue()
{
for (int i = 1; i <= 5; i++)
yield return i;
}
Difference between these two implementation is that in the second implementation, values are created and returned on the fly. Unless we use this, values are not created or we can say this is COLD. But in the first implementation, all values are created as soon as this method is called. We can say this is HOT!
Similarly for observables we can say that they are HOT when they are being produced regardless of whether some subscriber is there to consume them, e.g. MouseMove event. On the other hand, if they are only published if subscribers exist, they are COLD observable.
LeeCampbell explains:-
COLD Observable - Streams that are passive and start publishing on request,
HOT Observable - Streams that are active and publish regardless of subscriptions.
Observable.Defer method can be used to convert a hot observable to cold ans Observable.Publish can be used to convert a cold observable to hot.
Copied from http://stackoverflow.com/questions/2521277/what-are-the-hot-and-cold-observables
Hot observable is an exact match for event. In events, values usually are fed into the handler even if no subscribers are listening. All subscribers are receiving the same set of values. Because of following the "event" pattern, hot observables are easier to understand than the cold ones.
Cold observable is also like an an event, but with a twist - Cold observable's event is not a property on a shared instance, it is a property on an object that is produced from a factory each time when somebody subscribes. In addition, subscription starts the production of the values. Because of the above, multiple subscribers are isolated and each receives its own set of values.
The most common mistake RX beginners make is creating a cold observable (well, thinking they are creating a cold observable) using some state variables within a function (f.e. accumulated total) and not wrapping it into a .Defer() statement. As a result, multiple subscribers share these variables and cause side effects between them.
Things to do: Get hands dirty on Defer and Publish
No comments:
Post a Comment