Skip to content

Commit 3fa8952

Browse files
JiaLiPassionAndrewKushnir
authored andcommitted
fix(zone.js): zone.js patches rxjs should check null for unsubscribe (#35990)
Close #31687, #31684 Zone.js patches rxjs internal `_subscribe` and `_unsubscribe` methods, but zone.js doesn't do null check, so in some operator such as `retryWhen`, the `_unsubscribe` will be set to null, and will cause zone patched version throw error. In this PR, if `_subscribe` and `_unsubscribe` is null, will not do the patch. PR Close #35990
1 parent 5463462 commit 3fa8952

3 files changed

Lines changed: 87 additions & 21 deletions

File tree

packages/zone.js/lib/rxjs/rxjs.ts

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,28 @@ type ZoneSubscriberContext = {
5050
},
5151
set: function(this: Observable<any>, subscribe: any) {
5252
(this as any)._zone = Zone.current;
53-
(this as any)._zoneSubscribe = function(this: ZoneSubscriberContext) {
54-
if (this._zone && this._zone !== Zone.current) {
55-
const tearDown = this._zone.run(subscribe, this, arguments as any);
56-
if (tearDown && typeof tearDown === 'function') {
57-
const zone = this._zone;
58-
return function(this: ZoneSubscriberContext) {
59-
if (zone !== Zone.current) {
60-
return zone.run(tearDown, this, arguments as any);
61-
}
62-
return tearDown.apply(this, arguments);
63-
};
53+
if (!subscribe) {
54+
(this as any)._zoneSubscribe = subscribe;
55+
} else {
56+
(this as any)._zoneSubscribe = function(this: ZoneSubscriberContext) {
57+
if (this._zone && this._zone !== Zone.current) {
58+
const tearDown = this._zone.run(subscribe, this, arguments as any);
59+
if (typeof tearDown === 'function') {
60+
const zone = this._zone;
61+
return function(this: ZoneSubscriberContext) {
62+
if (zone !== Zone.current) {
63+
return zone.run(tearDown, this, arguments as any);
64+
}
65+
return tearDown.apply(this, arguments);
66+
};
67+
} else {
68+
return tearDown;
69+
}
70+
} else {
71+
return subscribe.apply(this, arguments);
6472
}
65-
return tearDown;
66-
}
67-
return subscribe.apply(this, arguments);
68-
};
73+
};
74+
}
6975
}
7076
},
7177
subjectFactory: {
@@ -113,12 +119,17 @@ type ZoneSubscriberContext = {
113119
},
114120
set: function(this: Subscription, unsubscribe: any) {
115121
(this as any)._zone = Zone.current;
116-
(this as any)._zoneUnsubscribe = function() {
117-
if (this._zone && this._zone !== Zone.current) {
118-
return this._zone.run(unsubscribe, this, arguments);
119-
}
120-
return unsubscribe.apply(this, arguments);
121-
};
122+
if (!unsubscribe) {
123+
(this as any)._zoneUnsubscribe = unsubscribe;
124+
} else {
125+
(this as any)._zoneUnsubscribe = function() {
126+
if (this._zone && this._zone !== Zone.current) {
127+
return this._zone.run(unsubscribe, this, arguments);
128+
} else {
129+
return unsubscribe.apply(this, arguments);
130+
}
131+
};
132+
}
122133
}
123134
}
124135
});
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import {Observable, of , timer} from 'rxjs';
9+
import {delayWhen, map, retryWhen} from 'rxjs/operators';
10+
11+
describe('Observable.retryWhen', () => {
12+
let log: any[];
13+
let observable1: Observable<any>;
14+
let defaultTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
15+
16+
beforeEach(() => {
17+
log = [];
18+
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
19+
});
20+
21+
afterEach(() => { jasmine.DEFAULT_TIMEOUT_INTERVAL = defaultTimeout; });
22+
23+
it('retryWhen func callback should run in the correct zone', (done: DoneFn) => {
24+
const constructorZone1: Zone = Zone.current.fork({name: 'Constructor Zone'});
25+
const subscriptionZone: Zone = Zone.current.fork({name: 'Subscription Zone'});
26+
let isErrorHandled = false;
27+
observable1 = constructorZone1.run(() => {
28+
return of (1, 2, 3).pipe(
29+
map(v => {
30+
if (v > 2 && !isErrorHandled) {
31+
isErrorHandled = true;
32+
throw v;
33+
}
34+
return v;
35+
}),
36+
retryWhen(err => err.pipe(delayWhen(v => timer(v)))));
37+
});
38+
39+
subscriptionZone.run(() => {
40+
observable1.subscribe(
41+
(result: any) => {
42+
log.push(result);
43+
expect(Zone.current.name).toEqual(subscriptionZone.name);
44+
},
45+
(err: any) => { fail('should not call error'); },
46+
() => {
47+
log.push('completed');
48+
expect(Zone.current.name).toEqual(subscriptionZone.name);
49+
expect(log).toEqual([1, 2, 1, 2, 3, 'completed']);
50+
done();
51+
});
52+
});
53+
});
54+
});

packages/zone.js/test/rxjs/rxjs.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,6 @@ import './rxjs.Observable.map.spec';
5050
import './rxjs.Observable.race.spec';
5151
import './rxjs.Observable.sample.spec';
5252
import './rxjs.Observable.take.spec';
53+
import './rxjs.Observable.retry.spec';
5354
import './rxjs.Observable.timeout.spec';
5455
import './rxjs.Observable.window.spec';

0 commit comments

Comments
 (0)