|
6 | 6 | * found in the LICENSE file at https://angular.io/license |
7 | 7 | */ |
8 | 8 |
|
| 9 | +import {SCHEDULE_IN_ROOT_ZONE_DEFAULT} from '../change_detection/scheduling/flags'; |
9 | 10 | import {RuntimeError, RuntimeErrorCode} from '../errors'; |
10 | 11 | import {EventEmitter} from '../event_emitter'; |
11 | 12 | import {scheduleCallbackWithRafRace} from '../util/callback_scheduler'; |
12 | | -import {global} from '../util/global'; |
13 | 13 | import {noop} from '../util/noop'; |
14 | 14 |
|
15 | 15 | import {AsyncStackTaggingZoneSpec} from './async-stack-tagging'; |
@@ -130,11 +130,18 @@ export class NgZone { |
130 | 130 | */ |
131 | 131 | readonly onError: EventEmitter<any> = new EventEmitter(false); |
132 | 132 |
|
133 | | - constructor({ |
134 | | - enableLongStackTrace = false, |
135 | | - shouldCoalesceEventChangeDetection = false, |
136 | | - shouldCoalesceRunChangeDetection = false, |
| 133 | + constructor(options: { |
| 134 | + enableLongStackTrace?: boolean; |
| 135 | + shouldCoalesceEventChangeDetection?: boolean; |
| 136 | + shouldCoalesceRunChangeDetection?: boolean; |
137 | 137 | }) { |
| 138 | + const { |
| 139 | + enableLongStackTrace = false, |
| 140 | + shouldCoalesceEventChangeDetection = false, |
| 141 | + shouldCoalesceRunChangeDetection = false, |
| 142 | + scheduleInRootZone = SCHEDULE_IN_ROOT_ZONE_DEFAULT, |
| 143 | + } = options as InternalNgZoneOptions; |
| 144 | + |
138 | 145 | if (typeof Zone == 'undefined') { |
139 | 146 | throw new RuntimeError( |
140 | 147 | RuntimeErrorCode.MISSING_ZONEJS, |
@@ -170,6 +177,7 @@ export class NgZone { |
170 | 177 | !shouldCoalesceRunChangeDetection && shouldCoalesceEventChangeDetection; |
171 | 178 | self.shouldCoalesceRunChangeDetection = shouldCoalesceRunChangeDetection; |
172 | 179 | self.callbackScheduled = false; |
| 180 | + self.scheduleInRootZone = scheduleInRootZone; |
173 | 181 | forkInnerZoneWithAngularBehavior(self); |
174 | 182 | } |
175 | 183 |
|
@@ -330,6 +338,11 @@ export interface NgZonePrivate extends NgZone { |
330 | 338 | * |
331 | 339 | */ |
332 | 340 | shouldCoalesceRunChangeDetection: boolean; |
| 341 | + |
| 342 | + /** |
| 343 | + * Whether to schedule the coalesced change detection in the root zone |
| 344 | + */ |
| 345 | + scheduleInRootZone: boolean; |
333 | 346 | } |
334 | 347 |
|
335 | 348 | function checkStable(zone: NgZonePrivate) { |
@@ -383,15 +396,24 @@ function delayChangeDetectionForEvents(zone: NgZonePrivate) { |
383 | 396 | return; |
384 | 397 | } |
385 | 398 | zone.callbackScheduled = true; |
386 | | - Zone.root.run(() => { |
| 399 | + function scheduleCheckStable() { |
387 | 400 | scheduleCallbackWithRafRace(() => { |
388 | 401 | zone.callbackScheduled = false; |
389 | 402 | updateMicroTaskStatus(zone); |
390 | 403 | zone.isCheckStableRunning = true; |
391 | 404 | checkStable(zone); |
392 | 405 | zone.isCheckStableRunning = false; |
393 | 406 | }); |
394 | | - }); |
| 407 | + } |
| 408 | + if (zone.scheduleInRootZone) { |
| 409 | + Zone.root.run(() => { |
| 410 | + scheduleCheckStable(); |
| 411 | + }); |
| 412 | + } else { |
| 413 | + zone._outer.run(() => { |
| 414 | + scheduleCheckStable(); |
| 415 | + }); |
| 416 | + } |
395 | 417 | updateMicroTaskStatus(zone); |
396 | 418 | } |
397 | 419 |
|
@@ -574,9 +596,10 @@ function hasApplyArgsData(applyArgs: unknown, key: string) { |
574 | 596 |
|
575 | 597 | // Set of options recognized by the NgZone. |
576 | 598 | export interface InternalNgZoneOptions { |
577 | | - enableLongStackTrace: boolean; |
578 | | - shouldCoalesceEventChangeDetection: boolean; |
579 | | - shouldCoalesceRunChangeDetection: boolean; |
| 599 | + enableLongStackTrace?: boolean; |
| 600 | + shouldCoalesceEventChangeDetection?: boolean; |
| 601 | + shouldCoalesceRunChangeDetection?: boolean; |
| 602 | + scheduleInRootZone?: boolean; |
580 | 603 | } |
581 | 604 |
|
582 | 605 | export function getNgZone( |
|
0 commit comments