-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
store_module.ts
287 lines (271 loc) · 8.1 KB
/
store_module.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import {
NgModule,
Inject,
ModuleWithProviders,
OnDestroy,
InjectionToken,
Injector,
} from '@angular/core';
import {
Action,
ActionReducer,
ActionReducerMap,
ActionReducerFactory,
StoreFeature,
InitialState,
MetaReducer,
RuntimeChecks,
} from './models';
import { compose, combineReducers, createReducerFactory } from './utils';
import {
INITIAL_STATE,
INITIAL_REDUCERS,
_INITIAL_REDUCERS,
REDUCER_FACTORY,
_REDUCER_FACTORY,
STORE_FEATURES,
_INITIAL_STATE,
META_REDUCERS,
_STORE_REDUCERS,
FEATURE_REDUCERS,
_FEATURE_REDUCERS,
_FEATURE_REDUCERS_TOKEN,
_STORE_FEATURES,
_FEATURE_CONFIGS,
USER_PROVIDED_META_REDUCERS,
_RESOLVED_META_REDUCERS,
} from './tokens';
import { ACTIONS_SUBJECT_PROVIDERS, ActionsSubject } from './actions_subject';
import {
REDUCER_MANAGER_PROVIDERS,
ReducerManager,
ReducerObservable,
} from './reducer_manager';
import {
SCANNED_ACTIONS_SUBJECT_PROVIDERS,
ScannedActionsSubject,
} from './scanned_actions_subject';
import { STATE_PROVIDERS } from './state';
import { STORE_PROVIDERS, Store } from './store';
import { provideRuntimeChecks } from './runtime_checks';
@NgModule({})
export class StoreRootModule {
constructor(
actions$: ActionsSubject,
reducer$: ReducerObservable,
scannedActions$: ScannedActionsSubject,
store: Store<any>
) {}
}
@NgModule({})
export class StoreFeatureModule implements OnDestroy {
constructor(
@Inject(_STORE_FEATURES) private features: StoreFeature<any, any>[],
@Inject(FEATURE_REDUCERS) private featureReducers: ActionReducerMap<any>[],
private reducerManager: ReducerManager,
root: StoreRootModule
) {
const feats = features.map((feature, index) => {
const featureReducerCollection = featureReducers.shift();
const reducers = featureReducerCollection /*TODO(#823)*/![index];
return {
...feature,
reducers,
initialState: _initialStateFactory(feature.initialState),
};
});
reducerManager.addFeatures(feats);
}
ngOnDestroy() {
this.reducerManager.removeFeatures(this.features);
}
}
export interface StoreConfig<T, V extends Action = Action> {
initialState?: InitialState<T>;
reducerFactory?: ActionReducerFactory<T, V>;
metaReducers?: MetaReducer<T, V>[];
}
export interface RootStoreConfig<T, V extends Action = Action>
extends StoreConfig<T, V> {
runtimeChecks?: Partial<RuntimeChecks>;
}
@NgModule({})
export class StoreModule {
static forRoot<T, V extends Action = Action>(
reducers: ActionReducerMap<T, V> | InjectionToken<ActionReducerMap<T, V>>,
config?: RootStoreConfig<T, V>
): ModuleWithProviders<StoreRootModule>;
static forRoot(
reducers:
| ActionReducerMap<any, any>
| InjectionToken<ActionReducerMap<any, any>>,
config: RootStoreConfig<any, any> = {}
): ModuleWithProviders<StoreRootModule> {
return {
ngModule: StoreRootModule,
providers: [
{ provide: _INITIAL_STATE, useValue: config.initialState },
{
provide: INITIAL_STATE,
useFactory: _initialStateFactory,
deps: [_INITIAL_STATE],
},
{ provide: _INITIAL_REDUCERS, useValue: reducers },
{
provide: _STORE_REDUCERS,
useExisting:
reducers instanceof InjectionToken ? reducers : _INITIAL_REDUCERS,
},
{
provide: INITIAL_REDUCERS,
deps: [Injector, _INITIAL_REDUCERS, [new Inject(_STORE_REDUCERS)]],
useFactory: _createStoreReducers,
},
{
provide: USER_PROVIDED_META_REDUCERS,
useValue: config.metaReducers ? config.metaReducers : [],
},
{
provide: _RESOLVED_META_REDUCERS,
deps: [META_REDUCERS, USER_PROVIDED_META_REDUCERS],
useFactory: _concatMetaReducers,
},
{
provide: _REDUCER_FACTORY,
useValue: config.reducerFactory
? config.reducerFactory
: combineReducers,
},
{
provide: REDUCER_FACTORY,
deps: [_REDUCER_FACTORY, _RESOLVED_META_REDUCERS],
useFactory: createReducerFactory,
},
ACTIONS_SUBJECT_PROVIDERS,
REDUCER_MANAGER_PROVIDERS,
SCANNED_ACTIONS_SUBJECT_PROVIDERS,
STATE_PROVIDERS,
STORE_PROVIDERS,
provideRuntimeChecks(config.runtimeChecks),
],
};
}
static forFeature<T, V extends Action = Action>(
featureName: string,
reducers: ActionReducerMap<T, V> | InjectionToken<ActionReducerMap<T, V>>,
config?: StoreConfig<T, V> | InjectionToken<StoreConfig<T, V>>
): ModuleWithProviders<StoreFeatureModule>;
static forFeature<T, V extends Action = Action>(
featureName: string,
reducer: ActionReducer<T, V> | InjectionToken<ActionReducer<T, V>>,
config?: StoreConfig<T, V> | InjectionToken<StoreConfig<T, V>>
): ModuleWithProviders<StoreFeatureModule>;
static forFeature(
featureName: string,
reducers:
| ActionReducerMap<any, any>
| InjectionToken<ActionReducerMap<any, any>>
| ActionReducer<any, any>
| InjectionToken<ActionReducer<any, any>>,
config: StoreConfig<any, any> | InjectionToken<StoreConfig<any, any>> = {}
): ModuleWithProviders<StoreFeatureModule> {
return {
ngModule: StoreFeatureModule,
providers: [
{
provide: _FEATURE_CONFIGS,
multi: true,
useValue: config,
},
{
provide: STORE_FEATURES,
multi: true,
useValue: {
key: featureName,
reducerFactory:
!(config instanceof InjectionToken) && config.reducerFactory
? config.reducerFactory
: combineReducers,
metaReducers:
!(config instanceof InjectionToken) && config.metaReducers
? config.metaReducers
: [],
initialState:
!(config instanceof InjectionToken) && config.initialState
? config.initialState
: undefined,
},
},
{
provide: _STORE_FEATURES,
deps: [Injector, _FEATURE_CONFIGS, STORE_FEATURES],
useFactory: _createFeatureStore,
},
{ provide: _FEATURE_REDUCERS, multi: true, useValue: reducers },
{
provide: _FEATURE_REDUCERS_TOKEN,
multi: true,
useExisting:
reducers instanceof InjectionToken ? reducers : _FEATURE_REDUCERS,
},
{
provide: FEATURE_REDUCERS,
multi: true,
deps: [
Injector,
_FEATURE_REDUCERS,
[new Inject(_FEATURE_REDUCERS_TOKEN)],
],
useFactory: _createFeatureReducers,
},
],
};
}
}
export function _createStoreReducers(
injector: Injector,
reducers: ActionReducerMap<any, any>
) {
return reducers instanceof InjectionToken ? injector.get(reducers) : reducers;
}
export function _createFeatureStore(
injector: Injector,
configs: StoreConfig<any, any>[] | InjectionToken<StoreConfig<any, any>>[],
featureStores: StoreFeature<any, any>[]
) {
return featureStores.map((feat, index) => {
if (configs[index] instanceof InjectionToken) {
const conf = injector.get(configs[index]);
return {
key: feat.key,
reducerFactory: conf.reducerFactory
? conf.reducerFactory
: combineReducers,
metaReducers: conf.metaReducers ? conf.metaReducers : [],
initialState: conf.initialState,
};
}
return feat;
});
}
export function _createFeatureReducers(
injector: Injector,
reducerCollection: ActionReducerMap<any, any>[]
) {
const reducers = reducerCollection.map(reducer => {
return reducer instanceof InjectionToken ? injector.get(reducer) : reducer;
});
return reducers;
}
export function _initialStateFactory(initialState: any): any {
if (typeof initialState === 'function') {
return initialState();
}
return initialState;
}
export function _concatMetaReducers(
metaReducers: MetaReducer[],
userProvidedMetaReducers: MetaReducer[]
): MetaReducer[] {
return metaReducers.concat(userProvidedMetaReducers);
}