Skip to content

Commit

Permalink
refactor(reportError): use a symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant committed Mar 25, 2018
1 parent f9c5a98 commit 1d926b5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
15 changes: 8 additions & 7 deletions spec/Subscriber-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from 'chai';
import * as sinon from 'sinon';
import * as Rx from 'rxjs/Rx';
import { Symbol_reportError } from '../src/internal/Subscriber';
import { noop } from '../src/internal/util/noop';

const Subscriber = Rx.Subscriber;
Expand Down Expand Up @@ -89,12 +90,12 @@ describe('Subscriber', () => {
expect(argument).to.have.lengthOf(0);
});

describe('_reportError', () => {
describe('reportError', () => {
it('should call error for an unstopped subscriber', () => {
const reported = new Error('Kaboom!');
let received: any;
const subscriber = new Rx.Subscriber(undefined, err => received = err);
(subscriber as any)._reportError(reported);
subscriber[Symbol_reportError](reported);
expect(received).to.equal(reported);
expect(subscriber).to.have.property('isStopped', true);
});
Expand All @@ -105,7 +106,7 @@ describe('Subscriber', () => {
const destination = new Rx.Subscriber(undefined, err => received = err);
const subscriber = new Rx.Subscriber(destination);
const spy = sinon.spy(subscriber, 'error');
(subscriber as any)._reportError(reported);
subscriber[Symbol_reportError](reported);
expect(received).to.equal(reported);
expect(subscriber).to.have.property('isStopped', true);
expect(destination).to.have.property('isStopped', true);
Expand All @@ -123,7 +124,7 @@ describe('Subscriber', () => {
const reported = new Error('Kaboom!');
const subscriber = new Rx.Subscriber(undefined, err => { throw new Error('should not be called'); });
subscriber.complete();
(subscriber as any)._reportError(reported);
subscriber[Symbol_reportError](reported);
expect(stubSetTimeout).to.have.property('callCount', 1);
const [[func]] = stubSetTimeout.args;
expect(func).to.throw('Kaboom!');
Expand All @@ -135,7 +136,7 @@ describe('Subscriber', () => {
const destination = new Rx.Subscriber(undefined, err => { throw new Error('should not be called'); });
const subscriber = new Rx.Subscriber(destination);
destination.complete();
(subscriber as any)._reportError(reported);
subscriber[Symbol_reportError](reported);
expect(stubSetTimeout).to.have.property('callCount', 1);
const [[func]] = stubSetTimeout.args;
expect(func).to.throw('Kaboom!');
Expand All @@ -160,7 +161,7 @@ describe('Subscriber', () => {
const reported = new Error('Kaboom!');
const subscriber = new Rx.Subscriber(undefined, err => { throw new Error('should not be called'); });
subscriber.complete();
expect(() => (subscriber as any)._reportError(reported)).to.throw('Kaboom!');
expect(() => subscriber[Symbol_reportError](reported)).to.throw('Kaboom!');
expect(subscriber).to.have.property('isStopped', true);
});

Expand All @@ -169,7 +170,7 @@ describe('Subscriber', () => {
const destination = new Rx.Subscriber(undefined, err => { throw new Error('should not be called'); });
const subscriber = new Rx.Subscriber(destination);
destination.complete();
expect(() => (subscriber as any)._reportError(reported)).to.throw('Kaboom!');
expect(() => subscriber[Symbol_reportError](reported)).to.throw('Kaboom!');
expect(subscriber).to.have.property('isStopped', true);
expect(destination).to.have.property('isStopped', true);
});
Expand Down
4 changes: 2 additions & 2 deletions src/internal/Observable.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Operator } from './Operator';
import { Subscriber } from './Subscriber';
import { Subscriber, Symbol_reportError } from './Subscriber';
import { Subscription } from './Subscription';
import { TeardownLogic } from './types';
import { toSubscriber } from './util/toSubscriber';
Expand Down Expand Up @@ -215,7 +215,7 @@ export class Observable<T> implements Subscribable<T> {
sink.syncErrorThrown = true;
sink.syncErrorValue = err;
}
(sink as any)._reportError(err);
sink[Symbol_reportError](err);
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/internal/Subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import { rxSubscriber as rxSubscriberSymbol } from '../internal/symbol/rxSubscri
import { config } from './config';
import { hostReportError } from './util/hostReportError';

export const Symbol_reportError =
(typeof Symbol === 'function' && typeof Symbol.for === 'function')
? Symbol.for('reportError')
: '@@reportError';

/**
* Implements the {@link Observer} interface and extends the
* {@link Subscription} class. While the {@link Observer} is the public API for
Expand Down Expand Up @@ -170,7 +175,7 @@ export class Subscriber<T> extends Subscription implements Observer<T> {
* subscribers and uses the appropriate unhandled mechanism to report the
* error if a stopped subscriber is found.
*/
private _reportError(err?: any): void {
[Symbol_reportError](err?: any): void {
let observer: PartialObserver<T> = this;
while (observer) {
if (observer instanceof Subscriber) {
Expand Down

0 comments on commit 1d926b5

Please sign in to comment.