@@ -4,7 +4,20 @@ import {isFunction} from './util/isFunction';
4
4
import { tryCatch } from './util/tryCatch' ;
5
5
import { errorObject } from './util/errorObject' ;
6
6
7
- export class Subscription {
7
+ export interface AnonymousSubscription {
8
+ unsubscribe ( ) : void ;
9
+ }
10
+
11
+ export type TeardownLogic = AnonymousSubscription | Function | void ;
12
+
13
+ export interface ISubscription extends AnonymousSubscription {
14
+ unsubscribe ( ) : void ;
15
+ isUnsubscribed : boolean ;
16
+ add ( teardown : TeardownLogic ) : void ;
17
+ remove ( sub : ISubscription ) : void ;
18
+ }
19
+
20
+ export class Subscription implements ISubscription {
8
21
public static EMPTY : Subscription = ( function ( empty : any ) {
9
22
empty . isUnsubscribed = true ;
10
23
return empty ;
@@ -68,22 +81,30 @@ export class Subscription {
68
81
}
69
82
}
70
83
71
- add ( subscription : Subscription | Function | void ) : void {
72
- // return early if:
73
- // 1. the subscription is null
74
- // 2. we're attempting to add our this
75
- // 3. we're attempting to add the static `empty` Subscription
76
- if ( ! subscription || (
77
- subscription === this ) || (
78
- subscription === Subscription . EMPTY ) ) {
84
+ /**
85
+ * Adds a tear down to be called during the unsubscribe() of this subscription.
86
+ *
87
+ * If the tear down being added is a subscription that is already unsubscribed,
88
+ * is the same reference `add` is being called on, or is `Subscription.EMPTY`,
89
+ * it will not be added.
90
+ *
91
+ * If this subscription is already in an `isUnsubscribed` state, the passed tear down logic
92
+ * will be executed immediately
93
+ *
94
+ * @param {TeardownLogic } teardown the additional logic to execute on teardown.
95
+ */
96
+ add ( teardown : TeardownLogic ) : void {
97
+ if ( ! teardown || (
98
+ teardown === this ) || (
99
+ teardown === Subscription . EMPTY ) ) {
79
100
return ;
80
101
}
81
102
82
- let sub = ( < Subscription > subscription ) ;
103
+ let sub = ( < Subscription > teardown ) ;
83
104
84
- switch ( typeof subscription ) {
105
+ switch ( typeof teardown ) {
85
106
case 'function' :
86
- sub = new Subscription ( < ( ( ) => void ) > subscription ) ;
107
+ sub = new Subscription ( < ( ( ) => void ) > teardown ) ;
87
108
case 'object' :
88
109
if ( sub . isUnsubscribed || typeof sub . unsubscribe !== 'function' ) {
89
110
break ;
@@ -94,16 +115,18 @@ export class Subscription {
94
115
}
95
116
break ;
96
117
default :
97
- throw new Error ( 'Unrecognized subscription ' + subscription + ' added to Subscription.' ) ;
118
+ throw new Error ( 'Unrecognized teardown ' + teardown + ' added to Subscription.' ) ;
98
119
}
99
120
}
100
121
122
+ /**
123
+ * removes a subscription from the internal list of subscriptions that will unsubscribe
124
+ * during unsubscribe process of this subscription.
125
+ * @param {Subscription } subscription the subscription to remove
126
+ */
101
127
remove ( subscription : Subscription ) : void {
102
128
103
- // return early if:
104
- // 1. the subscription is null
105
- // 2. we're attempting to remove ourthis
106
- // 3. we're attempting to remove the static `empty` Subscription
129
+ // HACK: This might be redundant because of the logic in `add()`
107
130
if ( subscription == null || (
108
131
subscription === this ) || (
109
132
subscription === Subscription . EMPTY ) ) {
0 commit comments