-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(effects): add provideEffects function
- Loading branch information
1 parent
53cb0ca
commit a011fe6
Showing
11 changed files
with
88 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { createAction } from '@ngrx/store'; | ||
|
||
export const ROOT_EFFECTS_INIT = '@ngrx/effects/init'; | ||
export const rootEffectsInit = createAction(ROOT_EFFECTS_INIT); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { ENVIRONMENT_INITIALIZER, inject, Provider, Type } from '@angular/core'; | ||
import { Store } from '@ngrx/store'; | ||
import { EffectsRunner } from './effects_runner'; | ||
import { EffectSources } from './effect_sources'; | ||
import { rootEffectsInit as effectsInit } from './effects_actions'; | ||
|
||
/** | ||
* Runs provided effects. Can be called at the root or feature level. | ||
* Unlike the `StoreModule.forRoot` method, this function does not need | ||
* to be called at the root level if there are no root effects. | ||
* | ||
* @usageNotes | ||
* | ||
* ### Providing root effects | ||
* | ||
* ```ts | ||
* bootstrapApplication(AppComponent, { | ||
* providers: [provideEffects([RouterEffects])], | ||
* }); | ||
* ``` | ||
* | ||
* ### Providing feature effects | ||
* | ||
* ```ts | ||
* const booksRoutes: Route[] = [ | ||
* { | ||
* path: '', | ||
* providers: [provideEffects([BooksApiEffects])], | ||
* children: [ | ||
* { path: '', component: BookListComponent }, | ||
* { path: ':id', component: BookDetailsComponent }, | ||
* ], | ||
* }, | ||
* ]; | ||
* ``` | ||
*/ | ||
export function provideEffects(effects: Type<unknown>[]): Provider[] { | ||
return [ | ||
effects, | ||
{ | ||
provide: ENVIRONMENT_INITIALIZER, | ||
multi: true, | ||
useValue: () => { | ||
const effectsRunner = inject(EffectsRunner); | ||
const effectSources = inject(EffectSources); | ||
const shouldInitEffects = !effectsRunner.isStarted; | ||
|
||
if (shouldInitEffects) { | ||
effectsRunner.start(); | ||
} | ||
|
||
for (const effectsClass of effects) { | ||
const effectsInstance = inject(effectsClass); | ||
effectSources.addEffects(effectsInstance); | ||
} | ||
|
||
if (shouldInitEffects) { | ||
const store = inject(Store); | ||
store.dispatch(effectsInit()); | ||
} | ||
}, | ||
}, | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters