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
This commit is contained in:
JiaLiPassion
2020-03-10 21:35:58 +09:00
committed by Andrew Kushnir
parent 54634628ac
commit 3fa895298d
3 changed files with 87 additions and 21 deletions

View File

@ -50,22 +50,28 @@ type ZoneSubscriberContext = {
},
set: function(this: Observable<any>, subscribe: any) {
(this as any)._zone = Zone.current;
(this as any)._zoneSubscribe = function(this: ZoneSubscriberContext) {
if (this._zone && this._zone !== Zone.current) {
const tearDown = this._zone.run(subscribe, this, arguments as any);
if (tearDown && typeof tearDown === 'function') {
const zone = this._zone;
return function(this: ZoneSubscriberContext) {
if (zone !== Zone.current) {
return zone.run(tearDown, this, arguments as any);
}
return tearDown.apply(this, arguments);
};
if (!subscribe) {
(this as any)._zoneSubscribe = subscribe;
} else {
(this as any)._zoneSubscribe = function(this: ZoneSubscriberContext) {
if (this._zone && this._zone !== Zone.current) {
const tearDown = this._zone.run(subscribe, this, arguments as any);
if (typeof tearDown === 'function') {
const zone = this._zone;
return function(this: ZoneSubscriberContext) {
if (zone !== Zone.current) {
return zone.run(tearDown, this, arguments as any);
}
return tearDown.apply(this, arguments);
};
} else {
return tearDown;
}
} else {
return subscribe.apply(this, arguments);
}
return tearDown;
}
return subscribe.apply(this, arguments);
};
};
}
}
},
subjectFactory: {
@ -113,12 +119,17 @@ type ZoneSubscriberContext = {
},
set: function(this: Subscription, unsubscribe: any) {
(this as any)._zone = Zone.current;
(this as any)._zoneUnsubscribe = function() {
if (this._zone && this._zone !== Zone.current) {
return this._zone.run(unsubscribe, this, arguments);
}
return unsubscribe.apply(this, arguments);
};
if (!unsubscribe) {
(this as any)._zoneUnsubscribe = unsubscribe;
} else {
(this as any)._zoneUnsubscribe = function() {
if (this._zone && this._zone !== Zone.current) {
return this._zone.run(unsubscribe, this, arguments);
} else {
return unsubscribe.apply(this, arguments);
}
};
}
}
}
});