Skip to content

Commit

Permalink
fix(windowToggle): fix windowToggle to dispose window Subjects
Browse files Browse the repository at this point in the history
Fix windowToggle() operator to dispose window Subjects when the destination Subscriber is
unsubscribed.
  • Loading branch information
staltz authored and kwonoj committed Dec 9, 2015
1 parent c346230 commit 15ff3f7
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/operator/windowToggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class WindowToggleOperator<T, R, O> implements Operator<T, R> {
private closingSelector: (openValue: O) => Observable<any>) {
}

call(subscriber: Subscriber<T>): Subscriber<T> {
call(subscriber: Subscriber<Observable<T>>): Subscriber<T> {
return new WindowToggleSubscriber<T, O>(
subscriber, this.openings, this.closingSelector
);
Expand All @@ -33,7 +33,7 @@ interface WindowContext<T> {
class WindowToggleSubscriber<T, O> extends Subscriber<T> {
private contexts: Array<WindowContext<T>> = [];

constructor(destination: Subscriber<T>,
constructor(protected destination: Subscriber<Observable<T>>,
private openings: Observable<O>,
private closingSelector: (openValue: O) => Observable<any>) {
super(destination);
Expand Down Expand Up @@ -72,24 +72,29 @@ class WindowToggleSubscriber<T, O> extends Subscriber<T> {
if (closingNotifier === errorObject) {
this.error(closingNotifier.e);
} else {
let context = {
window: new Subject<T>(),
subscription: new Subscription()
};
const destination = this.destination;
const window = new Subject<T>();
const subscription = new Subscription();
const context = { window, subscription };
this.contexts.push(context);
this.destination.next(context.window);
const subscriber = new WindowClosingNotifierSubscriber<T, O>(this, context);
const subscription = closingNotifier._subscribe(subscriber);
this.add(context.subscription.add(subscription));
const closingSubscription = closingNotifier._subscribe(subscriber);
subscription.add(closingSubscription);
destination.add(subscription);
destination.add(window);
destination.next(window);
}
}

closeWindow(context: WindowContext<T>) {
const { window, subscription } = context;
const contexts = this.contexts;
const destination = this.destination;

contexts.splice(contexts.indexOf(context), 1);
window.complete();
this.remove(subscription);
destination.remove(subscription);
destination.remove(window);
subscription.unsubscribe();
}
}
Expand Down

0 comments on commit 15ff3f7

Please sign in to comment.