Skip to content

Commit

Permalink
fix(SafeSubscriber): SafeSubscriber shouldn't mutate incoming Observers.
Browse files Browse the repository at this point in the history
2237
  • Loading branch information
trxcllnt committed Jan 16, 2017
1 parent 6a40d7b commit a1778e0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
12 changes: 8 additions & 4 deletions spec/Observable-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,9 @@ describe('Observable', () => {
' of the anonymous observer', (done: MochaDone) => {
//intentionally not using lambda to avoid typescript's this context capture
const o = {
myValue: 'foo',
next: function next(x) {
expect(this).to.equal(o);
expect(this.myValue).to.equal('foo');
expect(x).to.equal(1);
done();
}
Expand All @@ -490,8 +491,9 @@ describe('Observable', () => {
' of the anonymous observer', (done: MochaDone) => {
//intentionally not using lambda to avoid typescript's this context capture
const o = {
myValue: 'foo',
error: function error(err) {
expect(this).to.equal(o);
expect(this.myValue).to.equal('foo');
expect(err).to.equal('bad');
done();
}
Expand All @@ -504,8 +506,9 @@ describe('Observable', () => {
' context of the anonymous observer', (done: MochaDone) => {
//intentionally not using lambda to avoid typescript's this context capture
const o = {
myValue: 'foo',
complete: function complete() {
expect(this).to.equal(o);
expect(this.myValue).to.equal('foo');
done();
}
};
Expand All @@ -527,8 +530,9 @@ describe('Observable', () => {

//intentionally not using lambda to avoid typescript's this context capture
const o = {
myValue: 'foo',
next: function next(x) {
expect(this).to.equal(o);
expect(this.myValue).to.equal('foo');
throw x;
}
};
Expand Down
10 changes: 6 additions & 4 deletions src/Subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,16 @@ class SafeSubscriber<T> extends Subscriber<T> {
if (isFunction(observerOrNext)) {
next = (<((value: T) => void)> observerOrNext);
} else if (observerOrNext) {
context = observerOrNext;
next = (<PartialObserver<T>> observerOrNext).next;
error = (<PartialObserver<T>> observerOrNext).error;
complete = (<PartialObserver<T>> observerOrNext).complete;
if (isFunction(context.unsubscribe)) {
this.add(<() => void> context.unsubscribe.bind(context));
if (observerOrNext !== emptyObserver) {
context = Object.create(observerOrNext);
if (isFunction(context.unsubscribe)) {
this.add(<() => void> context.unsubscribe.bind(context));
}
context.unsubscribe = this.unsubscribe.bind(this);
}
context.unsubscribe = this.unsubscribe.bind(this);
}

this._context = context;
Expand Down

0 comments on commit a1778e0

Please sign in to comment.