-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix groupBy in order to pass tests. Also refactor some code related to groupBy, introducing groupBy-support.ts file. Most fixes are related to making inner Observables groups be hot which continue executing even if the outer Observable was unsubscribed. Another fix makes the outer Observable throw an error if the elementSelector function throws. The most significant refactor replaces GroupSubject with GroupedObservable, to resemble the RxJS legacy API, and disallow using Observer methods in the Subject. Relates to issue #375.
- Loading branch information
Showing
6 changed files
with
107 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import Subscription from '../Subscription'; | ||
import Subject from '../Subject'; | ||
import Subscriber from '../Subscriber'; | ||
import Observable from '../Observable'; | ||
|
||
export class RefCountSubscription<T> extends Subscription<T> { | ||
primary: Subscription<T>; | ||
attemptedToUnsubscribePrimary: boolean = false; | ||
count: number = 0; | ||
|
||
constructor() { | ||
super(); | ||
} | ||
|
||
setPrimary(subscription: Subscription<T>) { | ||
this.primary = subscription; | ||
} | ||
|
||
unsubscribe() { | ||
if (!this.isUnsubscribed && !this.attemptedToUnsubscribePrimary) { | ||
this.attemptedToUnsubscribePrimary = true; | ||
if (this.count === 0) { | ||
super.unsubscribe(); | ||
this.primary.unsubscribe(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
export class GroupedObservable<T> extends Observable<T> { | ||
constructor(public key: string, | ||
private groupSubject: Subject<T>, | ||
private refCountSubscription: RefCountSubscription<T>) { | ||
super(); | ||
} | ||
|
||
_subscribe(subscriber: Subscriber<T>) { | ||
const subscription = new Subscription(); | ||
if (!this.refCountSubscription.isUnsubscribed) { | ||
subscription.add(new InnerRefCountSubscription(this.refCountSubscription)); | ||
} | ||
subscription.add(this.groupSubject.subscribe(subscriber)); | ||
return subscription; | ||
} | ||
} | ||
|
||
export class InnerRefCountSubscription<T> extends Subscription<T> { | ||
constructor(private parent: RefCountSubscription<T>) { | ||
super(); | ||
parent.count++; | ||
} | ||
|
||
unsubscribe() { | ||
if (!this.parent.isUnsubscribed && !this.isUnsubscribed) { | ||
super.unsubscribe(); | ||
this.parent.count--; | ||
if (this.parent.count === 0 && this.parent.attemptedToUnsubscribePrimary) { | ||
this.parent.unsubscribe(); | ||
this.parent.primary.unsubscribe(); | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.