Skip to content

Commit

Permalink
feat(Subscription): add() now returns a Subscription reference
Browse files Browse the repository at this point in the history
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
  • Loading branch information
benlesh committed Mar 29, 2016
1 parent ec48719 commit a3f4552
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Subject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export class Subject<T> extends Observable<T> implements Observer<T>, ISubscript
return <any>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 {
Expand Down
9 changes: 7 additions & 2 deletions src/Subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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)) {
Expand All @@ -117,6 +120,8 @@ export class Subscription implements ISubscription {
default:
throw new Error('Unrecognized teardown ' + teardown + ' added to Subscription.');
}

return sub;
}

/**
Expand Down

0 comments on commit a3f4552

Please sign in to comment.