build: upgrade zone.js (#23108)

PR Close #23108
This commit is contained in:
JiaLi.Passion
2018-04-01 00:44:38 +09:00
committed by Alex Rickabaugh
parent 46eadb5cfb
commit c560423b52
24 changed files with 345 additions and 246 deletions

View File

@ -5,17 +5,10 @@
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {discardPeriodicTasksFallback, fakeAsyncFallback, flushFallback, flushMicrotasksFallback, resetFakeAsyncZoneFallback, tickFallback} from './fake_async_fallback';
const _Zone: any = typeof Zone !== 'undefined' ? Zone : null;
const FakeAsyncTestZoneSpec = _Zone && _Zone['FakeAsyncTestZoneSpec'];
type ProxyZoneSpec = {
setDelegate(delegateSpec: ZoneSpec): void; getDelegate(): ZoneSpec; resetDelegate(): void;
};
const ProxyZoneSpec: {get(): ProxyZoneSpec; assertPresent: () => ProxyZoneSpec} =
_Zone && _Zone['ProxyZoneSpec'];
let _fakeAsyncTestZoneSpec: any = null;
const fakeAsyncTestModule = _Zone && _Zone[_Zone.__symbol__('fakeAsyncTest')];
/**
* Clears out the shared fake async zone for a test.
@ -23,14 +16,14 @@ let _fakeAsyncTestZoneSpec: any = null;
*
* @experimental
*/
export function resetFakeAsyncZone() {
_fakeAsyncTestZoneSpec = null;
// in node.js testing we may not have ProxyZoneSpec in which case there is nothing to reset.
ProxyZoneSpec && ProxyZoneSpec.assertPresent().resetDelegate();
export function resetFakeAsyncZone(): void {
if (fakeAsyncTestModule) {
return fakeAsyncTestModule.resetFakeAsyncZone();
} else {
return resetFakeAsyncZoneFallback();
}
}
let _inFakeAsyncCall = false;
/**
* Wraps a function to be executed in the fakeAsync zone:
* - microtasks are manually executed by calling `flushMicrotasks()`,
@ -50,55 +43,11 @@ let _inFakeAsyncCall = false;
* @experimental
*/
export function fakeAsync(fn: Function): (...args: any[]) => any {
// Not using an arrow function to preserve context passed from call site
return function(...args: any[]) {
const proxyZoneSpec = ProxyZoneSpec.assertPresent();
if (_inFakeAsyncCall) {
throw new Error('fakeAsync() calls can not be nested');
}
_inFakeAsyncCall = true;
try {
if (!_fakeAsyncTestZoneSpec) {
if (proxyZoneSpec.getDelegate() instanceof FakeAsyncTestZoneSpec) {
throw new Error('fakeAsync() calls can not be nested');
}
_fakeAsyncTestZoneSpec = new FakeAsyncTestZoneSpec();
}
let res: any;
const lastProxyZoneSpec = proxyZoneSpec.getDelegate();
proxyZoneSpec.setDelegate(_fakeAsyncTestZoneSpec);
try {
res = fn.apply(this, args);
flushMicrotasks();
} finally {
proxyZoneSpec.setDelegate(lastProxyZoneSpec);
}
if (_fakeAsyncTestZoneSpec.pendingPeriodicTimers.length > 0) {
throw new Error(
`${_fakeAsyncTestZoneSpec.pendingPeriodicTimers.length} ` +
`periodic timer(s) still in the queue.`);
}
if (_fakeAsyncTestZoneSpec.pendingTimers.length > 0) {
throw new Error(
`${_fakeAsyncTestZoneSpec.pendingTimers.length} timer(s) still in the queue.`);
}
return res;
} finally {
_inFakeAsyncCall = false;
resetFakeAsyncZone();
}
};
}
function _getFakeAsyncZoneSpec(): any {
if (_fakeAsyncTestZoneSpec == null) {
throw new Error('The code should be running in the fakeAsync zone to call this function');
if (fakeAsyncTestModule) {
return fakeAsyncTestModule.fakeAsync(fn);
} else {
return fakeAsyncFallback(fn);
}
return _fakeAsyncTestZoneSpec;
}
/**
@ -114,7 +63,11 @@ function _getFakeAsyncZoneSpec(): any {
* @experimental
*/
export function tick(millis: number = 0): void {
_getFakeAsyncZoneSpec().tick(millis);
if (fakeAsyncTestModule) {
return fakeAsyncTestModule.tick(millis);
} else {
return tickFallback(millis);
}
}
/**
@ -128,7 +81,11 @@ export function tick(millis: number = 0): void {
* @experimental
*/
export function flush(maxTurns?: number): number {
return _getFakeAsyncZoneSpec().flush(maxTurns);
if (fakeAsyncTestModule) {
return fakeAsyncTestModule.flush(maxTurns);
} else {
return flushFallback(maxTurns);
}
}
/**
@ -137,9 +94,11 @@ export function flush(maxTurns?: number): number {
* @experimental
*/
export function discardPeriodicTasks(): void {
const zoneSpec = _getFakeAsyncZoneSpec();
const pendingTimers = zoneSpec.pendingPeriodicTimers;
zoneSpec.pendingPeriodicTimers.length = 0;
if (fakeAsyncTestModule) {
return fakeAsyncTestModule.discardPeriodicTasks();
} else {
discardPeriodicTasksFallback();
}
}
/**
@ -148,5 +107,9 @@ export function discardPeriodicTasks(): void {
* @experimental
*/
export function flushMicrotasks(): void {
_getFakeAsyncZoneSpec().flushMicrotasks();
if (fakeAsyncTestModule) {
return fakeAsyncTestModule.flushMicrotasks();
} else {
return flushMicrotasksFallback();
}
}