11import { isFunction } from './util/isFunction' ;
22import { empty as emptyObserver } from './Observer' ;
3- import { Observer , PartialObserver } from './types' ;
3+ import { Observer , PartialObserver , TeardownLogic } from './types' ;
44import { Subscription } from './Subscription' ;
55import { rxSubscriber as rxSubscriberSymbol } from '../internal/symbol/rxSubscriber' ;
66import { config } from './config' ;
@@ -47,6 +47,8 @@ export class Subscriber<T> extends Subscription implements Observer<T> {
4747 protected isStopped : boolean = false ;
4848 protected destination : PartialObserver < any > ; // this `any` is the escape hatch to erase extra type param (e.g. R)
4949
50+ private _parentSubscription : Subscription | null = null ;
51+
5052 /**
5153 * @param {Observer|function(value: T): void } [destinationOrNext] A partially
5254 * defined Observer or a `next` callback function.
@@ -76,7 +78,7 @@ export class Subscriber<T> extends Subscription implements Observer<T> {
7678 const trustedSubscriber = destinationOrNext [ rxSubscriberSymbol ] ( ) as Subscriber < any > ;
7779 this . syncErrorThrowable = trustedSubscriber . syncErrorThrowable ;
7880 this . destination = trustedSubscriber ;
79- trustedSubscriber . add ( this ) ;
81+ trustedSubscriber . _addParentTeardownLogic ( this ) ;
8082 } else {
8183 this . syncErrorThrowable = true ;
8284 this . destination = new SafeSubscriber < T > ( this , < PartialObserver < any > > destinationOrNext ) ;
@@ -114,6 +116,7 @@ export class Subscriber<T> extends Subscription implements Observer<T> {
114116 if ( ! this . isStopped ) {
115117 this . isStopped = true ;
116118 this . _error ( err ) ;
119+ this . _unsubscribeParentSubscription ( ) ;
117120 }
118121 }
119122
@@ -127,6 +130,7 @@ export class Subscriber<T> extends Subscription implements Observer<T> {
127130 if ( ! this . isStopped ) {
128131 this . isStopped = true ;
129132 this . _complete ( ) ;
133+ this . _unsubscribeParentSubscription ( ) ;
130134 }
131135 }
132136
@@ -152,6 +156,18 @@ export class Subscriber<T> extends Subscription implements Observer<T> {
152156 this . unsubscribe ( ) ;
153157 }
154158
159+ /** @deprecated This is an internal implementation detail, do not use. */
160+ _addParentTeardownLogic ( parentTeardownLogic : TeardownLogic ) {
161+ this . _parentSubscription = this . add ( parentTeardownLogic ) ;
162+ }
163+
164+ /** @deprecated This is an internal implementation detail, do not use. */
165+ _unsubscribeParentSubscription ( ) {
166+ if ( this . _parentSubscription !== null ) {
167+ this . _parentSubscription . unsubscribe ( ) ;
168+ }
169+ }
170+
155171 /** @deprecated This is an internal implementation detail, do not use. */
156172 _unsubscribeAndRecycle ( ) : Subscriber < T > {
157173 const { _parent, _parents } = this ;
@@ -162,6 +178,7 @@ export class Subscriber<T> extends Subscription implements Observer<T> {
162178 this . isStopped = false ;
163179 this . _parent = _parent ;
164180 this . _parents = _parents ;
181+ this . _parentSubscription = null ;
165182 return this ;
166183 }
167184}
0 commit comments