Skip to content

Commit a1778e0

Browse files
committedJan 16, 2017
fix(SafeSubscriber): SafeSubscriber shouldn't mutate incoming Observers.
2237
1 parent 6a40d7b commit a1778e0

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed
 

‎spec/Observable-spec.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,9 @@ describe('Observable', () => {
476476
' of the anonymous observer', (done: MochaDone) => {
477477
//intentionally not using lambda to avoid typescript's this context capture
478478
const o = {
479+
myValue: 'foo',
479480
next: function next(x) {
480-
expect(this).to.equal(o);
481+
expect(this.myValue).to.equal('foo');
481482
expect(x).to.equal(1);
482483
done();
483484
}
@@ -490,8 +491,9 @@ describe('Observable', () => {
490491
' of the anonymous observer', (done: MochaDone) => {
491492
//intentionally not using lambda to avoid typescript's this context capture
492493
const o = {
494+
myValue: 'foo',
493495
error: function error(err) {
494-
expect(this).to.equal(o);
496+
expect(this.myValue).to.equal('foo');
495497
expect(err).to.equal('bad');
496498
done();
497499
}
@@ -504,8 +506,9 @@ describe('Observable', () => {
504506
' context of the anonymous observer', (done: MochaDone) => {
505507
//intentionally not using lambda to avoid typescript's this context capture
506508
const o = {
509+
myValue: 'foo',
507510
complete: function complete() {
508-
expect(this).to.equal(o);
511+
expect(this.myValue).to.equal('foo');
509512
done();
510513
}
511514
};
@@ -527,8 +530,9 @@ describe('Observable', () => {
527530

528531
//intentionally not using lambda to avoid typescript's this context capture
529532
const o = {
533+
myValue: 'foo',
530534
next: function next(x) {
531-
expect(this).to.equal(o);
535+
expect(this.myValue).to.equal('foo');
532536
throw x;
533537
}
534538
};

‎src/Subscriber.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,16 @@ class SafeSubscriber<T> extends Subscriber<T> {
167167
if (isFunction(observerOrNext)) {
168168
next = (<((value: T) => void)> observerOrNext);
169169
} else if (observerOrNext) {
170-
context = observerOrNext;
171170
next = (<PartialObserver<T>> observerOrNext).next;
172171
error = (<PartialObserver<T>> observerOrNext).error;
173172
complete = (<PartialObserver<T>> observerOrNext).complete;
174-
if (isFunction(context.unsubscribe)) {
175-
this.add(<() => void> context.unsubscribe.bind(context));
173+
if (observerOrNext !== emptyObserver) {
174+
context = Object.create(observerOrNext);
175+
if (isFunction(context.unsubscribe)) {
176+
this.add(<() => void> context.unsubscribe.bind(context));
177+
}
178+
context.unsubscribe = this.unsubscribe.bind(this);
176179
}
177-
context.unsubscribe = this.unsubscribe.bind(this);
178180
}
179181

180182
this._context = context;

0 commit comments

Comments
 (0)
Please sign in to comment.