@@ -164,14 +164,15 @@ export class Subscription implements SubscriptionLike {
164
164
}
165
165
}
166
166
167
- // Optimize for the common case when adding the first subscription.
168
- const subscriptions = this . _subscriptions ;
169
- if ( subscriptions ) {
170
- subscriptions . push ( subscription ) ;
171
- } else {
172
- this . _subscriptions = [ subscription ] ;
167
+ if ( subscription . _addParent ( this ) ) {
168
+ // Optimize for the common case when adding the first subscription.
169
+ const subscriptions = this . _subscriptions ;
170
+ if ( subscriptions ) {
171
+ subscriptions . push ( subscription ) ;
172
+ } else {
173
+ this . _subscriptions = [ subscription ] ;
174
+ }
173
175
}
174
- subscription . _addParent ( this ) ;
175
176
176
177
return subscription ;
177
178
}
@@ -193,20 +194,26 @@ export class Subscription implements SubscriptionLike {
193
194
}
194
195
195
196
/** @internal */
196
- private _addParent ( parent : Subscription ) {
197
+ private _addParent ( parent : Subscription ) : boolean {
197
198
let { _parent, _parents } = this ;
198
- if ( ! _parent || _parent === parent ) {
199
- // If we don't have a parent, or the new parent is the same as the
200
- // current parent, then set this._parent to the new parent.
199
+ if ( _parent === parent ) {
200
+ // If the new parent is the same as the current parent, then do nothing.
201
+ return false ;
202
+ } else if ( ! _parent ) {
203
+ // If we don't have a parent, then set this._parent to the new parent.
201
204
this . _parent = parent ;
205
+ return true ;
202
206
} else if ( ! _parents ) {
203
207
// If there's already one parent, but not multiple, allocate an Array to
204
208
// store the rest of the parent Subscriptions.
205
209
this . _parents = [ parent ] ;
210
+ return true ;
206
211
} else if ( _parents . indexOf ( parent ) === - 1 ) {
207
212
// Only add the new parent to the _parents list if it's not already there.
208
213
_parents . push ( parent ) ;
214
+ return true ;
209
215
}
216
+ return false ;
210
217
}
211
218
}
212
219
0 commit comments