Skip to content

Commit

Permalink
Upgrade to typescript 2.5.2
Browse files Browse the repository at this point in the history
Due to stricter generic type checks, signatures for ZoneAwarePromise
need to be changed. See
microsoft/TypeScript#16368

The signatures from lib.es5.d.ts were copied over for .then and .catch.
  • Loading branch information
rkirov committed Oct 11, 2017
1 parent 326a07f commit 5686b98
Show file tree
Hide file tree
Showing 4 changed files with 3,271 additions and 2,904 deletions.
112 changes: 59 additions & 53 deletions lib/common/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr

const UNHANDLED_PROMISE_REJECTION_HANDLER_SYMBOL = __symbol__('unhandledPromiseRejectionHandler');

function handleUnhandledRejection(e: any) {
api.onUnhandledError(e);
try {
const handler = (Zone as any)[UNHANDLED_PROMISE_REJECTION_HANDLER_SYMBOL];
if (handler && typeof handler === 'function') {
handler.apply(this, [e]);
function handleUnhandledRejection(e: any) {
api.onUnhandledError(e);
try {
const handler = (Zone as any)[UNHANDLED_PROMISE_REJECTION_HANDLER_SYMBOL];
if (handler && typeof handler === 'function') {
handler.apply(this, [e]);
}
} catch (err) {
}
} catch (err) {
}
}

function isThenable(value: any): boolean {
return value && value.then;
Expand Down Expand Up @@ -207,13 +207,14 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
}
}

function scheduleResolveOrReject<R, U>(
promise: ZoneAwarePromise<any>, zone: AmbientZone, chainPromise: ZoneAwarePromise<any>,
onFulfilled?: (value: R) => U, onRejected?: (error: any) => U): void {
function scheduleResolveOrReject<R, U1, U2>(
promise: ZoneAwarePromise<any>, zone: AmbientZone,
chainPromise: ZoneAwarePromise<any>, onFulfilled?: (value: R) => U1,
onRejected?: (error: any) => U2): void {
clearRejectedNoCatch(promise);
const delegate = (promise as any)[symbolState] ?
(typeof onFulfilled === FUNCTION) ? onFulfilled : forwardResolution :
(typeof onRejected === FUNCTION) ? onRejected : forwardRejection;
(typeof onFulfilled === FUNCTION) ? onFulfilled : forwardResolution :
(typeof onRejected === FUNCTION) ? onRejected : forwardRejection;
zone.scheduleMicroTask(source, () => {
try {
resolvePromise(
Expand Down Expand Up @@ -265,7 +266,7 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
static all<R>(values: any): Promise<R> {
let resolve: (v: any) => void;
let reject: (v: any) => void;
let promise = new this((res, rej) => {
let promise = new this<R>((res, rej) => {
resolve = res;
reject = rej;
});
Expand Down Expand Up @@ -306,10 +307,13 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
}
}

then<R, U>(
onFulfilled?: (value: R) => U | PromiseLike<U>,
onRejected?: (error: any) => U | PromiseLike<U>): Promise<R> {
const chainPromise: Promise<R> = new (this.constructor as typeof ZoneAwarePromise)(null);
then<TResult1 = R, TResult2 = never>(
onFulfilled?: ((value: R) => TResult1 | PromiseLike<TResult1>)|
undefined|null,
onRejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>)|
undefined|null): Promise<TResult1|TResult2> {
const chainPromise: Promise<TResult1|TResult2> =
new (this.constructor as typeof ZoneAwarePromise)(null);
const zone = Zone.current;
if ((this as any)[symbolState] == UNRESOLVED) {
(<any[]>(this as any)[symbolValue]).push(zone, chainPromise, onFulfilled, onRejected);
Expand All @@ -319,7 +323,9 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
return chainPromise;
}

catch<U>(onRejected?: (error: any) => U | PromiseLike<U>): Promise<R> {
catch<TResult = never>(
onRejected?: ((reason: any) => TResult | PromiseLike<TResult>)|
undefined|null): Promise<R|TResult> {
return this.then(null, onRejected);
}
}
Expand All @@ -333,43 +339,43 @@ Zone.__load_patch('ZoneAwarePromise', (global: any, Zone: ZoneType, api: _ZonePr
const NativePromise = global[symbolPromise] = global['Promise'];
const ZONE_AWARE_PROMISE = Zone.__symbol__('ZoneAwarePromise');

let desc = Object.getOwnPropertyDescriptor(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);
let desc = Object.getOwnPropertyDescriptor(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);
}
};

Object.defineProperty(global, 'Promise', desc);
}
Object.defineProperty(global, 'Promise', desc);
}

global['Promise'] = ZoneAwarePromise;
global['Promise'] = ZoneAwarePromise;

const symbolThenPatched = __symbol__('thenPatched');

Expand Down
Loading

0 comments on commit 5686b98

Please sign in to comment.