From a3f4552f461bb8289b315bc3deed2773d2c2956c Mon Sep 17 00:00:00 2001 From: Ben Lesh Date: Thu, 24 Mar 2016 11:33:55 -0700 Subject: [PATCH] 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 --- src/Subject.ts | 4 ++-- src/Subscription.ts | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Subject.ts b/src/Subject.ts index bfa986b5dc..ab5133a43f 100644 --- a/src/Subject.ts +++ b/src/Subject.ts @@ -38,8 +38,8 @@ export class Subject extends Observable implements Observer, ISubscript return subject; } - add(subscription: TeardownLogic): void { - Subscription.prototype.add.call(this, subscription); + add(subscription: TeardownLogic): Subscription { + return Subscription.prototype.add.call(this, subscription); } remove(subscription: Subscription): void { diff --git a/src/Subscription.ts b/src/Subscription.ts index 00eca0d793..1c5c864994 100644 --- a/src/Subscription.ts +++ b/src/Subscription.ts @@ -13,7 +13,7 @@ export type TeardownLogic = AnonymousSubscription | Function | void; export interface ISubscription extends AnonymousSubscription { unsubscribe(): void; isUnsubscribed: boolean; - add(teardown: TeardownLogic): void; + add(teardown: TeardownLogic): ISubscription; remove(sub: ISubscription): void; } @@ -92,8 +92,11 @@ export class Subscription implements ISubscription { * will be executed immediately * * @param {TeardownLogic} teardown the additional logic to execute on teardown. + * @returns {Subscription} returns the subscription used or created to be added to the inner + * subscriptions list. This subscription can be used with `remove()` to remove the passed teardown + * logic from the inner subscriptions list. */ - add(teardown: TeardownLogic): void { + add(teardown: TeardownLogic): Subscription { if (!teardown || ( teardown === this) || ( teardown === Subscription.EMPTY)) { @@ -117,6 +120,8 @@ export class Subscription implements ISubscription { default: throw new Error('Unrecognized teardown ' + teardown + ' added to Subscription.'); } + + return sub; } /**