New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
What's the different between a hot observable and a subject #2604
Comments
A cold Observable is an Observable that re-executes its const cold = new Observable(function subscribe(observer) {
console.log('subscribed');
observer.next(Math.random());
observer.complete();
});
// > subscribed
// sub 1: 0.1231231231231
cold.subscribe((num) => console.log('sub 1:', num));
// > subscribed
// sub 2: 0.09805969045
cold.subscribe((num) => console.log('sub 2:', num)); A hot Observable is a source Observable (cold or otherwise) that has a Subject between the source and subscribers. When a hot Observable is subscribed to, the subscription is internally routed to the inner Subject transparently, and the Subject is subscribed to the source Observable. This ensures the source Observable only has one subscriber (the Subject), and the Subject shares the source's value with many Subscribers: const cold = new Observable(function subscribe(observer) {
console.log('subscribed');
observer.next(Math.random());
observer.complete();
});
const hot = cold.publish();
hot.subscribe((num) => console.log('sub 1:', num));
hot.subscribe((num) => console.log('sub 2:', num));
hot.connect(); // <-- this subscribes the inner Subject to the cold source
// > subscribed
// > sub 1: 0.249848935489
// > sub 2: 0.249848935489 You can make an Observable hot via In addition to |
@trxcllnt |
@trxcllnt This is the best simplest answer including the new changes (IMHO) where a subject is in the middle. |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
The text was updated successfully, but these errors were encountered: