1+ import { ModuleWithProviders , NgModule } from '@angular/core' ;
2+ import { EntityDataModuleConfig } from './entity-data-config' ;
13import {
2- ModuleWithProviders ,
3- NgModule ,
4- Inject ,
5- Injector ,
6- InjectionToken ,
7- Optional ,
8- OnDestroy ,
9- } from '@angular/core' ;
10-
11- import {
12- Action ,
13- combineReducers ,
14- MetaReducer ,
15- ReducerManager ,
16- StoreModule ,
17- } from '@ngrx/store' ;
18-
19- import { CorrelationIdGenerator } from './utils/correlation-id-generator' ;
20- import { EntityDispatcherDefaultOptions } from './dispatchers/entity-dispatcher-default-options' ;
21- import { EntityAction } from './actions/entity-action' ;
22- import { EntityActionFactory } from './actions/entity-action-factory' ;
23- import { EntityCache } from './reducers/entity-cache' ;
24- import { EntityCacheDispatcher } from './dispatchers/entity-cache-dispatcher' ;
25- import { entityCacheSelectorProvider } from './selectors/entity-cache-selector' ;
26- import { EntityCollectionServiceElementsFactory } from './entity-services/entity-collection-service-elements-factory' ;
27- import { EntityCollectionServiceFactory } from './entity-services/entity-collection-service-factory' ;
28- import { EntityServices } from './entity-services/entity-services' ;
29- import { EntityCollection } from './reducers/entity-collection' ;
30- import { EntityCollectionCreator } from './reducers/entity-collection-creator' ;
31- import { EntityCollectionReducerFactory } from './reducers/entity-collection-reducer' ;
32- import { EntityCollectionReducerMethodsFactory } from './reducers/entity-collection-reducer-methods' ;
33- import { EntityCollectionReducerRegistry } from './reducers/entity-collection-reducer-registry' ;
34- import { EntityDispatcherFactory } from './dispatchers/entity-dispatcher-factory' ;
35- import { EntityDefinitionService } from './entity-metadata/entity-definition.service' ;
36- import { EntityMetadataMap } from './entity-metadata/entity-metadata' ;
37- import { EntityCacheReducerFactory } from './reducers/entity-cache-reducer' ;
38- import {
39- ENTITY_CACHE_NAME ,
40- ENTITY_CACHE_NAME_TOKEN ,
41- ENTITY_CACHE_META_REDUCERS ,
42- ENTITY_COLLECTION_META_REDUCERS ,
43- INITIAL_ENTITY_CACHE_STATE ,
44- } from './reducers/constants' ;
45-
46- import { DefaultLogger } from './utils/default-logger' ;
47- import { EntitySelectorsFactory } from './selectors/entity-selectors' ;
48- import { EntitySelectors$Factory } from './selectors/entity-selectors$' ;
49- import { EntityServicesBase } from './entity-services/entity-services-base' ;
50- import { EntityServicesElements } from './entity-services/entity-services-elements' ;
51- import { Logger , PLURAL_NAMES_TOKEN } from './utils/interfaces' ;
52-
53- export interface EntityDataModuleConfig {
54- entityMetadata ?: EntityMetadataMap ;
55- entityCacheMetaReducers ?: (
56- | MetaReducer < EntityCache , Action >
57- | InjectionToken < MetaReducer < EntityCache , Action > >
58- ) [ ] ;
59- entityCollectionMetaReducers ?: MetaReducer < EntityCollection , EntityAction > [ ] ;
60- // Initial EntityCache state or a function that returns that state
61- initialEntityCacheState ?: EntityCache | ( ( ) => EntityCache ) ;
62- pluralNames ?: { [ name : string ] : string } ;
63- }
4+ provideRootEntityDataWithoutEffects ,
5+ ENTITY_DATA_WITHOUT_EFFECTS_PROVIDERS ,
6+ initializeEntityDataWithoutEffects ,
7+ } from './provide-entity-data' ;
648
659/**
6610 * Module without effects or dataservices which means no HTTP calls
@@ -69,105 +13,19 @@ export interface EntityDataModuleConfig {
6913 * therefore opt-out of @ngrx/effects for entities
7014 */
7115@NgModule ( {
72- imports : [
73- StoreModule , // rely on Store feature providers rather than Store.forFeature()
74- ] ,
75- providers : [
76- CorrelationIdGenerator ,
77- EntityDispatcherDefaultOptions ,
78- EntityActionFactory ,
79- EntityCacheDispatcher ,
80- EntityCacheReducerFactory ,
81- entityCacheSelectorProvider ,
82- EntityCollectionCreator ,
83- EntityCollectionReducerFactory ,
84- EntityCollectionReducerMethodsFactory ,
85- EntityCollectionReducerRegistry ,
86- EntityCollectionServiceElementsFactory ,
87- EntityCollectionServiceFactory ,
88- EntityDefinitionService ,
89- EntityDispatcherFactory ,
90- EntitySelectorsFactory ,
91- EntitySelectors$Factory ,
92- EntityServicesElements ,
93- { provide : ENTITY_CACHE_NAME_TOKEN , useValue : ENTITY_CACHE_NAME } ,
94- { provide : EntityServices , useClass : EntityServicesBase } ,
95- { provide : Logger , useClass : DefaultLogger } ,
96- ] ,
16+ providers : [ ENTITY_DATA_WITHOUT_EFFECTS_PROVIDERS ] ,
9717} )
98- export class EntityDataModuleWithoutEffects implements OnDestroy {
99- private entityCacheFeature : any ;
100-
18+ export class EntityDataModuleWithoutEffects {
10119 static forRoot (
10220 config : EntityDataModuleConfig
10321 ) : ModuleWithProviders < EntityDataModuleWithoutEffects > {
10422 return {
10523 ngModule : EntityDataModuleWithoutEffects ,
106- providers : [
107- {
108- provide : ENTITY_CACHE_META_REDUCERS ,
109- useValue : config . entityCacheMetaReducers
110- ? config . entityCacheMetaReducers
111- : [ ] ,
112- } ,
113- {
114- provide : ENTITY_COLLECTION_META_REDUCERS ,
115- useValue : config . entityCollectionMetaReducers
116- ? config . entityCollectionMetaReducers
117- : [ ] ,
118- } ,
119- {
120- provide : PLURAL_NAMES_TOKEN ,
121- multi : true ,
122- useValue : config . pluralNames ? config . pluralNames : { } ,
123- } ,
124- ] ,
125- } ;
126- }
127-
128- constructor (
129- private reducerManager : ReducerManager ,
130- entityCacheReducerFactory : EntityCacheReducerFactory ,
131- private injector : Injector ,
132- // optional params
133- @Optional ( )
134- @Inject ( ENTITY_CACHE_NAME_TOKEN )
135- private entityCacheName : string ,
136- @Optional ( )
137- @Inject ( INITIAL_ENTITY_CACHE_STATE )
138- private initialState : any ,
139- @Optional ( )
140- @Inject ( ENTITY_CACHE_META_REDUCERS )
141- private metaReducers : (
142- | MetaReducer < EntityCache , Action >
143- | InjectionToken < MetaReducer < EntityCache , Action > >
144- ) [ ]
145- ) {
146- // Add the @ngrx /data feature to the Store's features
147- // as Store.forFeature does for StoreFeatureModule
148- const key = entityCacheName || ENTITY_CACHE_NAME ;
149-
150- initialState =
151- typeof initialState === 'function' ? initialState ( ) : initialState ;
152-
153- const reducers : MetaReducer < EntityCache , Action > [ ] = (
154- metaReducers || [ ]
155- ) . map ( ( mr ) => {
156- return mr instanceof InjectionToken ? injector . get ( mr ) : mr ;
157- } ) ;
158-
159- this . entityCacheFeature = {
160- key,
161- reducers : entityCacheReducerFactory . create ( ) ,
162- reducerFactory : combineReducers ,
163- initialState : initialState || { } ,
164- metaReducers : reducers ,
24+ providers : [ provideRootEntityDataWithoutEffects ( config ) ] ,
16525 } ;
166- reducerManager . addFeature ( this . entityCacheFeature ) ;
16726 }
16827
169- // eslint-disable-next-line @angular-eslint/contextual-lifecycle
170- ngOnDestroy ( ) {
171- this . reducerManager . removeFeature ( this . entityCacheFeature ) ;
28+ constructor ( ) {
29+ initializeEntityDataWithoutEffects ( ) ;
17230 }
17331}
0 commit comments