Skip to content

Commit

Permalink
fix(core): render hooks should not specifically run outside the Angul…
Browse files Browse the repository at this point in the history
…ar zone (#55399)

The timing of render hook execution is almost entirely identical to
`ngZone.onMicrotaskEmpty`. Developers working towards zoneless
compatibility will need to migrate `onMicrotaskEmpty` calls to use
`afterNextRender`/`afterRender` instead. This, however, would lead to
confusing issues if there are promises in the callbacks because
`onMicrotaskEmpty` emits inside the Angular zone while render hooks
execute outside today. This is problematic because it's not documented
and does not produce any notification or error message when async work
is done inside the hooks that requires change detection. Instead, change detection
simply does not run, and this behavior has proven to be surprising to
developers who are used to ZoneJS change detection behavior.

fixes #55299

PR Close #55399
  • Loading branch information
atscott authored and AndrewKushnir committed Apr 25, 2024
1 parent 544aede commit af0eb84
Showing 1 changed file with 1 addition and 2 deletions.
3 changes: 1 addition & 2 deletions packages/core/src/render3/after_render_hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,6 @@ export function afterNextRender(
* A wrapper around a function to be used as an after render callback.
*/
class AfterRenderCallback {
private zone = inject(NgZone);
private errorHandler = inject(ErrorHandler, {optional: true});

constructor(readonly phase: AfterRenderPhase, private callbackFn: VoidFunction) {
Expand All @@ -341,7 +340,7 @@ class AfterRenderCallback {

invoke() {
try {
this.zone.runOutsideAngular(this.callbackFn);
this.callbackFn();
} catch (err) {
this.errorHandler?.handleError(err);
}
Expand Down

0 comments on commit af0eb84

Please sign in to comment.