@ngrx-traits/core 12.1.2 → 13.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/cache/cache.service.d.ts +2 -1
  2. package/esm2020/cache/cache.actions.mjs +6 -0
  3. package/esm2020/cache/cache.models.mjs +30 -0
  4. package/esm2020/cache/cache.module.mjs +18 -0
  5. package/esm2020/cache/cache.reducer.mjs +138 -0
  6. package/esm2020/cache/cache.selectors.mjs +5 -0
  7. package/esm2020/cache/cache.service.mjs +72 -0
  8. package/esm2020/cache/index.mjs +7 -0
  9. package/esm2020/create-entity-feature.mjs +404 -0
  10. package/esm2020/index.mjs +7 -0
  11. package/esm2020/local-store/disable-local-trait-effects.token.mjs +7 -0
  12. package/esm2020/local-store/index.mjs +3 -0
  13. package/esm2020/local-store/traits-local-store.mjs +147 -0
  14. package/esm2020/model.mjs +2 -0
  15. package/esm2020/ngrx-traits-core.mjs +5 -0
  16. package/esm2020/public_api.mjs +2 -0
  17. package/esm2020/testing/index.mjs +2 -0
  18. package/esm2020/testing/ngrx-traits-core-testing.mjs +5 -0
  19. package/esm2020/testing/provide-mock-local-traits.mjs +36 -0
  20. package/esm2020/testing/public_api.mjs +2 -0
  21. package/esm2020/trait-effect.mjs +32 -0
  22. package/esm2020/util.mjs +70 -0
  23. package/fesm2015/{ngrx-traits-core-testing.js → ngrx-traits-core-testing.mjs} +1 -1
  24. package/fesm2015/ngrx-traits-core-testing.mjs.map +1 -0
  25. package/fesm2015/{ngrx-traits-core.js → ngrx-traits-core.mjs} +13 -13
  26. package/fesm2015/ngrx-traits-core.mjs.map +1 -0
  27. package/{esm2015/testing/provide-mock-local-traits.js → fesm2020/ngrx-traits-core-testing.mjs} +12 -8
  28. package/fesm2020/ngrx-traits-core-testing.mjs.map +1 -0
  29. package/fesm2020/ngrx-traits-core.mjs +915 -0
  30. package/fesm2020/ngrx-traits-core.mjs.map +1 -0
  31. package/package.json +36 -14
  32. package/src/lib/cache/README.md +100 -0
  33. package/src/lib/local-store/README.md +172 -0
  34. package/testing/package.json +5 -5
  35. package/bundles/ngrx-traits-core-testing.umd.js +0 -52
  36. package/bundles/ngrx-traits-core-testing.umd.js.map +0 -1
  37. package/bundles/ngrx-traits-core.umd.js +0 -1442
  38. package/bundles/ngrx-traits-core.umd.js.map +0 -1
  39. package/esm2015/cache/cache.actions.js +0 -6
  40. package/esm2015/cache/cache.actions.js.map +0 -1
  41. package/esm2015/cache/cache.models.js +0 -31
  42. package/esm2015/cache/cache.models.js.map +0 -1
  43. package/esm2015/cache/cache.module.js +0 -18
  44. package/esm2015/cache/cache.module.js.map +0 -1
  45. package/esm2015/cache/cache.reducer.js +0 -141
  46. package/esm2015/cache/cache.reducer.js.map +0 -1
  47. package/esm2015/cache/cache.selectors.js +0 -5
  48. package/esm2015/cache/cache.selectors.js.map +0 -1
  49. package/esm2015/cache/cache.service.js +0 -72
  50. package/esm2015/cache/cache.service.js.map +0 -1
  51. package/esm2015/cache/index.js +0 -7
  52. package/esm2015/cache/index.js.map +0 -1
  53. package/esm2015/create-entity-feature.js +0 -409
  54. package/esm2015/create-entity-feature.js.map +0 -1
  55. package/esm2015/index.js +0 -7
  56. package/esm2015/index.js.map +0 -1
  57. package/esm2015/local-store/disable-local-trait-effects.token.js +0 -7
  58. package/esm2015/local-store/disable-local-trait-effects.token.js.map +0 -1
  59. package/esm2015/local-store/index.js +0 -3
  60. package/esm2015/local-store/index.js.map +0 -1
  61. package/esm2015/local-store/traits-local-store.js +0 -148
  62. package/esm2015/local-store/traits-local-store.js.map +0 -1
  63. package/esm2015/model.js +0 -2
  64. package/esm2015/model.js.map +0 -1
  65. package/esm2015/ngrx-traits-core.js +0 -5
  66. package/esm2015/ngrx-traits-core.js.map +0 -1
  67. package/esm2015/public_api.js +0 -2
  68. package/esm2015/public_api.js.map +0 -1
  69. package/esm2015/testing/index.js +0 -2
  70. package/esm2015/testing/index.js.map +0 -1
  71. package/esm2015/testing/ngrx-traits-core-testing.js +0 -5
  72. package/esm2015/testing/ngrx-traits-core-testing.js.map +0 -1
  73. package/esm2015/testing/provide-mock-local-traits.js.map +0 -1
  74. package/esm2015/testing/public_api.js +0 -2
  75. package/esm2015/testing/public_api.js.map +0 -1
  76. package/esm2015/trait-effect.js +0 -32
  77. package/esm2015/trait-effect.js.map +0 -1
  78. package/esm2015/util.js +0 -67
  79. package/esm2015/util.js.map +0 -1
  80. package/fesm2015/ngrx-traits-core-testing.js.map +0 -1
  81. package/fesm2015/ngrx-traits-core.js.map +0 -1
