Skip to content

refactor(migrations): useFactory in provide-initializer migration - #58493

Closed
cexbrayat wants to merge 1 commit into
angular:mainfrom
cexbrayat:fix/provide-initializer-migration-use-factory
Closed

cexbrayat wants to merge 1 commit into
angular:mainfrom
cexbrayat:fix/provide-initializer-migration-use-factory

Conversation

@cexbrayat

Copy link
Copy Markdown
Member

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.dev application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

The following provider:

{
  provide: APP_INITIALIZER,
  useFactory: (initService: InitService) => {
    return () => initService.init()
  },
  deps: [InitService],
  multi: true
}

was migrated in:

provideAppInitializer(() => { return ((initService: InitService) => {
  return () => initService.init()
})(inject(InitService)); })

This doesn't compile because there is an extra function:

✘ [ERROR] TS2345: Argument of type '() => () => void' is not assignable to parameter of type '() => void | Observable<unknown> | Promise<unknown>'.
  Type '() => void' is not assignable to type 'void | Observable<unknown> | Promise<unknown>'. [plugin angular-compiler]

    src/app/app.config.ts:7:26:
      7 │ ...ovideAppInitializer(() => { return ((initService: InitService) => {

What is the new behavior?

It now migrates the provider in:

provideAppInitializer(((initService: InitService) => {
  return () => initService.init()
})(inject(InitService)))

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

The following provider:

```ts
{
  provide: APP_INITIALIZER,
  useFactory: (initService: InitService) => {
    return () => initService.init()
  },
  deps: [InitService],
  multi: true
}
```

was migrated into:

```ts
provideAppInitializer(() => { return ((initService: InitService) => {
  return () => initService.init()
})(inject(InitService)); })
```

This doesn't compile because there is an extra function:

```
✘ [ERROR] TS2345: Argument of type '() => () => void' is not assignable to parameter of type '() => void | Observable<unknown> | Promise<unknown>'.
  Type '() => void' is not assignable to type 'void | Observable<unknown> | Promise<unknown>'. [plugin angular-compiler]

    src/app/app.config.ts:7:26:
      7 │ ...ovideAppInitializer(() => { return ((initService: InitService) => {
```

It now migrates the provider into:

```ts
provideAppInitializer(((initService: InitService) => {
  return () => initService.init()
})(inject(InitService)))
```
@pullapprove
pullapprove Bot requested a review from devversion November 4, 2024 08:56
@angular-robot angular-robot Bot added the area: migrations Issues related to `ng update`/`ng generate` migrations label Nov 4, 2024
@ngbot ngbot Bot added this to the Backlog milestone Nov 4, 2024
@devversion devversion added action: merge The PR is ready for merge by the caretaker action: review The PR is still awaiting reviews from at least one requested reviewer target: rc This PR is targeted for the next release-candidate labels Nov 4, 2024
@devversion

Copy link
Copy Markdown
Member

cc. @yjaaidi

@devversion devversion removed the action: review The PR is still awaiting reviews from at least one requested reviewer label Nov 4, 2024
@yjaaidi

yjaaidi commented Nov 4, 2024

Copy link
Copy Markdown
Contributor

Nice catch @cexbrayat ! Thanks for the fix

@pkozlowski-opensource

Copy link
Copy Markdown
Member

This PR was merged into the repository by commit a116a64.

The changes were merged into the following branches: main, 19.0.x

pkozlowski-opensource pushed a commit that referenced this pull request Nov 4, 2024
…8493)

The following provider:

```ts
{
  provide: APP_INITIALIZER,
  useFactory: (initService: InitService) => {
    return () => initService.init()
  },
  deps: [InitService],
  multi: true
}
```

was migrated into:

```ts
provideAppInitializer(() => { return ((initService: InitService) => {
  return () => initService.init()
})(inject(InitService)); })
```

This doesn't compile because there is an extra function:

```
✘ [ERROR] TS2345: Argument of type '() => () => void' is not assignable to parameter of type '() => void | Observable<unknown> | Promise<unknown>'.
  Type '() => void' is not assignable to type 'void | Observable<unknown> | Promise<unknown>'. [plugin angular-compiler]

    src/app/app.config.ts:7:26:
      7 │ ...ovideAppInitializer(() => { return ((initService: InitService) => {
```

It now migrates the provider into:

```ts
provideAppInitializer(((initService: InitService) => {
  return () => initService.init()
})(inject(InitService)))
```

PR Close #58493
@yjaaidi

yjaaidi commented Nov 5, 2024

Copy link
Copy Markdown
Contributor

Oh! Sorry! I'm afk and still didn't get a chance to look at this closely.

I'm afraid that this isn't the right fix. It's hard to read on my phone but it looks like the inject is not running in an injection context.

The function initially returned in the outer arrow function should have been called but still inside the outer arrow:

provideAppInitializer(() => { return ((initService: InitService) => {
  return () => initService.init()
})(inject(InitService))(); })
//                     ^

@yjaaidi

yjaaidi commented Nov 6, 2024

Copy link
Copy Markdown
Contributor

I just submitted a fix that should also improve readability.
#58518

@angular-automatic-lock-bot

Copy link
Copy Markdown

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 Dec 7, 2024
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: migrations Issues related to `ng update`/`ng generate` migrations target: rc This PR is targeted for the next release-candidate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants