Skip to content

Commit

Permalink
feat(Subscription): Subscription now implements Symbol.dispose
Browse files Browse the repository at this point in the history
  • Loading branch information
benlesh committed Jul 12, 2023
1 parent 2947583 commit 653528c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
20 changes: 20 additions & 0 deletions spec/Subscription-spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { expect } from 'chai';
import { Observable, UnsubscriptionError, Subscription, merge } from 'rxjs';
import sinon = require('sinon');

/** @test {Subscription} */
describe('Subscription', () => {
Expand Down Expand Up @@ -225,3 +226,22 @@ describe('Subscription', () => {
});
});
});

describe('Subscription[Symbol.dispose] implementation', () => {
it('should be the same as unsubscribe', () => {
const subscription = new Subscription();
expect(subscription[Symbol.dispose]).to.equal(subscription.unsubscribe);
});

it('should call a teardown when unsubscribed', () => {
// This is just a sanity check.
const callback = sinon.spy();
const subscription = new Subscription();

subscription.add(callback);
subscription[Symbol.dispose]();
expect(callback).to.have.been.calledOnce;
expect(subscription.closed).to.be.true;
});
});

2 changes: 1 addition & 1 deletion spec/helpers/interop-helper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Observable, Operator, Subject, Subscriber, Subscription } from 'rxjs';
import { Observable, Subject, Subscriber, Subscription } from 'rxjs';

/**
* Returns an observable that will be deemed by this package's implementation
Expand Down
6 changes: 6 additions & 0 deletions spec/helpers/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,11 @@ if (!(Symbol as any).observable) {
}
}(global));

// Polyfill Symbol.dispose for testing
if (typeof Symbol.dispose !== 'symbol') {
(Symbol as any).dispose = Symbol('dispose polyfill');
}


//setup sinon-chai
chai.use(sinonChai);
18 changes: 18 additions & 0 deletions src/internal/Subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,28 @@ export class Subscription implements SubscriptionLike {
}
}

// Even though Subscriptoin only conditionally implements `Symbol.dispose`
// if it's available, we still need to declare it here so that TypeScript
// knows that it exists on the prototype when it is available.
export interface Subscription {
[Symbol.dispose](): void;
}

if (typeof Symbol.dispose === 'symbol') {
Subscription.prototype[Symbol.dispose] = Subscription.prototype.unsubscribe;
}

function execFinalizer(finalizer: Unsubscribable | (() => void)) {
if (isFunction(finalizer)) {
finalizer();
} else {
finalizer.unsubscribe();
}
}

// Ensure that `Symbol.dispose` is defined in TypeScript
declare global {
interface SymbolConstructor {
readonly dispose: unique symbol;
}
}

0 comments on commit 653528c

Please sign in to comment.