@@ -0,0 +1,915 @@
1
+ import * as i2 from '@ngrx/store';
2
+ import { createFeatureSelector, createReducer, createSelector, combineReducers, ReducerManager, Store, createAction, props, on, StoreModule } from '@ngrx/store';
3
+ import * as i0 from '@angular/core';
4
+ import { Injectable, InjectionToken, Injector, NgModule } from '@angular/core';
5
+ import * as i1 from '@ngrx/effects';
6
+ import { ofType, EffectSources, Actions } from '@ngrx/effects';
7
+ import { takeUntil, first, concatMap, tap } from 'rxjs/operators';
8
+ import { of } from 'rxjs';
9
+
10
+ function insertIf(condition, getElement) {
11
+ return condition ? [getElement()] : [];
12
+ }
13
+ function toMap(a) {
14
+ return a.reduce((acum, value) => {
15
+ acum[value] = true;
16
+ return acum;
17
+ }, {});
18
+ }
19
+ function capitalize(name) {
20
+ return name.charAt(0).toUpperCase() + name.slice(1);
21
+ }
22
+ function camelCaseToSentence(text) {
23
+ const result = text.replace(/([A-Z])/g, ' $1');
24
+ return result.charAt(0).toUpperCase() + result.slice(1);
25
+ }
26
+ /**
27
+ * Set propertyReducer in sourceReducer in a property of the source state,
28
+ * @param sourceReducer
29
+ * @param property
30
+ * @param propertyReducer
31
+ *
32
+ * @example
33
+ *
34
+ * const newReducer = setPropertyReducer(productsReducer, 'selectedProduct', selectedProductReducer)
35
+ */
36
+ function setPropertyReducer(sourceReducer, property, propertyReducer) {
37
+ return function reducer(state, action) {
38
+ const sourceState = sourceReducer(state, action);
39
+ return {
40
+ ...sourceState,
41
+ [property]: propertyReducer(sourceState[property], action),
42
+ };
43
+ };
44
+ }
45
+ /**
46
+ * Set propertyReducers in sourceReducer each in a property of the source state,
47
+ * @param sourceReducer
48
+ * @param property
49
+ * @param propertyReducer
50
+ *
51
+ * @example
52
+ *
53
+ * const newReducer = setPropertyReducer(productsReducer,
54
+ * {
55
+ * selectedProduct: selectedProductReducer
56
+ * favoriteProduct: favoriteProductReducer
57
+ * })
58
+ */
59
+ function setPropertiesReducer(sourceReducer, propertiesReducers) {
60
+ return function reducer(state, action) {
61
+ const newState = { ...sourceReducer(state, action) };
62
+ for (const property in propertiesReducers) {
63
+ newState[property] = propertiesReducers[property](newState[property], action);
64
+ }
65
+ return newState;
66
+ };
67
+ }
68
+ /**
69
+ * joins two reducers so the work in the same state
70
+ * @param firstReducer
71
+ * @param secondReducer
72
+ */
73
+ function joinReducers(firstReducer, secondReducer) {
74
+ return function reducer(state, action) {
75
+ const sourceState = firstReducer(state, action);
76
+ return secondReducer(sourceState, action);
77
+ };
78
+ }
79
+
80
+ /**
81
+ * Creates a function that when execute will combine all the traits, and return a EntityFeatureFactory
82
+ * which combines all the traits actions, selectors , reducers and effects,
83
+ * the names param will replace any action and selector with the word Entity or Entities,
84
+ * with the corresponding entityName and entitiesName param (entityName+'s' if entitiesName is omitted).
85
+ * @param namesConfig - Optional Names for entities
86
+ * @param namesConfig.entityName - singular name for entity
87
+ * @param [namesConfig.entitiesName] - plural name for entities, defaults to entityName + 's'
88
+ * @param traits set of traits to be combined
89
+ *
90
+ * @example
91
+ *
92
+ * const featureFactory = createEntityFeatureFactory(
93
+ * { entityName: 'product' },
94
+ * addLoadEntitiesTrait<Product>(),
95
+ * addSelectEntityTrait<Product>(),
96
+ * addAsyncActionTrait({
97
+ * name: 'checkout',
98
+ * actionSuccessProps: props<{ orderId: string }>(),
99
+ * })
100
+ * );
101
+ *
102
+ * export const productsFeature = featureFactory({
103
+ * actionsGroupKey: '[Products]',
104
+ * featureSelector: 'products',
105
+ * });
106
+ */
107
+ function createEntityFeatureFactory(namesOrFactory, ...traits) {
108
+ return ((config) => {
109
+ const { entityName, entitiesName } = 'entityName' in namesOrFactory
110
+ ? namesOrFactory
111
+ : { entityName: 'Entity', entitiesName: 'Entities' };
112
+ const singular = capitalize(entityName);
113
+ const plural = entitiesName
114
+ ? capitalize(entitiesName)
115
+ : capitalize(entityName + 's');
116
+ const sortedTraits = sortTraits('entityName' in namesOrFactory ? [...traits] : [namesOrFactory, ...traits]);
117
+ const allConfigs = buildAllConfigs(sortedTraits);
118
+ const allActions = buildAllActions(sortedTraits, config.actionsGroupKey, singular, plural, allConfigs);
119
+ const allSelectors = buildAllSelectors(sortedTraits, allConfigs);
120
+ const allMutators = buildAllMutators(sortedTraits, allSelectors, allConfigs);
121
+ const initialState = buildInitialState(sortedTraits, allConfigs);
122
+ const reducer = buildReducer(sortedTraits, initialState, allActions, allSelectors, allMutators, allConfigs);
123
+ const featureSelector = typeof config.featureSelector === 'string'
124
+ ? createFeatureSelector(config.featureSelector)
125
+ : config.featureSelector;
126
+ const allFeatureSelectors = allSelectors && getSelectorsForFeature(featureSelector, allSelectors);
127
+ const allEffects = buildAllEffects(sortedTraits, allActions, allFeatureSelectors, allConfigs);
128
+ return {
129
+ actions: entityName
130
+ ? renameProps(allActions, singular, plural)
131
+ : allActions,
132
+ selectors: entityName
133
+ ? renameProps(allFeatureSelectors, singular, plural)
134
+ : allSelectors,
135
+ initialState,
136
+ reducer: reducer ?? createReducer(initialState),
137
+ effects: allEffects,
138
+ };
139
+ });
140
+ }
141
+ function renameProps(target, entityName, entitiesName) {
142
+ const result = {};
143
+ for (const [key, value] of Object.entries(target)) {
144
+ const newKey = key
145
+ .replace('Entities', entitiesName)
146
+ .replace('Entity', entityName);
147
+ result[newKey] = value;
148
+ }
149
+ return result;
150
+ }
151
+ function sortTraits(traits) {
152
+ const sortedTraits = [];
153
+ for (let i = 0; i < traits.length; i++) {
154
+ const trait = traits[i];
155
+ if (!trait.depends?.length) {
156
+ sortedTraits.push(trait);
157
+ continue;
158
+ }
159
+ if (trait.depends.length > 1)
160
+ for (const d of trait.depends) {
161
+ const isTraitPresent = traits.some((tr) => tr.key === d);
162
+ if (isTraitPresent) {
163
+ trait.depends = [d];
164
+ break;
165
+ }
166
+ }
167
+ if (trait.depends.length > 1)
168
+ throw Error('could not find dependencies ' + trait.depends.join(' '));
169
+ const isDependencyAlreadyAdded = sortedTraits.some((tr) => tr.key === trait?.depends?.[0]);
170
+ if (isDependencyAlreadyAdded)
171
+ sortedTraits.push(trait);
172
+ else {
173
+ // move trait to the end
174
+ delete traits[i];
175
+ traits.push(trait);
176
+ }
177
+ }
178
+ return sortedTraits;
179
+ }
180
+ function buildAllConfigs(sortedTraits) {
181
+ return sortedTraits.reduce((acc, factory) => {
182
+ acc[factory.key] = factory.config;
183
+ return acc;
184
+ }, {});
185
+ }
186
+ function buildAllActions(sortedTraits, actionsGroupKey, entityName, entitiesName, allConfigs) {
187
+ return sortedTraits.reduce((previousResult, factory) => {
188
+ let result = factory?.actions?.({
189
+ actionsGroupKey: actionsGroupKey,
190
+ entityName,
191
+ entitiesName,
192
+ allConfigs,
193
+ }) ?? {};
194
+ result = previousResult ? { ...previousResult, ...result } : result;
195
+ return result;
196
+ }, {});
197
+ }
198
+ function buildAllSelectors(sortedTraits, allConfigs) {
199
+ return sortedTraits.reduce((previousResult, factory) => {
200
+ let result = factory?.selectors?.({
201
+ previousSelectors: previousResult,
202
+ allConfigs,
203
+ }) ?? {};
204
+ result = previousResult ? { ...previousResult, ...result } : result;
205
+ return result;
206
+ }, {});
207
+ }
208
+ function buildAllMutators(sortedTraits, allSelectors, allConfigs) {
209
+ return (sortedTraits.reduce((previousResult, factory) => {
210
+ let result = factory?.mutators?.({
211
+ allSelectors: allSelectors,
212
+ previousMutators: previousResult,
213
+ allConfigs,
214
+ }) ?? {};
215
+ result = previousResult ? { ...previousResult, ...result } : result;
216
+ return result;
217
+ }, {}) || {});
218
+ }
219
+ function buildInitialState(sortedTraits, allConfigs) {
220
+ return sortedTraits.reduce((previousResult, factory) => {
221
+ const result = factory?.initialState?.({
222
+ previousInitialState: previousResult,
223
+ allConfigs,
224
+ }) ??
225
+ previousResult ??
226
+ {};
227
+ return result;
228
+ }, {});
229
+ }
230
+ function buildReducer(sortedTraits, initialState, allActions, allSelectors, allMutators, allConfigs) {
231
+ return sortedTraits.reduce((previousResult, factory) => {
232
+ const result = factory?.reducer?.({
233
+ initialState,
234
+ allActions,
235
+ allSelectors,
236
+ allMutators,
237
+ allConfigs,
238
+ });
239
+ return result && previousResult
240
+ ? (state = initialState, action) => {
241
+ const aState = previousResult(state, action);
242
+ return result(aState, action);
243
+ }
244
+ : result ?? previousResult;
245
+ }, undefined);
246
+ }
247
+ function buildAllEffects(sortedTraits, allActions, allFeatureSelectors, allConfigs) {
248
+ return sortedTraits.reduce((previousResult, factory) => {
249
+ let result = factory?.effects?.({
250
+ allActions,
251
+ allSelectors: allFeatureSelectors,
252
+ allConfigs,
253
+ }) ?? [];
254
+ result = previousResult ? [...previousResult, ...result] : result;
255
+ return result;
256
+ }, []);
257
+ }
258
+ function getSelectorsForFeature(featureSelect, selectors) {
259
+ const ss = {};
260
+ for (const prop in selectors) {
261
+ ss[prop] = createSelector(featureSelect, selectors[prop]);
262
+ }
263
+ return ss;
264
+ }
265
+ /**
266
+ * Combine a map entityFeatureFactories into one,
267
+ * grouping the actions and selectors by the key of the respective entityFeatureFactory
268
+ * @param traitFactoriesMap
269
+ *
270
+ * @example
271
+ *
272
+ * const clientsFeatureFactory = createEntityFeatureFactory(
273
+ * { entityName: 'client', entitiesName: 'clients' },
274
+ * addLoadEntitiesTrait<Client>(),
275
+ * addCrudEntitiesTrait<Client>()
276
+ * );
277
+ *
278
+ * const productOrderFeatureFactory = createEntityFeatureFactory(
279
+ * { entityName: 'productOrder' },
280
+ * addLoadEntitiesTrait<ProductOrder>(),
281
+ * addSelectEntitiesTrait<ProductOrder>()
282
+ * );
283
+ *
284
+ * const productFeatureFactory = createEntityFeatureFactory(
285
+ * { entityName: 'product' },
286
+ * addLoadEntitiesTrait<Product>(),
287
+ * addSelectEntitiesTrait<Product>()
288
+ * );
289
+ *
290
+ * const productCombinedFactory = combineEntityFeatures({
291
+ * products: productFeatureFactory,
292
+ * productOrders: productOrderFeatureFactory,
293
+ * clients: clientsFeatureFactory,
294
+ * });
295
+ *
296
+ * const combinedFeature = productCombinedFactory({
297
+ * actionsGroupKey: '[Combined]',
298
+ * featureSelector: 'combined',
299
+ * });
300
+ *
301
+ * combinedFeature.actions.client.loadClients();
302
+ * combinedFeature.actions.product.loadProducts();
303
+ */
304
+ function combineEntityFeatures(traitFactoriesMap) {
305
+ return ((config) => {
306
+ const featureSelector = typeof config.featureSelector === 'string'
307
+ ? createFeatureSelector(config.featureSelector)
308
+ : config.featureSelector;
309
+ const actions = {};
310
+ const selectors = {};
311
+ const reducers = {};
312
+ let effects = [];
313
+ for (const [key, entityFeatureFactory] of Object.entries(traitFactoriesMap)) {
314
+ const selector = createSelector(featureSelector, (state) => state[key]);
315
+ const featureTraits = entityFeatureFactory({
316
+ actionsGroupKey: config.actionsGroupKey,
317
+ featureSelector: selector,
318
+ });
319
+ actions[key] = featureTraits.actions;
320
+ selectors[key] = featureTraits.selectors;
321
+ reducers[key] = featureTraits.reducer;
322
+ effects = [...effects, ...featureTraits.effects];
323
+ }
324
+ return {
325
+ actions,
326
+ selectors,
327
+ reducer: combineReducers(reducers),
328
+ effects,
329
+ };
330
+ });
331
+ }
332
+ /**
333
+ * Mix a map entityFeatureFactories into one, different from combine the actions and selectors a mix, not group by key like in combine, the keys are still use
334
+ * internal in the reducers and selector to separate the state
335
+ * @param traitFactoriesMap
336
+ *
337
+ * @example
338
+ *
339
+ * const clientsFeatureFactory = createEntityFeatureFactory(
340
+ * { entityName: 'client', entitiesName: 'clients' },
341
+ * addLoadEntitiesTrait<Client>(),
342
+ * addCrudEntitiesTrait<Client>()
343
+ * );
344
+ *
345
+ * const productOrderFeatureFactory = createEntityFeatureFactory(
346
+ * { entityName: 'productOrder' },
347
+ * addLoadEntitiesTrait<ProductOrder>(),
348
+ * addSelectEntitiesTrait<ProductOrder>()
349
+ * );
350
+ *
351
+ * const productFeatureFactory = createEntityFeatureFactory(
352
+ * { entityName: 'product' },
353
+ * addLoadEntitiesTrait<Product>(),
354
+ * addSelectEntitiesTrait<Product>()
355
+ * );
356
+ *
357
+ * const productMixedFactory = mixEntityFeatures({
358
+ * products: productFeatureFactory,
359
+ * productOrders: productOrderFeatureFactory,
360
+ * clients: clientsFeatureFactory,
361
+ * });
362
+ *
363
+ * const mixedFeature = productMixedFactory({
364
+ * actionsGroupKey: '[Mixed]',
365
+ * featureSelector: 'mixed',
366
+ * });
367
+ * mixedFeature.actions.loadClients();
368
+ * mixedFeature.actions.loadProducts();
369
+ *
370
+ */
371
+ function mixEntityFeatures(traitFactoriesMap) {
372
+ return ((config) => {
373
+ const featureSelector = typeof config.featureSelector === 'string'
374
+ ? createFeatureSelector(config.featureSelector)
375
+ : config.featureSelector;
376
+ let actions = {};
377
+ let selectors = {};
378
+ const reducers = {};
379
+ let effects = [];
380
+ for (const [key, entityFeatureFactory] of Object.entries(traitFactoriesMap)) {
381
+ const selector = createSelector(featureSelector, (state) => state[key]);
382
+ const featureTraits = entityFeatureFactory({
383
+ actionsGroupKey: config.actionsGroupKey,
384
+ featureSelector: selector,
385
+ });
386
+ actions = { ...actions, ...featureTraits.actions };
387
+ selectors = { ...selectors, ...featureTraits.selectors };
388
+ reducers[key] = featureTraits.reducer;
389
+ effects = [...effects, ...featureTraits.effects];
390
+ }
391
+ return {
392
+ actions,
393
+ selectors,
394
+ reducer: combineReducers(reducers),
395
+ effects,
396
+ };
397
+ });
398
+ }
399
+ /**
400
+ * Combines targetTraitFactory with the traitFactoriesMap using the keys as props for the targetTraitFactory state,
401
+ * and grouping the combined actions by key
402
+ * @param targetTraitFactory
403
+ * @param traitFactoriesMap
404
+ *
405
+ * @example
406
+ *
407
+ * const clientsFeatureFactory = createEntityFeatureFactory(
408
+ * { entityName: 'client', entitiesName: 'clients' },
409
+ * addLoadEntitiesTrait<Client>(),
410
+ * addCrudEntitiesTrait<Client>()
411
+ * );
412
+ *
413
+ * const productOrderFeatureFactory = createEntityFeatureFactory(
414
+ * { entityName: 'productOrder' },
415
+ * addLoadEntitiesTrait<ProductOrder>(),
416
+ * addSelectEntitiesTrait<ProductOrder>()
417
+ * );
418
+ *
419
+ * const productFeatureFactory = createEntityFeatureFactory(
420
+ * { entityName: 'product' },
421
+ * addLoadEntitiesTrait<Product>(),
422
+ * addSelectEntitiesTrait<Product>()
423
+ * );
424
+ *
425
+ * const productAddEntityPropertiesFactory = addEntityFeaturesProperties(
426
+ * productFeatureFactory,
427
+ * {
428
+ * productOrders: productOrderFeatureFactory,
429
+ * clients: clientsFeatureFactory,
430
+ * }
431
+ * );
432
+ *
433
+ * const combinedFeature = productAddEntityPropertiesFactory({
434
+ * actionsGroupKey: '[addEntityFeatures]',
435
+ * featureSelector: 'addEntityFeatures',
436
+ * });
437
+ *
438
+ * combinedFeature.actions.loadProducts();
439
+ * combinedFeature.actions.clients.loadClients();
440
+ * combinedFeature.actions.productOrders.loadProductOrders();
441
+ */
442
+ function addEntityFeaturesProperties(targetTraitFactory, traitFactoriesMap) {
443
+ return ((config) => {
444
+ const featureSelector = typeof config.featureSelector === 'string'
445
+ ? createFeatureSelector(config.featureSelector)
446
+ : config.featureSelector;
447
+ const targetFeatureTraits = targetTraitFactory({
448
+ actionsGroupKey: config.actionsGroupKey,
449
+ featureSelector: featureSelector,
450
+ });
451
+ const actions = { ...targetFeatureTraits.actions };
452
+ const selectors = { ...targetFeatureTraits.selectors };
453
+ const reducers = {};
454
+ let effects = [...targetFeatureTraits.effects];
455
+ for (const [key, entityFeatureFactory] of Object.entries(traitFactoriesMap)) {
456
+ const selector = createSelector(featureSelector, (state) => state[key]);
457
+ const featureTraits = entityFeatureFactory({
458
+ actionsGroupKey: config.actionsGroupKey,
459
+ featureSelector: selector,
460
+ });
461
+ actions[key] = featureTraits.actions;
462
+ selectors[key] = featureTraits.selectors;
463
+ reducers[key] = featureTraits.reducer;
464
+ effects = [...effects, ...featureTraits.effects];
465
+ }
466
+ return {
467
+ actions,
468
+ selectors,
469
+ reducer: setPropertiesReducer(targetFeatureTraits.reducer, reducers),
470
+ effects,
471
+ };
472
+ });
473
+ }
474
+ /**
475
+ * Helper function to create an implementation a TraitFactory
476
+ * @param f TraitFactory implementation
477
+ */
478
+ function createTraitFactory(f) {
479
+ return f;
480
+ }
481
+
482
+ class TraitEffect {
483
+ constructor(actions$, store) {
484
+ this.actions$ = actions$;
485
+ this.store = store;
486
+ this.name = this.constructor.name;
487
+ this.componentId = '';
488
+ }
489
+ ngrxOnIdentifyEffects() {
490
+ return this.componentId ? this.name + this.componentId : '';
491
+ }
492
+ ngrxOnRunEffects(resolvedEffects$) {
493
+ return this.componentId
494
+ ? resolvedEffects$.pipe(takeUntil(this.actions$.pipe(ofType(getDestroyActionName(this.componentId)))))
495
+ : resolvedEffects$;
496
+ }
497
+ }
498
+ TraitEffect.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.11", ngImport: i0, type: TraitEffect, deps: [{ token: i1.Actions }, { token: i2.Store }], target: i0.ɵɵFactoryTarget.Injectable });
499
+ TraitEffect.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.3.11", ngImport: i0, type: TraitEffect });
500
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.11", ngImport: i0, type: TraitEffect, decorators: [{
501
+ type: Injectable
502
+ }], ctorParameters: function () { return [{ type: i1.Actions }, { type: i2.Store }]; } });
503
+ function getDestroyActionName(id) {
504
+ return `[${id}] Destroyed`;
505
+ }
506
+
507
+ /**
508
+ * @ignore
509
+ * @internal
510
+ */
511
+ const DISABLE_LOCAL_TRAIT_EFFECTS = new InjectionToken('disableLocalTraitEffects');
512
+
513
+ let id = 0;
514
+ function uniqueComponentId() {
515
+ return id++;
516
+ }
517
+ /**
518
+ * Builds traitFactory and registers effects and reducers with
519
+ * a generated component id, returns build actions and selectors
520
+ * and a destroy method that will unergister the effects and reducers
521
+ * when called, and a addEffect which can be use to register extra effects
522
+ *
523
+ * Used inside TraitsLocalStore, can be used to create your
524
+ * own Component Service without extending TraitsLocalStore
525
+ * @param injector
526
+ * @param componentName
527
+ * @param traitFactory
528
+ */
529
+ function buildLocalTraits(injector, componentName, traitFactory) {
530
+ const reducers = injector.get(ReducerManager);
531
+ const effects = injector.get(EffectSources);
532
+ const store = injector.get(Store);
533
+ const componentId = `${componentName}_${uniqueComponentId()}`;
534
+ const traits = traitFactory({
535
+ featureSelector: createFeatureSelector(componentId),
536
+ actionsGroupKey: `[${componentId}]`,
537
+ });
538
+ traits.reducer && reducers.addReducer(componentId, traits.reducer);
539
+ const providers = (traits.effects && [...traits.effects.map((e) => ({ provide: e }))]) || [];
540
+ const disableLocalTraitsEffects = injector.get(DISABLE_LOCAL_TRAIT_EFFECTS, false);
541
+ if (!disableLocalTraitsEffects) {
542
+ const i = Injector.create({
543
+ providers: providers,
544
+ parent: injector,
545
+ });
546
+ traits.effects?.forEach((e) => {
547
+ const effect = i.get(e);
548
+ effect.componentId = componentId;
549
+ effects.addEffects(effect);
550
+ });
551
+ }
552
+ function destroy() {
553
+ store.dispatch({ type: getDestroyActionName(componentId) });
554
+ /**
555
+ * A service that extends TraitsLocalStore and other component service are destroyed
556
+ * before the component that depends on them, this causes that any subscriptions
557
+ * to selectors of the TraitsLocalStore could fail because the store state is removed before
558
+ * they are unsubscribe by the onDestroy of the component. Executing reducers.removeReducer
559
+ * inside setTimeout ensures the state is remove after the component onDestroy was called,
560
+ * avoiding the before mentioned possible issues.
561
+ */
562
+ setTimeout(() => reducers.removeReducer(componentId));
563
+ }
564
+ return {
565
+ destroy,
566
+ actions: traits.actions,
567
+ selectors: traits.selectors,
568
+ addEffects(localEffect) {
569
+ localEffect.componentId = componentId;
570
+ effects.addEffects(localEffect);
571
+ },
572
+ };
573
+ }
574
+ /**
575
+ * Class used to create local traits service, receives a trait factory, which will be
576
+ * built and its reducers and effect register using a dynamic id when the service is built
577
+ * and get destroyed when the onDestroy lifecycle method of the service is called, if the service
578
+ * has effects this.traits.addEffects(this) should be call in the constructor
579
+ *
580
+ * @example
581
+ * const productFeatureFactory = createEntityFeatureFactory(
582
+ * { entityName: 'product' },
583
+ * addLoadEntitiesTrait<Product>(),
584
+ * addSelectEntityTrait<Product>(),
585
+ * );
586
+ *
587
+ * Injectable()
588
+ * export class ProductsLocalTraits extends TraitsLocalStore<
589
+ * typeof productFeatureFactory
590
+ * > {
591
+ * loadProducts$ = createEffect(() =>
592
+ * this.actions$.pipe(
593
+ * ofType(this.localActions.loadProducts),
594
+ * switchMap(() =>
595
+ * //call your service to get the products data
596
+ * this.productService.getProducts().pipe(
597
+ * map((res) =>
598
+ * this.localActions.loadProductsSuccess({ entities: res.resultList })
599
+ * ),
600
+ * catchError(() => of(this.localActions.loadProductsFail()))
601
+ * )
602
+ * )
603
+ * )
604
+ * );
605
+ *
606
+ * constructor(injector: Injector, private productService: ProductService) {
607
+ * super(injector);
608
+ * this.traits.addEffects(this); // IMPORTANT! add this line if the service has effects
609
+ * }
610
+ *
611
+ * setup(): LocalTraitsConfig<typeof productFeatureFactory> {
612
+ * return {
613
+ * componentName: 'ProductsPickerComponent',
614
+ * traitsFactory: productFeatureFactory,
615
+ * };
616
+ * }
617
+ * }
618
+ *
619
+ * // use in component later
620
+ *
621
+ * Component({
622
+ * selector: 'some-component',
623
+ * template: `<div> some content</div> `,
624
+ * providers: [ProductsLocalTraits],
625
+ * changeDetection: ChangeDetectionStrategy.OnPush,
626
+ * })
627
+ * export class ProductSelectDialogComponent implements OnInit {
628
+ * constructor(private store: Store, private traits: ProductsLocalTraits) {}
629
+ *
630
+ * ngOnInit() {
631
+ * this.store.dispatch(this.traits.localActions.loadProducts());
632
+ * }
633
+ * }
634
+ */
635
+ class TraitsLocalStore extends TraitEffect {
636
+ constructor(injector) {
637
+ super(injector.get(Actions), injector.get(Store));
638
+ this.injector = injector;
639
+ const config = this.setup();
640
+ this.traits = buildLocalTraits(this.injector, config.componentName, config.traitsFactory);
641
+ this.localActions = this.traits.actions;
642
+ this.localSelectors = this.traits.selectors;
643
+ }
644
+ ngOnDestroy() {
645
+ this.traits.destroy();
646
+ }
647
+ }
648
+ TraitsLocalStore.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.11", ngImport: i0, type: TraitsLocalStore, deps: [{ token: i0.Injector }], target: i0.ɵɵFactoryTarget.Injectable });
649
+ TraitsLocalStore.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "13.3.11", ngImport: i0, type: TraitsLocalStore });
650
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.11", ngImport: i0, type: TraitsLocalStore, decorators: [{
651
+ type: Injectable
652
+ }], ctorParameters: function () { return [{ type: i0.Injector }]; } });
653
+
654
+ function hash(key) {
655
+ return JSON.stringify(key, (_, val) => typeof val == 'object'
656
+ ? Object.keys(val)
657
+ .sort()
658
+ .reduce((result, k) => {
659
+ result[k] = val[k];
660
+ return result;
661
+ }, {})
662
+ : val);
663
+ }
664
+ function hashKey(key) {
665
+ return typeof key === 'string'
666
+ ? [key]
667
+ : key.map((k) => {
668
+ return typeof k === 'string' ? k : hash(k);
669
+ });
670
+ }
671
+ function getCacheValue(keys, state) {
672
+ let parent = state;
673
+ for (const key of keys) {
674
+ parent = parent?.keys?.[key];
675
+ if (!parent)
676
+ return undefined;
677
+ }
678
+ return parent?.data;
679
+ }
680
+ function isCacheValid(cache, exp) {
681
+ return !cache.invalid && Date.now() <= cache.date + exp;
682
+ }
683
+
684
+ const cacheStateSelector = createFeatureSelector('cache');
685
+ const selectCache = (key) => createSelector(cacheStateSelector, (state) => getCacheValue(hashKey(key), state));
686
+
687
+ const cache$1 = createAction('[Cache] Cache', props());
688
+ const hitCache = createAction('[Cache] Hit Cache', props());
689
+ const invalidateCache$1 = createAction('[Cache] Invalidate Cache', props());
690
+ const deleteCache = createAction('[Cache] Delete Cache', props());
691
+
692
+ /**
693
+ * Cache the result of source parameter using the provided key, when call
694
+ * again if the cache is valid (exist and is not expired or invalidated)
695
+ * it will return the cache value without calling again source
696
+ * @example
697
+ * // cache for 3 min
698
+ * loadStores$ = createEffect(() => {
699
+ * return this.actions$.pipe(
700
+ * ofType(ProductStoreActions.loadStores),
701
+ * exhaustMap(() =>
702
+ * cache({
703
+ * key: ['stores'],
704
+ * store: this.store,
705
+ * source: this.storeService.getStores(),
706
+ * expire: 1000 * 60 * 3 // optional param , cache forever if not present
707
+ * }).pipe(
708
+ * map((res) => ProductStoreActions.loadStoresSuccess({ entities: res })),
709
+ * catchError(() => of(ProductStoreActions.loadStoresFail()))
710
+ * )
711
+ * )
712
+ * );
713
+ * });
714
+ * // cache top 10, for 3 mins
715
+ * loadDepartments$ = createEffect(() => {
716
+ * return this.actions$.pipe(
717
+ * ofType(this.localActions.loadDepartments),
718
+ * concatLatestFrom(() =>
719
+ * this.store.select(this.localSelectors.selectDepartmentsFilter)
720
+ * ),
721
+ * exhaustMap(([_, filters]) =>
722
+ * cache({
723
+ * key: ['stores','departments',{ storeId: filters!.storeId },
724
+ * store: this.store,
725
+ * source: this.storeService.getStoreDepartments(filters!.storeId),
726
+ * expires: 1000 * 60 * 3,
727
+ * maxCacheSize: 10,
728
+ * }).pipe(
729
+ * map((res) =>
730
+ * this.localActions.loadDepartmentsSuccess({
731
+ * entities: res,
732
+ * })
733
+ * ),
734
+ * catchError(() => of(this.localActions.loadDepartmentsFail()))
735
+ * )
736
+ * )
737
+ * );
738
+ * });
739
+ *
740
+ * @param options - configuration
741
+ * @param options.store - required ngrx store
742
+ * @param options.key - key can be string, array of string or array of string with plain objects
743
+ * @param options.source - called when cache is invalid
744
+ * @param options.expires - time to expire the cache valued, if not present is infinite
745
+ * @param options.maxCacheSize - max number of keys to store , only works if last key is variable
746
+ */
747
+ function cache({ store, key, source, expires, maxCacheSize, skip, }) {
748
+ const exp = expires ?? Infinity;
749
+ return store.select(selectCache(key)).pipe(first(), concatMap((cache) => cache && !skip && isCacheValid(cache, exp)
750
+ ? of(cache.value).pipe(tap(() => store.dispatch(hitCache({ key }))))
751
+ : source.pipe(tap((value) => store.dispatch(cache$1({
752
+ key,
753
+ date: Date.now(),
754
+ value,
755
+ maxCacheSize,
756
+ }))))));
757
+ }
758
+
759
+ const initialState = {
760
+ keys: {},
761
+ };
762
+ const cacheReducer = createReducer(initialState, on(cache$1, (state, { key, value, date, maxCacheSize }) => setCacheValue(hashKey(key), { value, date, invalid: false }, state, maxCacheSize)), on(invalidateCache$1, (state, { key }) => {
763
+ const k = hashKey(key);
764
+ return invalidateCache(k, state);
765
+ }), on(deleteCache, (state, { key }) => {
766
+ const k = hashKey(key);
767
+ return deleteCacheValue(k, state);
768
+ }), on(hitCache, (state, { key }) => {
769
+ const k = hashKey(key);
770
+ return increaseCacheHitCount(k, state);
771
+ }));
772
+ function setCacheValue(keys, value, state, maxCacheSize, expires) {
773
+ const newState = { ...state };
774
+ let cache = newState;
775
+ let lastCache = undefined;
776
+ for (let i = 0; i < keys.length; i++) {
777
+ const key = keys[i];
778
+ cache.keys = cache?.keys ? { ...cache?.keys } : {};
779
+ let v = cache.keys[key];
780
+ v = v ? { ...v } : {};
781
+ cache.keys[key] = v;
782
+ lastCache = cache;
783
+ cache = v;
784
+ }
785
+ cache.data = cache.data
786
+ ? { ...value, hitCount: cache.data.hitCount + 1 }
787
+ : { ...value, hitCount: 1 };
788
+ if (maxCacheSize &&
789
+ lastCache?.keys &&
790
+ Object.keys(lastCache.keys).length > maxCacheSize) {
791
+ const entries = findLessHitOrOldestCacheEntries(lastCache, expires ?? Infinity, maxCacheSize);
792
+ if (entries && entries.length) {
793
+ for (const [key] of entries) {
794
+ delete lastCache.keys[key];
795
+ }
796
+ }
797
+ }
798
+ return newState;
799
+ }
800
+ function findLessHitOrOldestCacheEntries(state, expires, maxCacheSize) {
801
+ if (!state.keys)
802
+ return undefined;
803
+ const entries = Object.entries(state.keys);
804
+ // find the newest key;
805
+ const [newestKey] = entries.reduce((a, b) => {
806
+ const aDate = a[1].data?.date ?? 0;
807
+ const bDate = b[1].data?.date ?? 0;
808
+ return aDate > bDate ? a : b;
809
+ });
810
+ const sorted = entries.sort(([aKey, aValue], [bKey, bValue]) => {
811
+ // ensures the newest key always wins
812
+ if (aKey === newestKey)
813
+ return -1;
814
+ if (bKey === newestKey)
815
+ return 1;
816
+ const aValid = aValue.data && isCacheValid(aValue.data, expires) ? 1 : 0;
817
+ const bValid = bValue.data && isCacheValid(bValue.data, expires) ? 1 : 0;
818
+ const diffValid = aValid - bValid;
819
+ const diffHit = (aValue.data?.hitCount ?? 0) - (bValue.data?.hitCount ?? 0);
820
+ const diffDate = (aValue.data?.date ?? 0) - (bValue.data?.date ?? 0);
821
+ return (-1 * (diffValid === 0 ? (diffHit === 0 ? diffDate : diffHit) : diffValid));
822
+ });
823
+ return sorted.slice(maxCacheSize);
824
+ }
825
+ function deleteCacheValue(keys, state) {
826
+ const newState = { ...state };
827
+ let cache = newState;
828
+ for (const key of keys) {
829
+ if (!cache.keys)
830
+ return state;
831
+ cache.keys = { ...cache?.keys };
832
+ let v = cache.keys[key];
833
+ if (!v)
834
+ return state;
835
+ v = { ...v };
836
+ cache.keys[key] = v;
837
+ cache = v;
838
+ }
839
+ if (cache.data)
840
+ delete cache.data;
841
+ else if (cache.keys)
842
+ delete cache.keys;
843
+ return newState;
844
+ }
845
+ function invalidateCache(keys, state) {
846
+ const newState = { ...state };
847
+ let cache = newState;
848
+ for (const key of keys) {
849
+ if (!cache?.keys)
850
+ return state;
851
+ cache.keys = { ...cache?.keys };
852
+ let v = cache?.keys?.[key];
853
+ if (!v)
854
+ return state;
855
+ v = { ...v };
856
+ cache.keys[key] = v;
857
+ cache = v;
858
+ }
859
+ cache && invalidaSubKeys(cache);
860
+ return newState;
861
+ }
862
+ function increaseCacheHitCount(keys, state) {
863
+ const newState = { ...state };
864
+ let cache = newState;
865
+ for (const key of keys) {
866
+ if (!cache?.keys)
867
+ return state;
868
+ cache.keys = { ...cache?.keys };
869
+ let v = cache?.keys?.[key];
870
+ if (!v)
871
+ return state;
872
+ v = { ...v };
873
+ cache.keys[key] = v;
874
+ cache = v;
875
+ }
876
+ if (!cache.data)
877
+ return state;
878
+ cache.data = { ...cache.data, hitCount: cache.data.hitCount + 1 };
879
+ return newState;
880
+ }
881
+ function invalidaSubKeys(state) {
882
+ if (state.data) {
883
+ state.data = { ...state.data, invalid: true };
884
+ }
885
+ if (state.keys) {
886
+ state.keys = { ...state.keys };
887
+ for (const key in state.keys) {
888
+ state.keys[key] = invalidaSubKeys({ ...state.keys[key] });
889
+ }
890
+ }
891
+ return state;
892
+ }
893
+
894
+ class CacheModule {
895
+ }
896
+ CacheModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "13.3.11", ngImport: i0, type: CacheModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
897
+ CacheModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "13.3.11", ngImport: i0, type: CacheModule, imports: [i2.StoreFeatureModule] });
898
+ CacheModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "13.3.11", ngImport: i0, type: CacheModule, providers: [], imports: [[StoreModule.forFeature('cache', cacheReducer)]] });
899
+ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "13.3.11", ngImport: i0, type: CacheModule, decorators: [{
900
+ type: NgModule,
901
+ args: [{
902
+ imports: [StoreModule.forFeature('cache', cacheReducer)],
903
+ providers: [],
904
+ }]
905
+ }] });
906
+
907
+ const CacheActions = { invalidateCache: invalidateCache$1, deleteCache };
908
+ const CacheSelectors = { getCache: selectCache };
909
+
910
+ /**
911
+ * Generated bundle index. Do not edit.
912
+ */
913
+
914
+ export { CacheActions, CacheModule, CacheSelectors, DISABLE_LOCAL_TRAIT_EFFECTS, TraitEffect, TraitsLocalStore, addEntityFeaturesProperties, buildLocalTraits, cache, camelCaseToSentence, capitalize, combineEntityFeatures, createEntityFeatureFactory, createTraitFactory, getDestroyActionName, insertIf, joinReducers, mixEntityFeatures, setPropertiesReducer, setPropertyReducer, toMap };
915
+ //# sourceMappingURL=ngrx-traits-core.mjs.map