Skip to content

Commit

Permalink
fix: resolve run-time errors when using deprecated sync error handling (
Browse files Browse the repository at this point in the history
#6272)

Fixes a should-be-obvious issue where we were making sure `dest` was `undefined`, then trying to set a property on it.

fixes #6271
  • Loading branch information
benlesh committed Apr 27, 2021
1 parent 7be67fa commit 35daaf7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
11 changes: 11 additions & 0 deletions spec/Observable-spec.ts
Expand Up @@ -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;
});
Expand Down
10 changes: 6 additions & 4 deletions src/internal/Observable.ts
Expand Up @@ -241,8 +241,8 @@ export class Observable<T> implements Subscribable<T> {
* REMOVE THIS ENTIRE METHOD IN VERSION 8.
*/
private _deprecatedSyncErrorSubscribe(subscriber: Subscriber<unknown>) {
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
Expand All @@ -253,7 +253,7 @@ export class Observable<T> implements Subscribable<T> {
try {
this._subscribe(subscriber);
} catch (err) {
dest.__syncError = err;
localSubscriber.__syncError = err;
}
}

Expand All @@ -262,6 +262,7 @@ export class Observable<T> implements Subscribable<T> {
// 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
Expand All @@ -275,7 +276,8 @@ export class Observable<T> implements Subscribable<T> {
}
dest = dest.destination;
}
dest._syncErrorHack_isSubscribing = false;

localSubscriber._syncErrorHack_isSubscribing = false;
}

/** @internal */
Expand Down

0 comments on commit 35daaf7

Please sign in to comment.