Skip to content

Commit

Permalink
fix(core): should check Zone existance when scheduleMicroTask (#20656)
Browse files Browse the repository at this point in the history
PR Close #20656
  • Loading branch information
JiaLiPassion authored and alxhub committed Feb 2, 2018
1 parent 7b120b5 commit 3a86940
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 8 additions & 1 deletion packages/core/src/util.ts
Expand Up @@ -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<any> = Promise.resolve(0);
/**
* Attention: whenever providing a new value, be sure to add an
* entry into the corresponding `....externs.js` file,
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -19,6 +19,7 @@ export class AnimationRendererFactory implements RendererFactory2 {
private _animationCallbacksBuffer: [(e: any) => any, any][] = [];
private _rendererCache = new Map<Renderer2, BaseAnimationRenderer>();
private _cdRecurDepth = 0;
private promise: Promise<any> = Promise.resolve(0);

constructor(
private delegate: RendererFactory2, private engine: AnimationEngine, private _zone: NgZone) {
Expand Down Expand Up @@ -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 */
Expand Down

1 comment on commit 3a86940

@alxhub
Copy link
Member

@alxhub alxhub commented on 3a86940 Feb 6, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JiaLiPassion @mhevery is it possible to add a test for this?

Please sign in to comment.