Skip to content

Commit 0ac949c

Browse files
arturovtpkozlowski-opensource
authored andcommitted
fix(core): do not run change detection on global error events (angular#60944)
This commit wraps the `error` and `unhandledrejection` event listeners so they are installed outside of the Angular zone, because otherwise they trigger change detection whenever the event callbacks are invoked. PR Close angular#60944
1 parent e3d45a4 commit 0ac949c

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

packages/core/src/error_handler.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,19 @@ const globalErrorListeners = new InjectionToken<void>(ngDevMode ? 'GlobalErrorLi
104104
e.preventDefault();
105105
};
106106

107-
window.addEventListener('unhandledrejection', rejectionListener);
108-
window.addEventListener('error', errorListener);
107+
const setupEventListeners = () => {
108+
window.addEventListener('unhandledrejection', rejectionListener);
109+
window.addEventListener('error', errorListener);
110+
};
111+
112+
// Angular doesn't have to run change detection whenever any asynchronous tasks are invoked in
113+
// the scope of this functionality.
114+
if (typeof Zone !== 'undefined') {
115+
Zone.root.run(setupEventListeners);
116+
} else {
117+
setupEventListeners();
118+
}
119+
109120
inject(DestroyRef).onDestroy(() => {
110121
window.removeEventListener('error', errorListener);
111122
window.removeEventListener('unhandledrejection', rejectionListener);

0 commit comments

Comments
 (0)