From 3a86940ca553de512963524f2a86574fd0fe77ca Mon Sep 17 00:00:00 2001 From: "JiaLi.Passion" Date: Tue, 28 Nov 2017 16:23:24 +0900 Subject: [PATCH] fix(core): should check Zone existance when scheduleMicroTask (#20656) PR Close #20656 --- packages/core/src/util.ts | 9 ++++++++- .../animations/src/animation_renderer.ts | 4 +++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/core/src/util.ts b/packages/core/src/util.ts index 1b1d088258..3ff5b2ae6a 100644 --- a/packages/core/src/util.ts +++ b/packages/core/src/util.ts @@ -21,6 +21,8 @@ const __self = typeof self !== 'undefined' && typeof WorkerGlobalScope !== 'unde self instanceof WorkerGlobalScope && self; const __global = typeof global !== 'undefined' && global; const _global: {[name: string]: any} = __window || __global || __self; + +const promise: Promise = Promise.resolve(0); /** * Attention: whenever providing a new value, be sure to add an * entry into the corresponding `....externs.js` file, @@ -52,7 +54,12 @@ export function getSymbolIterator(): string|symbol { } export function scheduleMicroTask(fn: Function) { - Zone.current.scheduleMicroTask('scheduleMicrotask', fn); + if (typeof Zone === 'undefined') { + // use promise to schedule microTask instead of use Zone + promise.then(() => { fn && fn.apply(null, null); }); + } else { + Zone.current.scheduleMicroTask('scheduleMicrotask', fn); + } } // JS has NaN !== NaN diff --git a/packages/platform-browser/animations/src/animation_renderer.ts b/packages/platform-browser/animations/src/animation_renderer.ts index 9c360da927..b7cf1cc2f0 100644 --- a/packages/platform-browser/animations/src/animation_renderer.ts +++ b/packages/platform-browser/animations/src/animation_renderer.ts @@ -19,6 +19,7 @@ export class AnimationRendererFactory implements RendererFactory2 { private _animationCallbacksBuffer: [(e: any) => any, any][] = []; private _rendererCache = new Map(); private _cdRecurDepth = 0; + private promise: Promise = Promise.resolve(0); constructor( private delegate: RendererFactory2, private engine: AnimationEngine, private _zone: NgZone) { @@ -69,7 +70,8 @@ export class AnimationRendererFactory implements RendererFactory2 { } private _scheduleCountTask() { - Zone.current.scheduleMicroTask('incremenet the animation microtask', () => this._microtaskId++); + // always use promise to schedule microtask instead of use Zone + this.promise.then(() => { this._microtaskId++; }); } /* @internal */