Skip to content

refactor(cdk/menu): use inject for all injection #24941

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

Merged
merged 1 commit into from
May 23, 2022

Conversation

jelbourn
Copy link
Member

@jelbourn jelbourn commented May 18, 2022

This change switches the cdk menu from using constructor injection to using the inject function. This was enabled by angular/angular#45991. This approach has a number of advantages:

  • We can add and remove injectables without changing the public constructor signature
  • It reduces the boilerplate of passing arguments through super
  • It avoids long/messy constructor blocks in favor of simpler member definitions.
  • This sidesteps a common pitfall of making @Optional injection (null if not provided) as an optional parameter (undefined if not provided).

I expect this PR to involve some discussion around how we want to approach this in general.

@jelbourn jelbourn added P2 The issue is important to a large percentage of users, with a workaround refactoring This issue is related to a refactoring target: rc This PR is targeted for the next release-candidate labels May 18, 2022
@jelbourn jelbourn requested a review from andrewseguin May 18, 2022 22:53
@jelbourn jelbourn requested review from a team, mmalerba and crisbeto as code owners May 18, 2022 22:53
@jelbourn jelbourn changed the title This change switches the cdk menu from using constructor injection to… refactor(cdk/menu): use inject for all injection May 18, 2022
@jelbourn jelbourn changed the title refactor(cdk/menu): use inject for all injection refactor(cdk/menu): use inject for all injection May 18, 2022
@jelbourn jelbourn force-pushed the menu-injection branch 2 times, most recently from 0342974 to 7403ce8 Compare May 18, 2022 23:18
protected readonly viewContainerRef = inject(ViewContainerRef);

/** The menu stack in which this menu resides. */
protected readonly menuStack: MenuStack | null = inject(MENU_STACK, InjectFlags.Optional);
Copy link
Member

Choose a reason for hiding this comment

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

Why is this optional now? It used to be required. It causes some extra null checks in the components extending this one.

Copy link
Member Author

Choose a reason for hiding this comment

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

Bad copy and paste, removed the optional

@jelbourn jelbourn force-pushed the menu-injection branch 2 times, most recently from 3a49f30 to dc200d7 Compare May 19, 2022 17:23
@jelbourn
Copy link
Member Author

CI failures are resolved. I had to disable the no bitwise operator lint since it's required for this API

@jelbourn
Copy link
Member Author

Made another update to remove some unnecessary explicit types. e.g., if you have

class MenuTrigger {
  menuAim = inject(MENU_AIM, InjectFlags.Optional);
}

it will automatically infer MenuAim | null based on the InjectionToken type and the use of the second parameter to inject

@mmalerba
Copy link
Contributor

Looks like still some lint & api failures

@jelbourn
Copy link
Member Author

Whoops, I had it green and then messed up the formatting when I made that last edit. Fixed.

mmalerba
mmalerba previously approved these changes May 20, 2022
@mmalerba mmalerba added the action: merge The PR is ready for merge by the caretaker label May 20, 2022
@mmalerba mmalerba self-assigned this May 20, 2022
This change switches the cdk menu from using constructor injection to using the `inject` function. This was enabled by angular/angular#45991. This approach has a number of advantages:

* We can add and remove injectables without changing the public constructor signature
* It reduces the boilerplate of passing arguments through `super`
* It avoids long/messy constructor blocks in favor of simpler member definitions.
* This sidesteps a common pitfall of making `@Optional` injection (`null` if not provided) as an optional parameter (`undefined` if not provided)`.
Copy link
Member

@crisbeto crisbeto left a comment

Choose a reason for hiding this comment

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

LGTM

@mmalerba mmalerba merged commit c67a299 into angular:main May 23, 2022
mmalerba pushed a commit that referenced this pull request May 23, 2022
This change switches the cdk menu from using constructor injection to using the `inject` function. This was enabled by angular/angular#45991. This approach has a number of advantages:

* We can add and remove injectables without changing the public constructor signature
* It reduces the boilerplate of passing arguments through `super`
* It avoids long/messy constructor blocks in favor of simpler member definitions.
* This sidesteps a common pitfall of making `@Optional` injection (`null` if not provided) as an optional parameter (`undefined` if not provided)`.
@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 Jun 23, 2022
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 area: cdk/menu P2 The issue is important to a large percentage of users, with a workaround refactoring This issue is related to a refactoring target: rc This PR is targeted for the next release-candidate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants