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

Implementing OnInitEffects with functionalEffects #4128

Open
1 of 2 tasks
Clindbergh opened this issue Nov 14, 2023 · 4 comments
Open
1 of 2 tasks

Implementing OnInitEffects with functionalEffects #4128

Clindbergh opened this issue Nov 14, 2023 · 4 comments

Comments

@Clindbergh
Copy link

Which @ngrx/* package(s) are relevant/related to the feature request?

effects

Information

Is it possible to use createEffect to implement the OnInitEffects interface to trigger initial effects?

Describe any alternatives/workarounds you're currently using

No response

I would be willing to submit a PR to fix this issue

  • Yes
  • No
@timdeschryver
Copy link
Member

This isn't supported.
What's the use case that you have on your mind that does need this feature?

@Clindbergh
Copy link
Author

We have one file with (functional) effects:

effects.ts:

export const updateUrl = createEffect(
   ...
, { functional: true })

export const doFoo = createEffect(
   ...
, { functional: true })

export const getBar = createEffect(
   ...
, { functional: true })

We use them in our feature.module.ts:

import * as Effects from '../effects';

@NgModule({
  imports: [
    EffectsModule.forFeature(Effects),
  ],
})
export class FeatureModule {}

We would like to place the new initial effect in effect.ts:

export class LoadVacanciesEffects implements OnInitEffects {
  ngrxOnInitEffects(): Action {
    return ListActions.PageEntered();
  }
}

However this does not work, since the EffectsModule.forFeature call would now be invalid using the * import. It is therefore required to place this single Initial effect in a separate file which feels a bit awkward.

@timdeschryver
Copy link
Member

I do find it weird to add a lifecycle to the functional effects.
After thinking on this for a while, what about moving the lifecycle hook to provideEffects?

provideEffects(myEffects, { dispatchOnInit: myEffectsLoaded() })

@Hetty82
Copy link

Hetty82 commented May 2, 2024

That would help, but not the same. The cool thing about the OnInitEffect is that it only runs once, no matter how often it is provided in features. It was perfect for loading data for a feature for me. But we can always go back to using a concatLatestFrom(() => store.select(isLoaded)).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants