-
Notifications
You must be signed in to change notification settings - Fork 24.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(core): add ngAfterViewInit and ngAfterViewChecked support to ren… #21266
Conversation
You can preview bc8e9b0 at https://pr21266-bc8e9b0.ngbuilds.io/. |
lifecycle: LifecycleHook.AFTER_VIEW_CHECKED, self: any, method: Function): void; | ||
export function lifecycle(lifecycle: LifecycleHook): boolean; | ||
export function lifecycle(lifecycle: LifecycleHook, self?: any, method?: Function): boolean { | ||
if (lifecycle & LifecycleHook.ON_INIT) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why did you change it from ===
to &
?
} else if ( | ||
creationMode && | ||
(lifecycle & LifecycleHook.AFTER_VIEW_INIT || lifecycle & LifecycleHook.AFTER_VIEW_CHECKED)) { | ||
if (typeof viewHookStartIndex !== 'number') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how abut == null
that may be clearer than typeof
while (index < data.length) { | ||
data[index + 1].call(data[index + 2]); | ||
// Splice out init hooks after first run so we don't have to keep iterating over them | ||
data[index]&LifecycleHook.AFTER_VIEW_INIT ? data.splice(index, 3) : index += 3; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
repeated splice
may get expensive. What about compacting the array and than truncating it?
Compacting means that you copy value from higher location to lower in essence moving the higher bits lower and that you just truncate it. (in essence that is what splice does, but we will do it only once where splice will do it once per item.)
} | ||
return false; | ||
} | ||
|
||
/** Iterates over view hook functions and calls them. */ | ||
export function executeViewHooks(): void { | ||
if (typeof viewHookStartIndex !== 'number') return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
!== null
1dd8036
to
34a4acd
Compare
You can preview 1dd8036 at https://pr21266-1dd8036.ngbuilds.io/. |
You can preview 34a4acd at https://pr21266-34a4acd.ngbuilds.io/. |
You can preview b1d0c37 at https://pr21266-b1d0c37.ngbuilds.io/. |
You can preview 40f7a4f at https://pr21266-40f7a4f.ngbuilds.io/. |
Green in google3 💚 http://test/OCL:181318529:BASE:181318544:1515512702286:196a2991 |
You can preview 3a9b742 at https://pr21266-3a9b742.ngbuilds.io/. |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
This PR adds support for the
ngAfterViewInit
andngAfterViewChecked
lifecycle hooks in render3. These hooks cannot simply be called inside the componentrefresh
function because that would result in somengAfterViewInit
hooks being called prematurely (see correct order here). To fix this, the view hook functions are queued and only called once all the components below the current component in the tree have been processed.Notes:
data
array, so we need to keep track of theviewHookStartIndex
(the point in the data array after which only view hooks are stored). Storing this index is cheaper than creating a separateviewHooks
array because arrays have 70 bytes each in memory overhead (which would be 70 bytes cost for every view).creationMode
on theViewState
again. Previously, we were using the existence ofdata
as an implicit marker of creation mode, but this doesn't work for embedded views. When exiting a child view back into the parent view,data
will be defined andcreationMode
will be improperly reported as false. This is important to fix now because if creation mode is false, the view hook array won't be built up correctly.ngAfterViewInit
hooks are spliced out as soon as they are invoked (ngAfterViewChecked
hooks stay present). This will give us a performance hit in creation mode, but then we avoid iterating over dead init hooks in every change detection run after that.Array.splice
, as it's too expensive. Instead we are manually shifting AfterViewChecked hooks down as we go through and truncating once at the end.