-
Notifications
You must be signed in to change notification settings - Fork 3k
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(Observable): Subjects no longer wrapped in Subscriber #863
Conversation
This PR addresses the issue where Subjects are unnecessarily wrapped in a Subscriber during Observable.subscribe. Conceptually there is no reason they should be since they are designed to be both Observable and Subscriber. This fix does introduce a circular dependency between Subject and Observable that must be managed during initial import in Rx and KitchenSink. Is introducing module load ordering dependencies okay? (Also after checkin I noticed I forgot to remove a commented out line of code in Observable.ts) |
LGTM. |
@ntilwalli can you rephrase this commit as The reason for the change from Other than that, LGTM! Thanks for the PR! |
@@ -89,7 +89,8 @@ export class Observable<T> implements CoreOperators<T> { | |||
let subscriber: Subscriber<T>; | |||
|
|||
if (observerOrNext && typeof observerOrNext === 'object') { | |||
if (observerOrNext instanceof Subscriber) { | |||
//if (observerOrNext instanceof Subscriber) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would you able to remove comment if this is leftover?
Except one minor comment about commented code, change looks good to me too. |
Great. Done. |
@ntilwalli , would you able to flat 2 commit 42258b2 & 849d5b2 into single one? both has identical commit message, and change seems quite similar too. |
I originally said the same. But after looking at code, I noticed that Subjects are not Subscribers. A Subject is an Observable and an Observer. And a Subscriber is an Observer. Every Observer is wrapped by a Subscriber when we subscribe with an observer. That's why a Subject is wrapped by a Subscriber. Look at this inheritance diagram: Yet this PR introduces: if (observerOrNext instanceof Subscriber || observerOrNext instanceof Subject) {
subscriber = (<Subscriber<T>> observerOrNext);
} Does TypeScript allow us to (unsafe?) cast |
: Since typescript only does type inference on build time, above code snippet could get passed.
if you're trying to cast Anyway, you're inheritance diagram seems legit, |
Removed code snippet that I was thinking incorrectly. |
: and seems this is not occurring since execution will reach |
Is reimplementing Subject as extending both Observable and Subscriber too much work? Since Subject implements both Observer and Subscription anyway. |
I'm curious if subject need to extend subscriber. Nature of subject requires to be Observable and Observer as minimum - is there any specific usecases subscriber would benefit subject? thinking about inheritance & implementation hierarchy, extending subscriber will makes subject
makes subject quite complex object. |
I know, so maybe it's ok to keep it wrapped in a Subscriber. |
@staltz said:
What does reimplementing mean?
As long as the implementation of Subject adheres to the stronger contract of statement 2, the cast is safe and there would be no need to reimplement anything in Subject, except to possibly update Subject to statement 2 for added clarity. The discussion though seems to imply that the contract of Subject should be weakened to be:
If that's the case, this PR is not valid and there should be a PR to modify Subject accordingly. When I first proposed this issue, I assumed Subject was coded correctly and Observable was inconsistent. It sounds like you're saying it's the other way around. |
I edited the above comment to fix a reasoning error and to be more precise as to my intended meaning. |
It will be fine. Are we tricking TypeScript? Yes. But we're doing it because TypeScript types aren't a real thing, they're helpful for build-time and development-time sanity checks. When you see This PR should be a small performance improvement when using Subjects to subscribe, and I don't see anything about it that could be bad. ... Other than people who really like typing freaking out a little because we're being dirty, rotten liars about Subject being a Subscriber on that one line. |
@ntilwalli Thanks! I'll check in this after waiting couple of more hrs to see if there's any second opinion. |
Fair enough... |
Merged with 5cb0f2b, thanks @ntilwalli ! |
Whew, sorry for that. I should've keep track detail before merge.. I think current branch is protected from force update, would like to hear @Blesh which would be best. |
Observable.subscribe updated to check for instanceof Subject, test added, import order changed in Rx and KitchenSink to avoid circular dependency issue. closes #825, closes #748