fix(zone.js): remove unused Promise overwritten setter logic (#36851)
In the early Zone.js versions (< 0.10.3), `ZoneAwarePromise` did not support `Symbol.species`, so when user used a 3rd party `Promise` such as `es6-promise`, and try to load the promise library after import of `zone.js`, the loading promise library will overwrite the patched `Promise` from `zone.js` and will break `Promise` semantics with respect to `zone.js`. Starting with `zone.js` 0.10.3, `Symbol.species` is supported therefore this will not longer be an issue. (https://github.com//pull/34533) Before 0.10.3, the logic in zone.js tried to handle the case in the wrong way. It did so by overriding the descriptor of `global.Promise`, to allow the 3rd party libraries to override native `Promise` instead of `ZoneAwarePromise`. This is not the correct solution, and since the `Promise.species` is now supported, the 3rd party solution of overriding `global.Promise` is no longer needed. PR removes the wrong work around logic. (This will improve the bundle size.) PR Close #36851
This commit is contained in:

committed by
Misko Hevery

parent
9b8eb42354
commit
c909e731d7
@ -459,44 +459,6 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
|
||||
ZoneAwarePromise['all'] = ZoneAwarePromise.all;
|
||||
|
||||
const NativePromise = global[symbolPromise] = global['Promise'];
|
||||
const ZONE_AWARE_PROMISE = Zone.__symbol__('ZoneAwarePromise');
|
||||
|
||||
let desc = ObjectGetOwnPropertyDescriptor(global, 'Promise');
|
||||
if (!desc || desc.configurable) {
|
||||
desc && delete desc.writable;
|
||||
desc && delete desc.value;
|
||||
if (!desc) {
|
||||
desc = {configurable: true, enumerable: true};
|
||||
}
|
||||
desc.get = function() {
|
||||
// if we already set ZoneAwarePromise, use patched one
|
||||
// otherwise return native one.
|
||||
return global[ZONE_AWARE_PROMISE] ? global[ZONE_AWARE_PROMISE] : global[symbolPromise];
|
||||
};
|
||||
desc.set = function(NewNativePromise) {
|
||||
if (NewNativePromise === ZoneAwarePromise) {
|
||||
// if the NewNativePromise is ZoneAwarePromise
|
||||
// save to global
|
||||
global[ZONE_AWARE_PROMISE] = NewNativePromise;
|
||||
} else {
|
||||
// if the NewNativePromise is not ZoneAwarePromise
|
||||
// for example: after load zone.js, some library just
|
||||
// set es6-promise to global, if we set it to global
|
||||
// directly, assertZonePatched will fail and angular
|
||||
// will not loaded, so we just set the NewNativePromise
|
||||
// to global[symbolPromise], so the result is just like
|
||||
// we load ES6 Promise before zone.js
|
||||
global[symbolPromise] = NewNativePromise;
|
||||
if (!NewNativePromise.prototype[symbolThen]) {
|
||||
patchThen(NewNativePromise);
|
||||
}
|
||||
api.setNativePromise(NewNativePromise);
|
||||
}
|
||||
};
|
||||
|
||||
ObjectDefineProperty(global, 'Promise', desc);
|
||||
}
|
||||
|
||||
global['Promise'] = ZoneAwarePromise;
|
||||
|
||||
const symbolThenPatched = __symbol__('thenPatched');
|
||||
|
Reference in New Issue
Block a user