-
-
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.
fix: create injection token inside provideComponentStore
- Loading branch information
1 parent
867d25f
commit 86d19dd
Showing
4 changed files
with
86 additions
and
61 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './component-store'; | ||
export * from './tap-response'; | ||
export * from './lifecycle_hooks'; |
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,49 @@ | ||
import { Provider, InjectionToken, Type, inject } from '@angular/core'; | ||
import { take } from 'rxjs'; | ||
import { ComponentStore } from './component-store'; | ||
|
||
export interface OnStoreInit { | ||
readonly ngrxOnStoreInit: () => void; | ||
} | ||
|
||
export interface OnStateInit { | ||
readonly ngrxOnStateInit: () => void; | ||
} | ||
|
||
export function isOnStoreInitDefined(cs: unknown): cs is OnStoreInit { | ||
return typeof (cs as OnStoreInit).ngrxOnStoreInit === 'function'; | ||
} | ||
|
||
export function isOnStateInitDefined(cs: unknown): cs is OnStateInit { | ||
return typeof (cs as OnStateInit).ngrxOnStateInit === 'function'; | ||
} | ||
|
||
export function provideComponentStore( | ||
componentStoreClass: Type<ComponentStore<any>> | ||
): Provider[] { | ||
const CS_WITH_HOOKS = new InjectionToken<ComponentStore<any>>( | ||
'@ngrx/component-store ComponentStore with Hooks' | ||
); | ||
|
||
return [ | ||
{ provide: CS_WITH_HOOKS, useClass: componentStoreClass }, | ||
{ | ||
provide: componentStoreClass, | ||
useFactory: () => { | ||
const componentStore = inject(CS_WITH_HOOKS); | ||
|
||
if (isOnStoreInitDefined(componentStore)) { | ||
componentStore.ngrxOnStoreInit(); | ||
} | ||
|
||
if (isOnStateInitDefined(componentStore)) { | ||
componentStore.state$ | ||
.pipe(take(1)) | ||
.subscribe(() => componentStore.ngrxOnStateInit()); | ||
} | ||
|
||
return componentStore; | ||
}, | ||
}, | ||
]; | ||
} |