From d754cc1ea56efc771867ac288e25a1968fd314fe Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Fri, 29 Jan 2021 15:09:37 -0800 Subject: [PATCH] Revert "fix(core): NgZone coaleascing options should trigger onStable correctly (#40540)" This reverts commit 22f9e454a46a8b017479d518bedd40efe1e7be2b. --- packages/core/src/zone/ng_zone.ts | 32 ++----------------- packages/core/test/zone/ng_zone_spec.ts | 42 ------------------------- 2 files changed, 2 insertions(+), 72 deletions(-) diff --git a/packages/core/src/zone/ng_zone.ts b/packages/core/src/zone/ng_zone.ts index 33152592cb..bb230c27f7 100644 --- a/packages/core/src/zone/ng_zone.ts +++ b/packages/core/src/zone/ng_zone.ts @@ -148,7 +148,6 @@ export class NgZone { !shouldCoalesceRunChangeDetection && shouldCoalesceEventChangeDetection; self.shouldCoalesceRunChangeDetection = shouldCoalesceRunChangeDetection; self.lastRequestAnimationFrameId = -1; - self.isCheckStableRunning = false; self.nativeRequestAnimationFrame = getNativeRequestAnimationFrame().nativeRequestAnimationFrame; forkInnerZoneWithAngularBehavior(self); } @@ -244,17 +243,6 @@ interface NgZonePrivate extends NgZone { hasPendingMacrotasks: boolean; hasPendingMicrotasks: boolean; lastRequestAnimationFrameId: number; - /** - * A flag to indicate if NgZone is currently inside - * checkStable and to prevent re-entry. The flag is - * needed because it is possible to invoke the change - * detection from within change detection leading to - * incorrect behavior. - * - * For detail, please refer here, - * https://github.com/angular/angular/pull/40540 - */ - isCheckStableRunning: boolean; isStable: boolean; /** * Optionally specify coalescing event change detections or not. @@ -303,9 +291,7 @@ interface NgZonePrivate extends NgZone { } function checkStable(zone: NgZonePrivate) { - if (!zone.isCheckStableRunning && zone._nesting == 0 && !zone.hasPendingMicrotasks && - !zone.isStable) { - zone.isCheckStableRunning = true; + if (zone._nesting == 0 && !zone.hasPendingMicrotasks && !zone.isStable) { try { zone._nesting++; zone.onMicrotaskEmpty.emit(null); @@ -318,26 +304,12 @@ function checkStable(zone: NgZonePrivate) { zone.isStable = true; } } - zone.isCheckStableRunning = false; } } } function delayChangeDetectionForEvents(zone: NgZonePrivate) { - /** - * We also need to check isCheckStableRunning here - * Consider the following case with shouldCoalesceRunChangeDetection = true - * - * ngZone.run(() => {}); - * ngZone.run(() => {}); - * - * We want the two `ngZone.run()` only trigger one change detection - * when shouldCoalesceRunChangeDetection is true. - * And because in this case, change detection run in async way(requestAnimationFrame), - * so we also need to check the isCheckStableRunning here to prevent multiple - * change detections. - */ - if (zone.isCheckStableRunning || zone.lastRequestAnimationFrameId !== -1) { + if (zone.lastRequestAnimationFrameId !== -1) { return; } zone.lastRequestAnimationFrameId = zone.nativeRequestAnimationFrame.call(global, () => { diff --git a/packages/core/test/zone/ng_zone_spec.ts b/packages/core/test/zone/ng_zone_spec.ts index dd9b6cc5ad..3f11518a0e 100644 --- a/packages/core/test/zone/ng_zone_spec.ts +++ b/packages/core/test/zone/ng_zone_spec.ts @@ -982,7 +982,6 @@ function commonTests() { describe('shouldCoalesceEventChangeDetection = true, shouldCoalesceRunChangeDetection = false', () => { let nativeRequestAnimationFrame: (fn: FrameRequestCallback) => void; - let nativeSetTimeout: any = (global as any)[Zone.__symbol__('setTimeout')]; if (!(global as any).requestAnimationFrame) { nativeRequestAnimationFrame = function(fn: Function) { (global as any)[Zone.__symbol__('setTimeout')](fn, 16); @@ -1043,32 +1042,6 @@ function commonTests() { }); }); - it('should only emit onMicroTaskEmpty once within the same event loop for ngZone.run in onMicrotaskEmpty subscription', - (done: DoneFn) => { - const tasks: Task[] = []; - coalesceZone.onMicrotaskEmpty.subscribe(() => { - coalesceZone.run(() => {}); - }); - coalesceZone.run(() => { - tasks.push(Zone.current.scheduleEventTask('myEvent', () => { - logs.push('eventTask1'); - }, undefined, () => {})); - }); - coalesceZone.run(() => { - tasks.push(Zone.current.scheduleEventTask('myEvent', () => { - logs.push('eventTask2'); - }, undefined, () => {})); - }); - tasks.forEach(t => t.invoke()); - expect(logs).toEqual(['microTask empty', 'microTask empty', 'eventTask1', 'eventTask2']); - nativeSetTimeout(() => { - expect(logs).toEqual([ - 'microTask empty', 'microTask empty', 'eventTask1', 'eventTask2', 'microTask empty' - ]); - done(); - }, 100); - }); - it('should emit onMicroTaskEmpty once within the same event loop for not only event tasks, but event tasks are before other tasks', (done: DoneFn) => { const tasks: Task[] = []; @@ -1121,7 +1094,6 @@ function commonTests() { describe('shouldCoalesceRunChangeDetection = true', () => { let nativeRequestAnimationFrame: (fn: FrameRequestCallback) => void; - let nativeSetTimeout: any = (global as any)[Zone.__symbol__('setTimeout')]; if (!(global as any).requestAnimationFrame) { nativeRequestAnimationFrame = function(fn: Function) { (global as any)[Zone.__symbol__('setTimeout')](fn, 16); @@ -1167,20 +1139,6 @@ function commonTests() { }); }); - it('should only emit onMicroTaskEmpty once within the same event loop for ngZone.run in onMicrotaskEmpty subscription', - (done: DoneFn) => { - coalesceZone.onMicrotaskEmpty.subscribe(() => { - coalesceZone.run(() => {}); - }); - coalesceZone.run(() => {}); - coalesceZone.run(() => {}); - expect(logs).toEqual([]); - nativeSetTimeout(() => { - expect(logs).toEqual(['microTask empty']); - done(); - }, 100); - }); - it('should only emit onMicroTaskEmpty once within the same event loop for multiple tasks', (done: DoneFn) => { const tasks: Task[] = [];