Skip to content

Commit 31796e8

Browse files
JiaLiPassionmhevery
authored andcommitted
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
1 parent 284123c commit 31796e8

5 files changed

Lines changed: 8 additions & 58 deletions

File tree

goldens/size-tracking/integration-payloads.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"master": {
44
"uncompressed": {
55
"runtime-es2015": 1485,
6-
"main-es2015": 141394,
7-
"polyfills-es2015": 36963
6+
"main-es2015": 141151,
7+
"polyfills-es2015": 36571
88
}
99
}
1010
},
@@ -22,7 +22,7 @@
2222
"uncompressed": {
2323
"runtime-es2015": 1485,
2424
"main-es2015": 147314,
25-
"polyfills-es2015": 36963
25+
"polyfills-es2015": 36571
2626
}
2727
}
2828
},

packages/zone.js/lib/common/promise.ts

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -459,44 +459,6 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
459459
ZoneAwarePromise['all'] = ZoneAwarePromise.all;
460460

461461
const NativePromise = global[symbolPromise] = global['Promise'];
462-
const ZONE_AWARE_PROMISE = Zone.__symbol__('ZoneAwarePromise');
463-
464-
let desc = ObjectGetOwnPropertyDescriptor(global, 'Promise');
465-
if (!desc || desc.configurable) {
466-
desc && delete desc.writable;
467-
desc && delete desc.value;
468-
if (!desc) {
469-
desc = {configurable: true, enumerable: true};
470-
}
471-
desc.get = function() {
472-
// if we already set ZoneAwarePromise, use patched one
473-
// otherwise return native one.
474-
return global[ZONE_AWARE_PROMISE] ? global[ZONE_AWARE_PROMISE] : global[symbolPromise];
475-
};
476-
desc.set = function(NewNativePromise) {
477-
if (NewNativePromise === ZoneAwarePromise) {
478-
// if the NewNativePromise is ZoneAwarePromise
479-
// save to global
480-
global[ZONE_AWARE_PROMISE] = NewNativePromise;
481-
} else {
482-
// if the NewNativePromise is not ZoneAwarePromise
483-
// for example: after load zone.js, some library just
484-
// set es6-promise to global, if we set it to global
485-
// directly, assertZonePatched will fail and angular
486-
// will not loaded, so we just set the NewNativePromise
487-
// to global[symbolPromise], so the result is just like
488-
// we load ES6 Promise before zone.js
489-
global[symbolPromise] = NewNativePromise;
490-
if (!NewNativePromise.prototype[symbolThen]) {
491-
patchThen(NewNativePromise);
492-
}
493-
api.setNativePromise(NewNativePromise);
494-
}
495-
};
496-
497-
ObjectDefineProperty(global, 'Promise', desc);
498-
}
499-
500462
global['Promise'] = ZoneAwarePromise;
501463

502464
const symbolThenPatched = __symbol__('thenPatched');

packages/zone.js/lib/extra/bluebird.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,6 @@ Zone.__load_patch('bluebird', (global: any, Zone: ZoneType, api: _ZonePrivate) =
8080
});
8181

8282
// override global promise
83-
global[api.symbol('ZoneAwarePromise')] = Bluebird;
83+
global.Promise = Bluebird;
8484
};
8585
});

packages/zone.js/lib/zone.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,6 @@ interface _ZonePrivate {
340340
patchEventTarget: (global: any, apis: any[], options?: any) => boolean[];
341341
patchOnProperties: (obj: any, properties: string[]|null, prototype?: any) => void;
342342
patchThen: (ctro: Function) => void;
343-
setNativePromise: (nativePromise: any) => void;
344343
patchMethod:
345344
(target: any, name: string,
346345
patchFn: (delegate: Function, delegateName: string, name: string) =>
@@ -1419,14 +1418,6 @@ const Zone: ZoneType = (function(global: any) {
14191418
bindArguments: () => [],
14201419
patchThen: () => noop,
14211420
patchMacroTask: () => noop,
1422-
setNativePromise: (NativePromise: any) => {
1423-
// sometimes NativePromise.resolve static function
1424-
// is not ready yet, (such as core-js/es6.promise)
1425-
// so we need to check here.
1426-
if (NativePromise && typeof NativePromise.resolve === 'function') {
1427-
nativeMicroTaskQueuePromise = NativePromise.resolve(0);
1428-
}
1429-
},
14301421
patchEventPrototype: () => noop,
14311422
isIEOrEdge: () => false,
14321423
getGlobalObjects: () => undefined,

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -334,23 +334,20 @@ describe('Zone', function() {
334334
Zone.assertZonePatched();
335335
});
336336

337-
it('should keep ZoneAwarePromise has been patched', () => {
337+
xit('should throw error if ZoneAwarePromise has been overwritten', () => {
338338
class WrongPromise {
339339
static resolve(value: any) {}
340340

341341
then() {}
342342
}
343343

344344
const ZoneAwarePromise = global.Promise;
345-
const NativePromise = (global as any)[zoneSymbol('Promise')];
346-
global.Promise = WrongPromise;
347345
try {
348-
expect(ZoneAwarePromise).toBeTruthy();
349-
Zone.assertZonePatched();
350-
expect(global.Promise).toBe(ZoneAwarePromise);
346+
global.Promise = WrongPromise;
347+
expect(Zone.assertZonePatched()).toThrow();
351348
} finally {
352349
// restore it.
353-
global.Promise = NativePromise;
350+
global.Promise = ZoneAwarePromise;
354351
}
355352
Zone.assertZonePatched();
356353
});

0 commit comments

Comments
 (0)