From 36f90b4bf245571cc5572e1561568f5ada46197d Mon Sep 17 00:00:00 2001 From: Ben Lesh Date: Tue, 27 Apr 2021 15:04:43 -0500 Subject: [PATCH] fix: resolve run-time errors when using deprecated sync error handling Fixes a should-be-obvious issue where we were making sure `dest` was `undefined`, then trying to set a property on it. fixes #6271 --- spec/Observable-spec.ts | 11 +++++++++++ src/internal/Observable.ts | 10 ++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/spec/Observable-spec.ts b/spec/Observable-spec.ts index 6fe9c99761..3863ef1b5d 100644 --- a/spec/Observable-spec.ts +++ b/spec/Observable-spec.ts @@ -799,6 +799,17 @@ describe('Observable', () => { expect(results).to.deep.equal([1, 2]); }); + // https://github.com/ReactiveX/rxjs/issues/6271 + it('should not have a run-time error if no errors are thrown and there are operators', () => { + expect(() => { + of(1, 2, 3).pipe( + map(x => x + x), + map(x => Math.log(x)) + ) + .subscribe(); + }).not.to.throw(); + }); + afterEach(() => { config.useDeprecatedSynchronousErrorHandling = false; }); diff --git a/src/internal/Observable.ts b/src/internal/Observable.ts index 67b1c6a669..c91680eba5 100644 --- a/src/internal/Observable.ts +++ b/src/internal/Observable.ts @@ -241,8 +241,8 @@ export class Observable implements Subscribable { * REMOVE THIS ENTIRE METHOD IN VERSION 8. */ private _deprecatedSyncErrorSubscribe(subscriber: Subscriber) { - let dest: any = subscriber; - dest._syncErrorHack_isSubscribing = true; + const localSubscriber: any = subscriber; + localSubscriber._syncErrorHack_isSubscribing = true; const { operator } = this; if (operator) { // We don't need to try/catch on operators, as they @@ -253,7 +253,7 @@ export class Observable implements Subscribable { try { this._subscribe(subscriber); } catch (err) { - dest.__syncError = err; + localSubscriber.__syncError = err; } } @@ -262,6 +262,7 @@ export class Observable implements Subscribable { // look to see if there's any synchronously thrown errors. // Does this suck for perf? Yes. So stop using the deprecated sync // error handling already. We're removing this in v8. + let dest = localSubscriber; while (dest) { // Technically, someone could throw something falsy, like 0, or "", // so we need to check to see if anything was thrown, and we know @@ -275,7 +276,8 @@ export class Observable implements Subscribable { } dest = dest.destination; } - dest._syncErrorHack_isSubscribing = false; + + localSubscriber._syncErrorHack_isSubscribing = false; } /** @internal */