6
6
* found in the LICENSE file at https://angular.dev/license
7
7
*/
8
8
9
- import { AfterRenderPhase , AfterRenderRef } from './api' ;
10
- import { NgZone } from '../../zone' ;
11
- import { inject } from '../../di/injector_compatibility' ;
12
- import { ɵɵdefineInjectable } from '../../di/interface/defs' ;
13
- import { ErrorHandler } from '../../error_handler' ;
9
+ import { TracingAction , TracingService , TracingSnapshot } from '../../application/tracing' ;
14
10
import {
15
11
ChangeDetectionScheduler ,
16
12
NotificationSource ,
17
13
} from '../../change_detection/scheduling/zoneless_scheduling' ;
14
+ import { inject } from '../../di/injector_compatibility' ;
15
+ import { ɵɵdefineInjectable } from '../../di/interface/defs' ;
16
+ import { ErrorHandler } from '../../error_handler' ;
18
17
import { type DestroyRef } from '../../linker/destroy_ref' ;
19
- import { TracingAction , TracingService , TracingSnapshot } from '../../application/tracing' ;
18
+ import { NgZone } from '../../zone' ;
19
+ import { AFTER_RENDER_SEQUENCES_TO_ADD , FLAGS , LView , LViewFlags } from '../interfaces/view' ;
20
20
import { profiler } from '../profiler' ;
21
21
import { ProfilerEvent } from '../profiler_types' ;
22
+ import { markAncestorsForTraversal } from '../util/view_utils' ;
23
+ import { AfterRenderPhase , AfterRenderRef } from './api' ;
22
24
23
25
export class AfterRenderManager {
24
26
impl : AfterRenderImpl | null = null ;
@@ -111,7 +113,7 @@ export class AfterRenderImpl {
111
113
this . sequences . add ( sequence ) ;
112
114
}
113
115
if ( this . deferredRegistrations . size > 0 ) {
114
- this . scheduler . notify ( NotificationSource . DeferredRenderHook ) ;
116
+ this . scheduler . notify ( NotificationSource . RenderHook ) ;
115
117
}
116
118
this . deferredRegistrations . clear ( ) ;
117
119
@@ -121,16 +123,28 @@ export class AfterRenderImpl {
121
123
}
122
124
123
125
register ( sequence : AfterRenderSequence ) : void {
124
- if ( ! this . executing ) {
125
- this . sequences . add ( sequence ) ;
126
- // Trigger an `ApplicationRef.tick()` if one is not already pending/running, because we have a
127
- // new render hook that needs to run.
128
- this . scheduler . notify ( NotificationSource . RenderHook ) ;
126
+ const { view} = sequence ;
127
+ if ( view !== undefined ) {
128
+ // Delay adding it to the manager, add it to the view instead.
129
+ ( view [ AFTER_RENDER_SEQUENCES_TO_ADD ] ??= [ ] ) . push ( sequence ) ;
130
+
131
+ // Mark the view for traversal to ensure we eventually schedule the afterNextRender.
132
+ markAncestorsForTraversal ( view ) ;
133
+ view [ FLAGS ] |= LViewFlags . HasChildViewsToRefresh ;
134
+ } else if ( ! this . executing ) {
135
+ this . addSequence ( sequence ) ;
129
136
} else {
130
137
this . deferredRegistrations . add ( sequence ) ;
131
138
}
132
139
}
133
140
141
+ addSequence ( sequence : AfterRenderSequence ) : void {
142
+ this . sequences . add ( sequence ) ;
143
+ // Trigger an `ApplicationRef.tick()` if one is not already pending/running, because we have a
144
+ // new render hook that needs to run.
145
+ this . scheduler . notify ( NotificationSource . RenderHook ) ;
146
+ }
147
+
134
148
unregister ( sequence : AfterRenderSequence ) : void {
135
149
if ( this . executing && this . sequences . has ( sequence ) ) {
136
150
// We can't remove an `AfterRenderSequence` in the middle of iteration.
@@ -185,6 +199,7 @@ export class AfterRenderSequence implements AfterRenderRef {
185
199
constructor (
186
200
readonly impl : AfterRenderImpl ,
187
201
readonly hooks : AfterRenderHooks ,
202
+ readonly view : LView | undefined ,
188
203
public once : boolean ,
189
204
destroyRef : DestroyRef | null ,
190
205
public snapshot : TracingSnapshot | null = null ,
@@ -207,5 +222,9 @@ export class AfterRenderSequence implements AfterRenderRef {
207
222
destroy ( ) : void {
208
223
this . impl . unregister ( this ) ;
209
224
this . unregisterOnDestroy ?.( ) ;
225
+ const scheduled = this . view ?. [ AFTER_RENDER_SEQUENCES_TO_ADD ] ;
226
+ if ( scheduled ) {
227
+ this . view [ AFTER_RENDER_SEQUENCES_TO_ADD ] = scheduled . filter ( ( s ) => s !== this ) ;
228
+ }
210
229
}
211
230
}
0 commit comments