Skip to content
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

Closed
code-tinker opened this issue May 17, 2017 · 4 comments
Closed

What's the different between a hot observable and a subject #2604

code-tinker opened this issue May 17, 2017 · 4 comments

Comments

@code-tinker
Copy link

const a = Rx.Observable.create().share();
const b = new Rx.Subject();
  • What's the different between a & b ? Hope someone can help me 馃檹
@trxcllnt
Copy link
Member

trxcllnt commented May 17, 2017

A cold Observable is an Observable that re-executes its subscribe handler every time it's subscribed to:

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 multicast, which takes a function that returns a Subject to use when its connected. There are also variants of multicast for convenience (such as publish) that create specific types of Subjects. publish() is a convenience method for multicast(() => new Subject())

In addition to connect(), which subscribes the inner Subject to the source and returns the underlying Subscription, you can call refCount(), which returns an Observable. When the Observable returned by refCount() is subscribed to once, it will automatically call connect() internally, and subsequent subscriptions won't reconnect. When all subscribers unsubscribe, refCount will automatically unsubscribe the inner Subject from the source. share() is a convenience method for source.publish().refCount().

@code-tinker
Copy link
Author

@trxcllnt
Thank you ! It really helps me a lot !馃槃

@RoyiNamir
Copy link

@trxcllnt This is the best simplest answer including the new changes (IMHO) where a subject is in the middle.

@lock
Copy link

lock bot commented Jun 6, 2018

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.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants