-
Notifications
You must be signed in to change notification settings - Fork 26.3k
feat(core): add syntactic sugar for initializers #53152
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
Conversation
b3e0ac1
to
7208d27
Compare
To keep consistency with |
99f087c
to
7058b62
Compare
Hey @lacolaco! I wonder if we really want to keep them consistent. On the other hand, app initializers are often totally independent and even provided in different places like custom provide functions. (e.g. We could end up in a What do you think? |
@yjaaidi As you says, each APP_INITIALIZER function is basically independent. I agree with your design 👍 |
@yjaaidi can you please update the commit to be a |
Done! We tried to make it ship before 17.1.0 😅 but you got us! |
7058b62
to
30fe7a6
Compare
provideAppInitializer
syntactic sugarprovideAppInitializer
syntactic sugar
30fe7a6
to
3ad1994
Compare
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.
Reviewed-for: public-api
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.
reviewed-for: public-api
What you have is fine |
Thanks @atscott, @alxhub and @JeanMeche for your time! |
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.
Feel free to address in a follow-up as these are non-blocking.
Now that the initializer is running within an injection context even without using the helper, I update the reference docs with simplified examples. What do you think? I'd prefer replacing the following function initializeApp() {
const http = inject(HttpClient);
return firstValueFrom(
http
.get("https://someUrl.com/api/user")
.pipe(tap(user => { ... }))
);
} with something simpler even though more abstract like this: function initializeApp(): Promise<unknown> {
return inject(MyService).init();
} This could be fixed in a distinct PR. |
6ba3a73
to
a501983
Compare
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.
From a docs perspective.
@@ -9,6 +9,11 @@ | |||
"version": "19.0.0", | |||
"description": "Updates ExperimentalPendingTasks to PendingTasks", | |||
"factory": "./bundles/pending-tasks#migrate" | |||
}, | |||
"provide-initializer": { |
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.
I don't think this migration is safe to run automatically. The provider functions in this PR return EnvironmentProviders
(as they should) but existing usages may not be typed in a way to handle that type.
We can exclude it and use it as an optional migration for now.
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.
Note: to make it optional you can just add “optional”: true
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.
Nice catch!
What would be the solution to make it automatic for v20?
We could detect where it is used and transform when it is safe. When it's not safe what would be the fallback? A type cast? A compact function? 🤔
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.
Thx @JeanMeche for handling this while I'm AFK 😊
This PR was merged into the repository by commit 19edf2c. The changes were merged into the following branches: main |
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.
@@ -25,7 +34,12 @@ import {isPromise, isSubscribable} from '../util/lang'; | |||
* The function is executed during the application bootstrap process, | |||
* and the needed data is available on startup. | |||
* | |||
* Note that the provided initializer is run in the injection context. | |||
* | |||
* @deprecated from v18.1.0, use provideAppInitializer instead |
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.
* @deprecated from v18.1.0, use provideAppInitializer instead | |
* @deprecated from v19.0.0, use provideAppInitializer instead |
@@ -49,6 +49,11 @@ const DEFAULT_APP_ID = 'ng'; | |||
|
|||
/** | |||
* A function that is executed when a platform is initialized. | |||
* | |||
* @deprecated from v18.1.0, use providePlatformInitializer instead |
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.
* @deprecated from v18.1.0, use providePlatformInitializer instead | |
* @deprecated from v19.0.0, use providePlatformInitializer instead |
Nice catch Pawel! |
* ``` | ||
* createEnvironmentInjector( | ||
* [ | ||
* provideEnvironmentInjector(() => { |
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.
Should this be provideEnvironmentInitializer
?
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.
Oh good one! Would you like to make a PR to fix this?
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.
Just opened #58355 :)
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. |
Add a helper provider functions to simplify the use of INITIALIZER intializer tokens
provideAppInitializer()
forAPP_INITIALIZER
provideEnvironementInitializer()
forENVIRONMENT_INITIALIZER
providePlatformInitializer()
forPLATFORM_INITIALIZER
This removes the boilerplate code for the initializers like:
in favor of:
This also includes a schematic migration.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Issue Number: N/A
What is the new behavior?
Does this PR introduce a breaking change?
Other information