Skip to content

Commit a3f4552

Browse files
committed
feat(Subscription): add() now returns a Subscription reference
Add returns a Subscription reference that can be used with `remove()` to remove the passed teardown logic from the internal subscriptions list that is processed during unsubscription
1 parent ec48719 commit a3f4552

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/Subject.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ export class Subject<T> extends Observable<T> implements Observer<T>, ISubscript
3838
return <any>subject;
3939
}
4040

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

4545
remove(subscription: Subscription): void {

src/Subscription.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export type TeardownLogic = AnonymousSubscription | Function | void;
1313
export interface ISubscription extends AnonymousSubscription {
1414
unsubscribe(): void;
1515
isUnsubscribed: boolean;
16-
add(teardown: TeardownLogic): void;
16+
add(teardown: TeardownLogic): ISubscription;
1717
remove(sub: ISubscription): void;
1818
}
1919

@@ -92,8 +92,11 @@ export class Subscription implements ISubscription {
9292
* will be executed immediately
9393
*
9494
* @param {TeardownLogic} teardown the additional logic to execute on teardown.
95+
* @returns {Subscription} returns the subscription used or created to be added to the inner
96+
* subscriptions list. This subscription can be used with `remove()` to remove the passed teardown
97+
* logic from the inner subscriptions list.
9598
*/
96-
add(teardown: TeardownLogic): void {
99+
add(teardown: TeardownLogic): Subscription {
97100
if (!teardown || (
98101
teardown === this) || (
99102
teardown === Subscription.EMPTY)) {
@@ -117,6 +120,8 @@ export class Subscription implements ISubscription {
117120
default:
118121
throw new Error('Unrecognized teardown ' + teardown + ' added to Subscription.');
119122
}
123+
124+
return sub;
120125
}
121126

122127
/**

0 commit comments

Comments
 (0)