|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {EnvironmentProviders, inject, makeEnvironmentProviders} from '../di'; |
| 10 | +import {NgZone} from '../zone'; |
| 11 | +import {provideAppInitializer} from './application_init'; |
| 12 | +import {ApplicationRef} from './application_ref'; |
| 13 | +import {APPLICATION_IS_STABLE_TIMEOUT} from '../hydration/api'; |
| 14 | +import {DEBUG_TASK_TRACKER, DebugTaskTracker} from './stability_debug'; |
| 15 | + |
| 16 | +const STABILITY_WARNING_THRESHOLD = APPLICATION_IS_STABLE_TIMEOUT - 1_000; |
| 17 | + |
| 18 | +class DebugTaskTrackerImpl implements DebugTaskTracker { |
| 19 | + readonly openTasks = new Map<number, Error>(); |
| 20 | + |
| 21 | + add(taskId: number): void { |
| 22 | + this.openTasks.set(taskId, new Error('Task stack tracking error')); |
| 23 | + } |
| 24 | + |
| 25 | + remove(taskId: number): void { |
| 26 | + this.openTasks.delete(taskId); |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Provides an application initializer that will log information about what tasks are keeping |
| 32 | + * the application from stabilizing if the application does not stabilize within 9 seconds. |
| 33 | + * |
| 34 | + * The logged information includes the stack of the tasks preventing stability. This stack can be traced |
| 35 | + * back to the source in the application code. |
| 36 | + * |
| 37 | + * If you are using Zone.js, it is recommended that you also temporarily import "zone.js/plugins/task-tracking". |
| 38 | + * This Zone.js plugin provides additional information about which macrotasks are scheduled in the Angular Zone |
| 39 | + * and keeping the Zone from stabilizing. |
| 40 | + * |
| 41 | + * @usageNotes |
| 42 | + * |
| 43 | + * ```ts |
| 44 | + * import 'zone.js/plugins/task-tracking'; |
| 45 | + * |
| 46 | + * bootstrapApplication(AppComponent, {providers: [provideStabilityDebugging()]}); |
| 47 | + * ``` |
| 48 | + * |
| 49 | + * IMPORTANT: Neither the zone.js task tracking plugin nor this utility are removed from production bundles. |
| 50 | + * They are intended for temporary use while debugging stability issues during development, including for |
| 51 | + * optimized production builds. |
| 52 | + * |
| 53 | + * @publicApi 21.1 |
| 54 | + */ |
| 55 | +export function provideStabilityDebugging(): EnvironmentProviders { |
| 56 | + const taskTracker = new DebugTaskTrackerImpl(); |
| 57 | + const {openTasks} = taskTracker; |
| 58 | + return makeEnvironmentProviders([ |
| 59 | + { |
| 60 | + provide: DEBUG_TASK_TRACKER, |
| 61 | + useValue: taskTracker, |
| 62 | + }, |
| 63 | + provideAppInitializer(() => { |
| 64 | + if (typeof ngDevMode === 'undefined' || !ngDevMode) { |
| 65 | + console.warn( |
| 66 | + 'Stability debugging untility was provided in production mode. ' + |
| 67 | + 'This will cause debug code to be included in production bundles. ' + |
| 68 | + 'If this is intentional because you are debugging stability issues in a production environment, you can ignore this warning.', |
| 69 | + ); |
| 70 | + } |
| 71 | + const ngZone = inject(NgZone); |
| 72 | + const applicationRef = inject(ApplicationRef); |
| 73 | + |
| 74 | + // From TaskTrackingZone: |
| 75 | + // https://github.com/angular/angular/blob/ae0c59028a2f393ea5716bf222db2c38e7a3989f/packages/zone.js/lib/zone-spec/task-tracking.ts#L46 |
| 76 | + let _taskTrackingZone: {macroTasks: Array<{creationLocation: Error}>} | null = null; |
| 77 | + if (typeof Zone !== 'undefined') { |
| 78 | + ngZone.run(() => { |
| 79 | + _taskTrackingZone = Zone.current.get('TaskTrackingZone'); |
| 80 | + }); |
| 81 | + } |
| 82 | + ngZone.runOutsideAngular(() => { |
| 83 | + const timeoutId = setTimeout(() => { |
| 84 | + console.debug( |
| 85 | + `---- Application did not stabilize within ${STABILITY_WARNING_THRESHOLD / 1000} seconds ----`, |
| 86 | + ); |
| 87 | + if (typeof Zone !== 'undefined' && !_taskTrackingZone) { |
| 88 | + console.info( |
| 89 | + 'Zone.js is present but no TaskTrackingZone found. To enable better debugging of tasks in the Angular Zone, ' + |
| 90 | + 'import "zone.js/plugins/task-tracking" in your application.', |
| 91 | + ); |
| 92 | + } |
| 93 | + if (_taskTrackingZone?.macroTasks?.length) { |
| 94 | + console.group('Macrotasks keeping Angular Zone unstable:'); |
| 95 | + for (const t of _taskTrackingZone?.macroTasks ?? []) { |
| 96 | + console.debug(t.creationLocation.stack); |
| 97 | + } |
| 98 | + console.groupEnd(); |
| 99 | + } |
| 100 | + console.group('PendingTasks keeping application unstable:'); |
| 101 | + for (const error of openTasks.values()) { |
| 102 | + console.debug(error.stack); |
| 103 | + } |
| 104 | + console.groupEnd(); |
| 105 | + }, STABILITY_WARNING_THRESHOLD); |
| 106 | + |
| 107 | + applicationRef.whenStable().then(() => { |
| 108 | + clearTimeout(timeoutId); |
| 109 | + }); |
| 110 | + }); |
| 111 | + }), |
| 112 | + ]); |
| 113 | +} |
0 commit comments