Skip to content

Commit a20325c

Browse files
staltzbenlesh
authored andcommitted
fix(bufferToggle): fix disposal of subscriptions when errors occur
Fix bufferToggle operator to appropriately unsubscribe whenever an error happens, either on the source or on the opening or closing observables.
1 parent fe1ba5d commit a20325c

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

src/operator/bufferToggle.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ class BufferToggleSubscriber<T, O> extends Subscriber<T> {
5353
}
5454

5555
_error(err: any) {
56+
const contexts = this.contexts;
57+
while (contexts.length > 0) {
58+
const context = contexts.shift();
59+
context.subscription.unsubscribe();
60+
context.buffer = null;
61+
context.subscription = null;
62+
}
5663
this.contexts = null;
5764
this.destination.error(err);
5865
}
@@ -64,7 +71,9 @@ class BufferToggleSubscriber<T, O> extends Subscriber<T> {
6471
this.destination.next(context.buffer);
6572
context.subscription.unsubscribe();
6673
context.buffer = null;
74+
context.subscription = null;
6775
}
76+
this.contexts = null;
6877
this.destination.complete();
6978
}
7079

@@ -74,16 +83,14 @@ class BufferToggleSubscriber<T, O> extends Subscriber<T> {
7483

7584
let closingNotifier = tryCatch(closingSelector)(value);
7685
if (closingNotifier === errorObject) {
77-
const err = closingNotifier.e;
78-
this.contexts = null;
79-
this.destination.error(err);
86+
this._error(closingNotifier.e);
8087
} else {
8188
let context = {
8289
buffer: [],
8390
subscription: new Subscription()
8491
};
8592
contexts.push(context);
86-
const subscriber = new BufferClosingNotifierSubscriber(this, context);
93+
const subscriber = new BufferToggleClosingsSubscriber(this, context);
8794
const subscription = closingNotifier._subscribe(subscriber);
8895
this.add(context.subscription.add(subscription));
8996
}
@@ -102,39 +109,40 @@ class BufferToggleSubscriber<T, O> extends Subscriber<T> {
102109
}
103110
}
104111

105-
class BufferClosingNotifierSubscriber<T> extends Subscriber<T> {
106-
constructor(private parent: BufferToggleSubscriber<any, T>, private context: { subscription: any, buffer: T[] }) {
112+
class BufferToggleOpeningsSubscriber<T> extends Subscriber<T> {
113+
constructor(private parent: BufferToggleSubscriber<any, T>) {
107114
super(null);
108115
}
109116

110-
_next() {
111-
this.parent.closeBuffer(this.context);
117+
_next(value: T) {
118+
this.parent.openBuffer(value);
112119
}
113120

114121
_error(err) {
115122
this.parent.error(err);
116123
}
117124

118125
_complete() {
119-
this.parent.closeBuffer(this.context);
126+
// noop
120127
}
121128
}
122129

123-
class BufferToggleOpeningsSubscriber<T> extends Subscriber<T> {
124-
constructor(private parent: BufferToggleSubscriber<any, T>) {
130+
class BufferToggleClosingsSubscriber<T> extends Subscriber<T> {
131+
constructor(private parent: BufferToggleSubscriber<any, T>,
132+
private context: { subscription: any, buffer: T[] }) {
125133
super(null);
126134
}
127135

128-
_next(value: T) {
129-
this.parent.openBuffer(value);
136+
_next() {
137+
this.parent.closeBuffer(this.context);
130138
}
131139

132140
_error(err) {
133141
this.parent.error(err);
134142
}
135143

136144
_complete() {
137-
// noop
145+
this.parent.closeBuffer(this.context);
138146
}
139147
}
140148

0 commit comments

Comments
 (0)