Skip to content
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(ivy): add afterContentInit and afterContentChecked to render3 #21650

Closed
wants to merge 9 commits into from

Conversation

kara
Copy link
Contributor

@kara kara commented Jan 18, 2018

This PR adds support for the afterContentInit and afterContentChecked hooks to render3. It also adds some missing tests for how projected components will work with other lifecycle hooks.

Update:
This PR now also rewrites lifecycle hooks to remove the l() instruction. We now queue all hooks on the TView as we build up the node tree and execute them implicitly in componentRefresh and view end.

Notes:

  • type has been added back to DirectiveDef. This allows us to copy over lifecycle hooks from the type during defineComponent. This is a megamorphic lookup, but it only runs once per component and it saves us from conducting a megamorphic lookup for each component instance.
  • r has been removed from the DirectiveDef, as it no longer has a purpose. Previously, it contained view hook declarations, but now that these are implicit, we can just call componentRefresh directly.

cc @jelbourn

@kara kara requested a review from mhevery January 18, 2018 22:57
@kara kara added action: review The PR is still awaiting reviews from at least one requested reviewer comp: ivy target: major This PR is targeted for the next major release labels Jan 18, 2018
@@ -40,8 +40,8 @@ export const enum LifecycleHook {
ON_INIT = 1,
ON_DESTROY = 2,
ON_CHANGES = 4,
AFTER_VIEW_INIT = 8,
AFTER_VIEW_CHECKED = 16
AFTER_INIT = 8,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we removing VIEW? We have AFTER_VIEW_INIT and AFTER_CONTENT_INIT

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generalizing it so we can use the same code for content hooks and view hooks in executeHooksAndRemoveInits. We could also pass through the hook, but it would require creating two more hooks just for that check when they are treated the same.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SGTM


for (let i = start, end = start + size; i < end; i++) {
const instance = data[i];
if (instance.ngAfterContentInit != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this is a megamorphic property read and we will do this every time we are creating the component. A better way is to store Component.prototype.ngAfterContentInit in TView that way we are doing the megamorphic property read only once per template. We than use the information from the TView to enqueue the instance into the contentHooks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is a great point

(contentHooks || (currentView.contentHooks = contentHooks = [
])).push(LifecycleHook.AFTER_INIT, instance.ngAfterContentInit, instance);
}
if (instance.ngAfterContentChecked != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

(contentHooks || (currentView.contentHooks = contentHooks = [
])).push(LifecycleHook.AFTER_CHECKED, instance.ngAfterContentChecked, instance);
}
if (instance.ngOnDestroy != null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

* Calls all afterContentInit and afterContentChecked hooks for the view, then splices
* out afterContentInit hooks to prep for the next run in update mode.
*/
function executeContentHooks(): void {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we move this into a file out of instructions.ts since it is getting long? I suggest new file hooks.ts?
(we will have to pass in contentHooks and currentView)

* @param arr The array in which the hooks are found
* @param startIndex The index at which to start calling hooks
*/
function executeHooksAndRemoveInits(arr: any[], startIndex: number): void {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here move to different file.

* out afterContentInit hooks to prep for the next run in update mode.
*/
function executeContentHooks(): void {
if (contentHooks == null || currentView.contentHooksCalled) return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have really hard time with early returns. Could you rewire it in form of a normal if () {} form?

* adding to the code size), so it needs to be able to check whether or not they should
* be called.
*/
contentHooksCalled: boolean;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I don't understand why we need this. Can you explain what would happen if we did not have this?

Copy link
Contributor Author

@kara kara Jan 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then every Comp.r() call would call content hooks, and they'd be called multiple times per change detection run (once for each component in that view). So if you had:

Comp.r(1,0);
Comp.r(3,2);

The first r() call should run content hooks. The second one should not.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@kara kara force-pushed the afterContentInit branch 2 times, most recently from 0fb6253 to 7c9a0cd Compare January 20, 2018 01:14
@kara kara added the action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews label Jan 20, 2018
@kara kara self-assigned this Jan 20, 2018
@kara kara force-pushed the afterContentInit branch 3 times, most recently from e28f10e to 03216e9 Compare January 23, 2018 01:54
@kara kara changed the base branch from g3 to master January 23, 2018 01:56
@kara kara force-pushed the afterContentInit branch 3 times, most recently from 6b08bab to 81de6c4 Compare January 23, 2018 02:02
@mary-poppins
Copy link

You can preview f7e09e2 at https://pr21650-f7e09e2.ngbuilds.io/.

@mary-poppins
Copy link

You can preview 6b08bab at https://pr21650-6b08bab.ngbuilds.io/.

@mary-poppins
Copy link

You can preview 81de6c4 at https://pr21650-81de6c4.ngbuilds.io/.

@mary-poppins
Copy link

You can preview 11787b6 at https://pr21650-11787b6.ngbuilds.io/.

@mary-poppins
Copy link

You can preview e8cbff5 at https://pr21650-e8cbff5.ngbuilds.io/.

@mary-poppins
Copy link

You can preview 9af98ea at https://pr21650-9af98ea.ngbuilds.io/.

@mary-poppins
Copy link

You can preview 3d16302 at https://pr21650-3d16302.ngbuilds.io/.

@mary-poppins
Copy link

You can preview 2be9525 at https://pr21650-2be9525.ngbuilds.io/.

@kara kara removed the action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews label Jan 23, 2018
mhevery pushed a commit that referenced this pull request Jan 25, 2018
mhevery pushed a commit that referenced this pull request Jan 25, 2018
chuckjaz added a commit to chuckjaz/angular that referenced this pull request Jan 29, 2018
Change compiler to reflect changes made in angular#21650
jasonaden pushed a commit that referenced this pull request Jan 31, 2018
Change compiler to reflect changes made in #21650

PR Close #21862
jbogarthyde pushed a commit to jbogarthyde/angular that referenced this pull request Feb 23, 2018
jbogarthyde pushed a commit to jbogarthyde/angular that referenced this pull request Feb 23, 2018
jbogarthyde pushed a commit to jbogarthyde/angular that referenced this pull request Feb 23, 2018
jbogarthyde pushed a commit to jbogarthyde/angular that referenced this pull request Feb 23, 2018
jbogarthyde pushed a commit to jbogarthyde/angular that referenced this pull request Feb 23, 2018
jbogarthyde pushed a commit to jbogarthyde/angular that referenced this pull request Feb 23, 2018
jbogarthyde pushed a commit to jbogarthyde/angular that referenced this pull request Feb 23, 2018
jbogarthyde pushed a commit to jbogarthyde/angular that referenced this pull request Feb 23, 2018
jbogarthyde pushed a commit to jbogarthyde/angular that referenced this pull request Feb 23, 2018
jbogarthyde pushed a commit to jbogarthyde/angular that referenced this pull request Feb 23, 2018
leo6104 pushed a commit to leo6104/angular that referenced this pull request Mar 25, 2018
leo6104 pushed a commit to leo6104/angular that referenced this pull request Mar 25, 2018
leo6104 pushed a commit to leo6104/angular that referenced this pull request Mar 25, 2018
leo6104 pushed a commit to leo6104/angular that referenced this pull request Mar 25, 2018
leo6104 pushed a commit to leo6104/angular that referenced this pull request Mar 25, 2018
leo6104 pushed a commit to leo6104/angular that referenced this pull request Mar 25, 2018
leo6104 pushed a commit to leo6104/angular that referenced this pull request Mar 25, 2018
leo6104 pushed a commit to leo6104/angular that referenced this pull request Mar 25, 2018
leo6104 pushed a commit to leo6104/angular that referenced this pull request Mar 25, 2018
@kara kara deleted the afterContentInit branch October 13, 2018 01:09
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 14, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes target: major This PR is targeted for the next major release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants