Skip to content

Commit ec48719

Browse files
committed
refactor(Subscription): update typings for subscriptions
1 parent f8d7d1b commit ec48719

File tree

2 files changed

+43
-20
lines changed

2 files changed

+43
-20
lines changed

src/Subject.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {Operator} from './Operator';
22
import {Observer} from './Observer';
33
import {Observable} from './Observable';
44
import {Subscriber} from './Subscriber';
5-
import {Subscription} from './Subscription';
5+
import {Subscription, ISubscription, TeardownLogic} from './Subscription';
66
import {SubjectSubscription} from './subject/SubjectSubscription';
77
import {$$rxSubscriber} from './symbol/rxSubscriber';
88

@@ -12,7 +12,7 @@ import {ObjectUnsubscribedError} from './util/ObjectUnsubscribedError';
1212
/**
1313
* @class Subject<T>
1414
*/
15-
export class Subject<T> extends Observable<T> implements Observer<T>, Subscription {
15+
export class Subject<T> extends Observable<T> implements Observer<T>, ISubscription {
1616

1717
static create: Function = <T>(destination: Observer<T>, source: Observable<T>): Subject<T> => {
1818
return new Subject<T>(destination, source);
@@ -38,7 +38,7 @@ export class Subject<T> extends Observable<T> implements Observer<T>, Subscripti
3838
return <any>subject;
3939
}
4040

41-
add(subscription: Subscription|Function|void): void {
41+
add(subscription: TeardownLogic): void {
4242
Subscription.prototype.add.call(this, subscription);
4343
}
4444

src/Subscription.ts

+40-17
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,20 @@ import {isFunction} from './util/isFunction';
44
import {tryCatch} from './util/tryCatch';
55
import {errorObject} from './util/errorObject';
66

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 {
821
public static EMPTY: Subscription = (function(empty: any){
922
empty.isUnsubscribed = true;
1023
return empty;
@@ -68,22 +81,30 @@ export class Subscription {
6881
}
6982
}
7083

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)) {
79100
return;
80101
}
81102

82-
let sub = (<Subscription> subscription);
103+
let sub = (<Subscription> teardown);
83104

84-
switch (typeof subscription) {
105+
switch (typeof teardown) {
85106
case 'function':
86-
sub = new Subscription(<(() => void) > subscription);
107+
sub = new Subscription(<(() => void) > teardown);
87108
case 'object':
88109
if (sub.isUnsubscribed || typeof sub.unsubscribe !== 'function') {
89110
break;
@@ -94,16 +115,18 @@ export class Subscription {
94115
}
95116
break;
96117
default:
97-
throw new Error('Unrecognized subscription ' + subscription + ' added to Subscription.');
118+
throw new Error('Unrecognized teardown ' + teardown + ' added to Subscription.');
98119
}
99120
}
100121

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+
*/
101127
remove(subscription: Subscription): void {
102128

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()`
107130
if (subscription == null || (
108131
subscription === this) || (
109132
subscription === Subscription.EMPTY)) {

0 commit comments

Comments
 (0)