ngssm-store 22.1.0 → 22.2.1

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.
package/README.md CHANGED
@@ -4,13 +4,13 @@ A lightweight, production-ready state management library for Angular application
4
4
 
5
5
  ## Overview
6
6
 
7
- `ngssm-store` is a simple yet powerful custom implementation of the Redux pattern designed specifically for Angular. It leverages Angular's dependency injection, RxJS for reactive updates, and modern Angular signals for optimal reactivity.
7
+ `ngssm-store` is a simple yet powerful custom implementation of the Redux pattern designed specifically for Angular. It leverages Angular's dependency injection and modern Angular signals for reactive state updates.
8
8
 
9
9
  ### Key Features
10
10
 
11
11
  - **Centralized State Management**: Single source of truth for application state
12
12
  - **Redux Pattern**: Actions → Reducers → State → Effects → Actions
13
- - **Dual Reactivity**: Both RxJS observables and Angular Signals support
13
+ - **Signal Reactivity**: Angular Signals provide reactive state and action updates
14
14
  - **Immutable State**: Uses `immutability-helper` to ensure state immutability
15
15
  - **Effect System**: Side effects, async operations, and action chaining
16
16
  - **Feature States**: Modular state management with feature-based organization
@@ -199,54 +199,11 @@ export class CounterComponent {
199
199
  }
200
200
  ```
201
201
 
202
- ### Accessing State with Signals
203
-
204
- Use the signal-based API for reactive components:
205
-
206
- ```typescript
207
- import { createSignal } from 'ngssm-store';
208
-
209
- @Component({
210
- selector: 'app-dashboard',
211
- template: `
212
- <div>
213
- <p>Total Users: {{ userCount() }}</p>
214
- <div *ngIf="loading()">Loading...</div>
215
- <ul>
216
- <li *ngFor="let user of users()">{{ user.name }}</li>
217
- </ul>
218
- </div>`
219
- })
220
- export class DashboardComponent {
221
- private store = inject(Store);
222
-
223
- users = createSignal((state) => state.users?.list ?? []);
224
202
  loading = createSignal((state) => state.users?.loading ?? false);
225
203
  userCount = createSignal((state) => (state.users?.list ?? []).length);
226
204
  }
227
205
  ```
228
206
 
229
- ### Accessing State with RxJS
230
-
231
- For components that need RxJS integration:
232
-
233
- ```typescript
234
- @Component({
235
- selector: 'app-user-list',
236
- template: `
237
- <ul>
238
- <li *ngFor="let user of users$ | async">{{ user.name }}</li>
239
- </ul>`
240
- })
241
- export class UserListComponent {
242
- private store = inject(Store);
243
-
244
- users$ = this.store.state$.pipe(
245
- map((state) => state.users?.list ?? [])
246
- );
247
- }
248
- ```
249
-
250
207
  ### Direct State Access
251
208
 
252
209
  Access the current state directly:
@@ -258,8 +215,6 @@ export class MyComponent {
258
215
 
259
216
  getCurrentState() {
260
217
  const currentState = this.store.state(); // Signal access
261
- // or
262
- const viaObservable = this.store.state$; // Observable access
263
218
  }
264
219
  }
265
220
  ```
@@ -274,9 +229,6 @@ export class MyComponent {
274
229
  private store = inject(Store);
275
230
 
276
231
  lastAction = createSignal((state) => this.store.processedAction().type);
277
-
278
- // Or with RxJS
279
- lastAction$ = this.store.processedAction$.pipe(map(a => a.type));
280
232
  }
281
233
  ```
282
234
 
@@ -435,9 +387,7 @@ export class Store {
435
387
  ### Store Class
436
388
 
437
389
  - `state(): Signal<State>` - Get current state as a Signal
438
- - `state$: Observable<State>` - Get state as an Observable
439
390
  - `processedAction(): Signal<Action>` - Get last processed action as a Signal
440
- - `processedAction$: Observable<Action>` - Get last processed action as Observable
441
391
  - `dispatchAction(action: Action): void` - Dispatch an action
442
392
  - `dispatchActionType(actionType: string): void` - Dispatch by action type string
443
393
 
@@ -449,6 +399,7 @@ export class Store {
449
399
  - `provideReducers(...reducers)` - Register multiple reducers
450
400
  - `provideEffect(effect)` - Register a single effect
451
401
  - `provideEffects(...effects)` - Register multiple effects
402
+ - `EffectFunc<TActionType extends Action = Action>` - Effect function receiving state and an action; defaults to the base `Action` type
452
403
  - `provideEffectFunc(actionType, func)` - Register an effect function
453
404
 
454
405
  ### Interfaces
@@ -1,132 +1,67 @@
1
- import * as i0 from '@angular/core';
2
- import { inject, Injectable, makeEnvironmentProviders, provideAppInitializer } from '@angular/core';
3
- import { Store, Logger } from 'ngssm-store';
4
- import { StoreMock } from 'ngssm-store/testing';
5
- import { updateNgssmCachingState, selectNgssmCachedItem, CachedItemStatus, NgssmCachingStateSpecification } from 'ngssm-store/caching';
6
- import { TestBed } from '@angular/core/testing';
7
-
8
- /**
9
- * Utility service for setting and updating cached items in the StoreMock during tests.
10
- * Provides methods to apply a SetCachedItemAction, set the status, or set the value of a cached item.
11
- */
12
- class NgssmCachedItemSetter {
13
- store = inject(Store);
14
- /**
15
- * Applies a SetCachedItemAction to the StoreMock, updating the cache state.
16
- * @param action The SetCachedItemAction to apply.
17
- * @returns The NgssmCachedItemSetter instance for chaining.
18
- */
19
- apply(action) {
20
- this.store.stateValue = updateNgssmCachingState(this.store.stateValue, {
21
- caches: {
22
- [action.cachedItemKey]: {
23
- $set: {
24
- status: action.status,
25
- item: action.cachedItem,
26
- error: action.error
27
- }
28
- }
29
- }
30
- });
31
- return this;
32
- }
33
- /**
34
- * Sets the status of a cached item in the StoreMock.
35
- * If the key does not exist in cache, it is created with an undefined value.
36
- *
37
- * @param cachedItemKey The key of the cached item.
38
- * @param status The new status to set.
39
- * @returns The NgssmCachedItemSetter instance for chaining.
40
- */
41
- setCachedItemStatus(cachedItemKey, status) {
42
- if (selectNgssmCachedItem(this.store.stateValue, cachedItemKey)) {
43
- this.store.stateValue = updateNgssmCachingState(this.store.stateValue, {
44
- caches: {
45
- [cachedItemKey]: {
46
- status: { $set: status }
47
- }
48
- }
49
- });
50
- }
51
- else {
52
- this.store.stateValue = updateNgssmCachingState(this.store.stateValue, {
53
- caches: {
54
- [cachedItemKey]: {
55
- $set: {
56
- status
57
- }
58
- }
59
- }
60
- });
61
- }
62
- return this;
63
- }
64
- /**
65
- * Sets the value of a cached item in the StoreMock.
66
- * If the key does not exist in cache, it is created with the status CachedItemStatus.notSet.
67
- *
68
- * @param cachedItemKey The key of the cached item.
69
- * @param value The value to set.
70
- * @returns The NgssmCachedItemSetter instance for chaining.
71
- */
72
- setCachedItemValue(cachedItemKey, value) {
73
- if (selectNgssmCachedItem(this.store.stateValue, cachedItemKey)) {
74
- this.store.stateValue = updateNgssmCachingState(this.store.stateValue, {
75
- caches: {
76
- [cachedItemKey]: {
77
- item: { $set: value }
78
- }
79
- }
80
- });
81
- }
82
- else {
83
- this.store.stateValue = updateNgssmCachingState(this.store.stateValue, {
84
- caches: {
85
- [cachedItemKey]: {
86
- $set: {
87
- status: CachedItemStatus.notSet,
88
- item: value
89
- }
90
- }
91
- }
92
- });
93
- }
94
- return this;
95
- }
96
- static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.1.1", ngImport: i0, type: NgssmCachedItemSetter, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
97
- static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.1.1", ngImport: i0, type: NgssmCachedItemSetter });
98
- }
99
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.1.1", ngImport: i0, type: NgssmCachedItemSetter, decorators: [{
100
- type: Injectable
101
- }] });
1
+ import * as i0 from "@angular/core";
2
+ import { Injectable, inject, makeEnvironmentProviders, provideAppInitializer } from "@angular/core";
3
+ import { Logger, Store } from "ngssm-store";
4
+ import { StoreMock } from "ngssm-store/testing";
5
+ import { CachedItemStatus, NgssmCachingStateSpecification, selectNgssmCachedItem, updateNgssmCachingState } from "ngssm-store/caching";
6
+ import { TestBed } from "@angular/core/testing";
7
+ var NgssmCachedItemSetter = class NgssmCachedItemSetter {
8
+ store = inject(Store);
9
+ apply(action) {
10
+ this.store.stateValue = updateNgssmCachingState(this.store.stateValue, { caches: { [action.cachedItemKey]: { $set: {
11
+ status: action.status,
12
+ item: action.cachedItem,
13
+ error: action.error
14
+ } } } });
15
+ return this;
16
+ }
17
+ setCachedItemStatus(cachedItemKey, status) {
18
+ if (selectNgssmCachedItem(this.store.stateValue, cachedItemKey)) this.store.stateValue = updateNgssmCachingState(this.store.stateValue, { caches: { [cachedItemKey]: { status: { $set: status } } } });
19
+ else this.store.stateValue = updateNgssmCachingState(this.store.stateValue, { caches: { [cachedItemKey]: { $set: { status } } } });
20
+ return this;
21
+ }
22
+ setCachedItemValue(cachedItemKey, value) {
23
+ if (selectNgssmCachedItem(this.store.stateValue, cachedItemKey)) this.store.stateValue = updateNgssmCachingState(this.store.stateValue, { caches: { [cachedItemKey]: { item: { $set: value } } } });
24
+ else this.store.stateValue = updateNgssmCachingState(this.store.stateValue, { caches: { [cachedItemKey]: { $set: {
25
+ status: CachedItemStatus.notSet,
26
+ item: value
27
+ } } } });
28
+ return this;
29
+ }
30
+ static ɵfac = i0.ɵɵngDeclareFactory({
31
+ minVersion: "12.0.0",
32
+ version: "22.2.0",
33
+ ngImport: i0,
34
+ type: NgssmCachedItemSetter,
35
+ deps: [],
36
+ target: i0.ɵɵFactoryTarget.Injectable
37
+ });
38
+ static ɵprov = i0.ɵɵngDeclareInjectable({
39
+ minVersion: "12.0.0",
40
+ version: "22.2.0",
41
+ ngImport: i0,
42
+ type: NgssmCachedItemSetter
43
+ });
44
+ };
45
+ i0.ɵɵngDeclareClassMetadata({
46
+ minVersion: "12.0.0",
47
+ version: "22.2.0",
48
+ ngImport: i0,
49
+ type: NgssmCachedItemSetter,
50
+ decorators: [{ type: Injectable }]
51
+ });
102
52
  const ngssmCachedItemSetter = () => TestBed.inject(NgssmCachedItemSetter);
103
-
104
- /**
105
- * App initializer that sets up the NgssmCaching state in the StoreMock for testing purposes.
106
- * Throws an error if StoreMock is not registered.
107
- */
108
53
  const ngssmCachingStateInitializer = () => {
109
- const logger = inject(Logger);
110
- logger.information('[ngssm-caching-testing] Initialization of state');
111
- const store = inject(Store);
112
- if (!(store instanceof StoreMock)) {
113
- throw new Error('StoreMock is not registered.');
114
- }
115
- store.stateValue = {
116
- ...store.stateValue,
117
- [NgssmCachingStateSpecification.featureStateKey]: NgssmCachingStateSpecification.initialState
118
- };
54
+ inject(Logger).information("[ngssm-caching-testing] Initialization of state");
55
+ const store = inject(Store);
56
+ if (!(store instanceof StoreMock)) throw new Error("StoreMock is not registered.");
57
+ store.stateValue = {
58
+ ...store.stateValue,
59
+ [NgssmCachingStateSpecification.featureStateKey]: NgssmCachingStateSpecification.initialState
60
+ };
119
61
  };
120
- /**
121
- * Provides environment providers for NgssmCaching testing, including the state initializer.
122
- */
123
62
  const provideNgssmCachingTesting = () => {
124
- return makeEnvironmentProviders([provideAppInitializer(ngssmCachingStateInitializer), NgssmCachedItemSetter]);
63
+ return makeEnvironmentProviders([provideAppInitializer(ngssmCachingStateInitializer), NgssmCachedItemSetter]);
125
64
  };
126
-
127
- /**
128
- * Generated bundle index. Do not edit.
129
- */
130
-
131
65
  export { NgssmCachedItemSetter, ngssmCachedItemSetter, ngssmCachingStateInitializer, provideNgssmCachingTesting };
132
- //# sourceMappingURL=ngssm-store-caching-testing.mjs.map
66
+
67
+ //# sourceMappingURL=ngssm-store-caching-testing.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"ngssm-store-caching-testing.mjs","sources":["../../../projects/ngssm-store/caching/testing/src/ngssm-cached-item-setter.ts","../../../projects/ngssm-store/caching/testing/src/provide-ngssm-caching-testing.ts","../../../projects/ngssm-store/caching/testing/ngssm-store-caching-testing.ts"],"sourcesContent":["import { inject, Injectable } from '@angular/core';\nimport { TestBed } from '@angular/core/testing';\n\nimport { Store } from 'ngssm-store';\nimport { CachedItemStatus, selectNgssmCachedItem, SetCachedItemAction, updateNgssmCachingState } from 'ngssm-store/caching';\nimport { StoreMock } from 'ngssm-store/testing';\n\n/**\n * Utility service for setting and updating cached items in the StoreMock during tests.\n * Provides methods to apply a SetCachedItemAction, set the status, or set the value of a cached item.\n */\n@Injectable()\nexport class NgssmCachedItemSetter {\n public readonly store = inject(Store) as unknown as StoreMock;\n\n /**\n * Applies a SetCachedItemAction to the StoreMock, updating the cache state.\n * @param action The SetCachedItemAction to apply.\n * @returns The NgssmCachedItemSetter instance for chaining.\n */\n public apply<T = unknown>(action: SetCachedItemAction<T>): NgssmCachedItemSetter {\n this.store.stateValue = updateNgssmCachingState(this.store.stateValue, {\n caches: {\n [action.cachedItemKey]: {\n $set: {\n status: action.status,\n item: action.cachedItem,\n error: action.error\n }\n }\n }\n });\n\n return this;\n }\n\n /**\n * Sets the status of a cached item in the StoreMock.\n * If the key does not exist in cache, it is created with an undefined value.\n *\n * @param cachedItemKey The key of the cached item.\n * @param status The new status to set.\n * @returns The NgssmCachedItemSetter instance for chaining.\n */\n public setCachedItemStatus(cachedItemKey: string, status: CachedItemStatus): NgssmCachedItemSetter {\n if (selectNgssmCachedItem(this.store.stateValue, cachedItemKey)) {\n this.store.stateValue = updateNgssmCachingState(this.store.stateValue, {\n caches: {\n [cachedItemKey]: {\n status: { $set: status }\n }\n }\n });\n } else {\n this.store.stateValue = updateNgssmCachingState(this.store.stateValue, {\n caches: {\n [cachedItemKey]: {\n $set: {\n status\n }\n }\n }\n });\n }\n\n return this;\n }\n\n /**\n * Sets the value of a cached item in the StoreMock.\n * If the key does not exist in cache, it is created with the status CachedItemStatus.notSet.\n *\n * @param cachedItemKey The key of the cached item.\n * @param value The value to set.\n * @returns The NgssmCachedItemSetter instance for chaining.\n */\n public setCachedItemValue<T>(cachedItemKey: string, value?: T): NgssmCachedItemSetter {\n if (selectNgssmCachedItem(this.store.stateValue, cachedItemKey)) {\n this.store.stateValue = updateNgssmCachingState(this.store.stateValue, {\n caches: {\n [cachedItemKey]: {\n item: { $set: value }\n }\n }\n });\n } else {\n this.store.stateValue = updateNgssmCachingState(this.store.stateValue, {\n caches: {\n [cachedItemKey]: {\n $set: {\n status: CachedItemStatus.notSet,\n item: value\n }\n }\n }\n });\n }\n\n return this;\n }\n}\n\nexport const ngssmCachedItemSetter = () => TestBed.inject(NgssmCachedItemSetter);\n","import { EnvironmentProviders, inject, makeEnvironmentProviders, provideAppInitializer } from '@angular/core';\n\nimport { Logger, Store } from 'ngssm-store';\nimport { StoreMock } from 'ngssm-store/testing';\nimport { NgssmCachingStateSpecification } from 'ngssm-store/caching';\n\nimport { NgssmCachedItemSetter } from './ngssm-cached-item-setter';\n\n/**\n * App initializer that sets up the NgssmCaching state in the StoreMock for testing purposes.\n * Throws an error if StoreMock is not registered.\n */\nexport const ngssmCachingStateInitializer = () => {\n const logger = inject(Logger);\n logger.information('[ngssm-caching-testing] Initialization of state');\n const store = inject(Store);\n if (!(store instanceof StoreMock)) {\n throw new Error('StoreMock is not registered.');\n }\n\n store.stateValue = {\n ...store.stateValue,\n [NgssmCachingStateSpecification.featureStateKey]: NgssmCachingStateSpecification.initialState\n };\n};\n\n/**\n * Provides environment providers for NgssmCaching testing, including the state initializer.\n */\nexport const provideNgssmCachingTesting = (): EnvironmentProviders => {\n return makeEnvironmentProviders([provideAppInitializer(ngssmCachingStateInitializer), NgssmCachedItemSetter]);\n};\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public_api';\n"],"names":[],"mappings":";;;;;;;AAOA;;;AAGG;MAEU,qBAAqB,CAAA;AAChB,IAAA,KAAK,GAAG,MAAM,CAAC,KAAK,CAAyB;AAE7D;;;;AAIG;AACI,IAAA,KAAK,CAAc,MAA8B,EAAA;AACtD,QAAA,IAAI,CAAC,KAAK,CAAC,UAAU,GAAG,uBAAuB,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,EAAE;AACrE,YAAA,MAAM,EAAE;AACN,gBAAA,CAAC,MAAM,CAAC,aAAa,GAAG;AACtB,oBAAA,IAAI,EAAE;wBACJ,MAAM,EAAE,MAAM,CAAC,MAAM;wBACrB,IAAI,EAAE,MAAM,CAAC,UAAU;wBACvB,KAAK,EAAE,MAAM,CAAC;AACf;AACF;AACF;AACF,SAAA,CAAC;AAEF,QAAA,OAAO,IAAI;IACb;AAEA;;;;;;;AAOG;IACI,mBAAmB,CAAC,aAAqB,EAAE,MAAwB,EAAA;QACxE,IAAI,qBAAqB,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,EAAE,aAAa,CAAC,EAAE;AAC/D,YAAA,IAAI,CAAC,KAAK,CAAC,UAAU,GAAG,uBAAuB,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,EAAE;AACrE,gBAAA,MAAM,EAAE;oBACN,CAAC,aAAa,GAAG;AACf,wBAAA,MAAM,EAAE,EAAE,IAAI,EAAE,MAAM;AACvB;AACF;AACF,aAAA,CAAC;QACJ;aAAO;AACL,YAAA,IAAI,CAAC,KAAK,CAAC,UAAU,GAAG,uBAAuB,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,EAAE;AACrE,gBAAA,MAAM,EAAE;oBACN,CAAC,aAAa,GAAG;AACf,wBAAA,IAAI,EAAE;4BACJ;AACD;AACF;AACF;AACF,aAAA,CAAC;QACJ;AAEA,QAAA,OAAO,IAAI;IACb;AAEA;;;;;;;AAOG;IACI,kBAAkB,CAAI,aAAqB,EAAE,KAAS,EAAA;QAC3D,IAAI,qBAAqB,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,EAAE,aAAa,CAAC,EAAE;AAC/D,YAAA,IAAI,CAAC,KAAK,CAAC,UAAU,GAAG,uBAAuB,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,EAAE;AACrE,gBAAA,MAAM,EAAE;oBACN,CAAC,aAAa,GAAG;AACf,wBAAA,IAAI,EAAE,EAAE,IAAI,EAAE,KAAK;AACpB;AACF;AACF,aAAA,CAAC;QACJ;aAAO;AACL,YAAA,IAAI,CAAC,KAAK,CAAC,UAAU,GAAG,uBAAuB,CAAC,IAAI,CAAC,KAAK,CAAC,UAAU,EAAE;AACrE,gBAAA,MAAM,EAAE;oBACN,CAAC,aAAa,GAAG;AACf,wBAAA,IAAI,EAAE;4BACJ,MAAM,EAAE,gBAAgB,CAAC,MAAM;AAC/B,4BAAA,IAAI,EAAE;AACP;AACF;AACF;AACF,aAAA,CAAC;QACJ;AAEA,QAAA,OAAO,IAAI;IACb;uGAvFW,qBAAqB,EAAA,IAAA,EAAA,EAAA,EAAA,MAAA,EAAA,EAAA,CAAA,eAAA,CAAA,UAAA,EAAA,CAAA;2GAArB,qBAAqB,EAAA,CAAA;;2FAArB,qBAAqB,EAAA,UAAA,EAAA,CAAA;kBADjC;;AA2FM,MAAM,qBAAqB,GAAG,MAAM,OAAO,CAAC,MAAM,CAAC,qBAAqB;;AC9F/E;;;AAGG;AACI,MAAM,4BAA4B,GAAG,MAAK;AAC/C,IAAA,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;AAC7B,IAAA,MAAM,CAAC,WAAW,CAAC,iDAAiD,CAAC;AACrE,IAAA,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;AAC3B,IAAA,IAAI,EAAE,KAAK,YAAY,SAAS,CAAC,EAAE;AACjC,QAAA,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC;IACjD;IAEA,KAAK,CAAC,UAAU,GAAG;QACjB,GAAG,KAAK,CAAC,UAAU;AACnB,QAAA,CAAC,8BAA8B,CAAC,eAAe,GAAG,8BAA8B,CAAC;KAClF;AACH;AAEA;;AAEG;AACI,MAAM,0BAA0B,GAAG,MAA2B;IACnE,OAAO,wBAAwB,CAAC,CAAC,qBAAqB,CAAC,4BAA4B,CAAC,EAAE,qBAAqB,CAAC,CAAC;AAC/G;;AC/BA;;AAEG;;;;"}
1
+ {"version":3,"file":"ngssm-store-caching-testing.mjs","names":[],"sources":["../../../projects/ngssm-store/caching/testing/src/ngssm-cached-item-setter.ts","../../../projects/ngssm-store/caching/testing/src/provide-ngssm-caching-testing.ts"],"sourcesContent":["import { inject, Injectable } from '@angular/core';\nimport { TestBed } from '@angular/core/testing';\n\nimport { Store } from 'ngssm-store';\nimport { CachedItemStatus, selectNgssmCachedItem, SetCachedItemAction, updateNgssmCachingState } from 'ngssm-store/caching';\nimport { StoreMock } from 'ngssm-store/testing';\n\n/**\n * Utility service for setting and updating cached items in the StoreMock during tests.\n * Provides methods to apply a SetCachedItemAction, set the status, or set the value of a cached item.\n */\n@Injectable()\nexport class NgssmCachedItemSetter {\n public readonly store = inject(Store) as unknown as StoreMock;\n\n /**\n * Applies a SetCachedItemAction to the StoreMock, updating the cache state.\n * @param action The SetCachedItemAction to apply.\n * @returns The NgssmCachedItemSetter instance for chaining.\n */\n public apply<T = unknown>(action: SetCachedItemAction<T>): NgssmCachedItemSetter {\n this.store.stateValue = updateNgssmCachingState(this.store.stateValue, {\n caches: {\n [action.cachedItemKey]: {\n $set: {\n status: action.status,\n item: action.cachedItem,\n error: action.error\n }\n }\n }\n });\n\n return this;\n }\n\n /**\n * Sets the status of a cached item in the StoreMock.\n * If the key does not exist in cache, it is created with an undefined value.\n *\n * @param cachedItemKey The key of the cached item.\n * @param status The new status to set.\n * @returns The NgssmCachedItemSetter instance for chaining.\n */\n public setCachedItemStatus(cachedItemKey: string, status: CachedItemStatus): NgssmCachedItemSetter {\n if (selectNgssmCachedItem(this.store.stateValue, cachedItemKey)) {\n this.store.stateValue = updateNgssmCachingState(this.store.stateValue, {\n caches: {\n [cachedItemKey]: {\n status: { $set: status }\n }\n }\n });\n } else {\n this.store.stateValue = updateNgssmCachingState(this.store.stateValue, {\n caches: {\n [cachedItemKey]: {\n $set: {\n status\n }\n }\n }\n });\n }\n\n return this;\n }\n\n /**\n * Sets the value of a cached item in the StoreMock.\n * If the key does not exist in cache, it is created with the status CachedItemStatus.notSet.\n *\n * @param cachedItemKey The key of the cached item.\n * @param value The value to set.\n * @returns The NgssmCachedItemSetter instance for chaining.\n */\n public setCachedItemValue<T>(cachedItemKey: string, value?: T): NgssmCachedItemSetter {\n if (selectNgssmCachedItem(this.store.stateValue, cachedItemKey)) {\n this.store.stateValue = updateNgssmCachingState(this.store.stateValue, {\n caches: {\n [cachedItemKey]: {\n item: { $set: value }\n }\n }\n });\n } else {\n this.store.stateValue = updateNgssmCachingState(this.store.stateValue, {\n caches: {\n [cachedItemKey]: {\n $set: {\n status: CachedItemStatus.notSet,\n item: value\n }\n }\n }\n });\n }\n\n return this;\n }\n}\n\nexport const ngssmCachedItemSetter = () => TestBed.inject(NgssmCachedItemSetter);\n","import { EnvironmentProviders, inject, makeEnvironmentProviders, provideAppInitializer } from '@angular/core';\n\nimport { Logger, Store } from 'ngssm-store';\nimport { StoreMock } from 'ngssm-store/testing';\nimport { NgssmCachingStateSpecification } from 'ngssm-store/caching';\n\nimport { NgssmCachedItemSetter } from './ngssm-cached-item-setter';\n\n/**\n * App initializer that sets up the NgssmCaching state in the StoreMock for testing purposes.\n * Throws an error if StoreMock is not registered.\n */\nexport const ngssmCachingStateInitializer = () => {\n const logger = inject(Logger);\n logger.information('[ngssm-caching-testing] Initialization of state');\n const store = inject(Store);\n if (!(store instanceof StoreMock)) {\n throw new Error('StoreMock is not registered.');\n }\n\n store.stateValue = {\n ...store.stateValue,\n [NgssmCachingStateSpecification.featureStateKey]: NgssmCachingStateSpecification.initialState\n };\n};\n\n/**\n * Provides environment providers for NgssmCaching testing, including the state initializer.\n */\nexport const provideNgssmCachingTesting = (): EnvironmentProviders => {\n return makeEnvironmentProviders([provideAppInitializer(ngssmCachingStateInitializer), NgssmCachedItemSetter]);\n};\n"],"mappings":";;;;;;AAYA,IAAa,wBAAb,MAAa,sBAAqB;CAChB,QAAQ,OAAO,KAAK;CAO7B,MAAmB,QAA8B;EACtD,KAAK,MAAM,aAAa,wBAAwB,KAAK,MAAM,YAAY,EACrE,QAAQ,GACL,OAAO,gBAAgB,EACtB,MAAM;GACJ,QAAQ,OAAO;GACf,MAAM,OAAO;GACb,OAAO,OAAO;EACf,EACF,EACF,EACF,CAAA;EAED,OAAO;CACT;CAUO,oBAAoB,eAAuB,QAAwB;EACxE,IAAI,sBAAsB,KAAK,MAAM,YAAY,aAAa,GAC5D,KAAK,MAAM,aAAa,wBAAwB,KAAK,MAAM,YAAY,EACrE,QAAQ,GACL,gBAAgB,EACf,QAAQ,EAAE,MAAM,OAAM,EACvB,EACF,EACF,CAAA;OAED,KAAK,MAAM,aAAa,wBAAwB,KAAK,MAAM,YAAY,EACrE,QAAQ,GACL,gBAAgB,EACf,MAAM,EACJ,OACD,EACF,EACF,EACF,CAAA;EAGH,OAAO;CACT;CAUO,mBAAsB,eAAuB,OAAS;EAC3D,IAAI,sBAAsB,KAAK,MAAM,YAAY,aAAa,GAC5D,KAAK,MAAM,aAAa,wBAAwB,KAAK,MAAM,YAAY,EACrE,QAAQ,GACL,gBAAgB,EACf,MAAM,EAAE,MAAM,MAAK,EACpB,EACF,EACF,CAAA;OAED,KAAK,MAAM,aAAa,wBAAwB,KAAK,MAAM,YAAY,EACrE,QAAQ,GACL,gBAAgB,EACf,MAAM;GACJ,QAAQ,iBAAiB;GACzB,MAAM;EACP,EACF,EACF,EACF,CAAA;EAGH,OAAO;CACT;CAvFW,OAAA,OAAA,GAAA,mBAAA;EAAA,YAAA;EAAA,SAAA;EAAA,UAAA;EAAA,MAAA;EAAqB,MAAA,CAAA;EAAA,QAAA,GAAA,gBAAA;CAAA,CAAA;CAArB,OAAA,QAAA,GAAA,sBAAA;EAAA,YAAA;EAAA,SAAA;EAAA,UAAA;EAAA,MAAA;CAAqB,CAAA;;AAArB,GAAA,yBAAA;CAAA,YAAA;CAAA,SAAA;CAAA,UAAA;CAAA,MAAA;CAAqB,YAAA,CAAA,EADjC,MAAA,WAAA,CAAA;;AA2FD,MAAa,8BAA8B,QAAQ,OAAO,qBAAqB;AC1F/E,MAAa,qCAAoC;CAE/C,AADe,OAAO,MACtB,CAAA,CAAO,YAAY,iDAAiD;CACpE,MAAM,QAAQ,OAAO,KAAK;CAC1B,IAAI,EAAE,iBAAiB,YACrB,MAAM,IAAI,MAAM,8BAA8B;CAGhD,MAAM,aAAa;EACjB,GAAG,MAAM;GACR,+BAA+B,kBAAkB,+BAA+B;CAClF;AACH;AAKA,MAAa,mCAAwD;CACnE,OAAO,yBAAyB,CAAC,sBAAsB,4BAA4B,GAAG,qBAAqB,CAAC;AAC9G"}
@@ -1,147 +1,115 @@
1
- import * as i0 from '@angular/core';
2
- import { Injectable, makeEnvironmentProviders, inject, computed } from '@angular/core';
3
- import { NgSsmFeatureState, provideReducers, Store } from 'ngssm-store';
4
- import { __decorate } from 'tslib';
5
- import update from 'immutability-helper';
6
-
1
+ import * as i0 from "@angular/core";
2
+ import { Injectable, computed, inject, makeEnvironmentProviders } from "@angular/core";
3
+ import { NgSsmFeatureState, Store, provideReducers } from "ngssm-store";
4
+ import { __decorate } from "tslib";
5
+ import update from "immutability-helper";
7
6
  var NgssmCachingActionType;
8
- (function (NgssmCachingActionType) {
9
- NgssmCachingActionType["setCachedItem"] = "[NgssmCachingActionType] setCachedItem";
10
- NgssmCachingActionType["unsetCachedItem"] = "[NgssmCachingActionType] unsetCachedItem";
7
+ (function(NgssmCachingActionType) {
8
+ NgssmCachingActionType["setCachedItem"] = "[NgssmCachingActionType] setCachedItem";
9
+ NgssmCachingActionType["unsetCachedItem"] = "[NgssmCachingActionType] unsetCachedItem";
11
10
  })(NgssmCachingActionType || (NgssmCachingActionType = {}));
12
-
13
11
  var CachedItemStatus;
14
- (function (CachedItemStatus) {
15
- CachedItemStatus["notSet"] = "Not Set";
16
- CachedItemStatus["loading"] = "Loading";
17
- CachedItemStatus["set"] = "Set";
18
- CachedItemStatus["error"] = "Error";
12
+ (function(CachedItemStatus) {
13
+ CachedItemStatus["notSet"] = "Not Set";
14
+ CachedItemStatus["loading"] = "Loading";
15
+ CachedItemStatus["set"] = "Set";
16
+ CachedItemStatus["error"] = "Error";
19
17
  })(CachedItemStatus || (CachedItemStatus = {}));
20
-
21
- /**
22
- * Represents an action to set a cached item in the state. If key is already set in the state, the value is replaced by the new one.
23
- *
24
- * @template TData The type of the cached item's data.
25
- * @implements {Action}
26
- *
27
- * @property {string} cachedItemKey - The unique key identifying the cached item.
28
- * @property {TData} cachedItem - The data to be cached.
29
- * @property {CachedItemStatus} [status=CachedItemStatus.set] - The status of the cached item operation.
30
- * @property {string} [error] - An optional error message if the caching operation failed.
31
- *
32
- * @remarks
33
- * This action is dispatched to update the cache with a new or modified item, along with its status and any error information.
34
- */
35
- class SetCachedItemAction {
36
- cachedItemKey;
37
- cachedItem;
38
- status;
39
- error;
40
- type = NgssmCachingActionType.setCachedItem;
41
- constructor(cachedItemKey, cachedItem, status = CachedItemStatus.set, error) {
42
- this.cachedItemKey = cachedItemKey;
43
- this.cachedItem = cachedItem;
44
- this.status = status;
45
- this.error = error;
46
- }
47
- }
48
-
49
- class UnsetCachedItemAction {
50
- cachedItemKey;
51
- type = NgssmCachingActionType.unsetCachedItem;
52
- constructor(cachedItemKey) {
53
- this.cachedItemKey = cachedItemKey;
54
- }
55
- }
56
-
18
+ var SetCachedItemAction = class {
19
+ cachedItemKey;
20
+ cachedItem;
21
+ status;
22
+ error;
23
+ type = NgssmCachingActionType.setCachedItem;
24
+ constructor(cachedItemKey, cachedItem, status = CachedItemStatus.set, error) {
25
+ this.cachedItemKey = cachedItemKey;
26
+ this.cachedItem = cachedItem;
27
+ this.status = status;
28
+ this.error = error;
29
+ }
30
+ };
31
+ var UnsetCachedItemAction = class {
32
+ cachedItemKey;
33
+ type = NgssmCachingActionType.unsetCachedItem;
34
+ constructor(cachedItemKey) {
35
+ this.cachedItemKey = cachedItemKey;
36
+ }
37
+ };
57
38
  const selectNgssmCachingState = (state) => state[NgssmCachingStateSpecification.featureStateKey];
58
- const updateNgssmCachingState = (state, command) => update(state, {
59
- [NgssmCachingStateSpecification.featureStateKey]: command
60
- });
39
+ const updateNgssmCachingState = (state, command) => update(state, { [NgssmCachingStateSpecification.featureStateKey]: command });
61
40
  let NgssmCachingStateSpecification = class NgssmCachingStateSpecification {
62
- static featureStateKey = 'ngssm-caching-state';
63
- static initialState = {
64
- caches: {}
65
- };
41
+ static featureStateKey = "ngssm-caching-state";
42
+ static initialState = { caches: {} };
66
43
  };
67
- NgssmCachingStateSpecification = __decorate([
68
- NgSsmFeatureState({
69
- featureStateKey: NgssmCachingStateSpecification.featureStateKey,
70
- initialState: NgssmCachingStateSpecification.initialState
71
- })
72
- ], NgssmCachingStateSpecification);
73
-
44
+ NgssmCachingStateSpecification = __decorate([NgSsmFeatureState({
45
+ featureStateKey: NgssmCachingStateSpecification.featureStateKey,
46
+ initialState: NgssmCachingStateSpecification.initialState
47
+ })], NgssmCachingStateSpecification);
74
48
  const selectNgssmCachedItem = (state, key) => selectNgssmCachingState(state).caches[key];
75
-
76
- class CachedItemReducer {
77
- processedActions = [NgssmCachingActionType.setCachedItem, NgssmCachingActionType.unsetCachedItem];
78
- updateState(state, action) {
79
- switch (action.type) {
80
- case NgssmCachingActionType.setCachedItem: {
81
- const setCachedItemAction = action;
82
- return updateNgssmCachingState(state, {
83
- caches: {
84
- [setCachedItemAction.cachedItemKey]: {
85
- $set: {
86
- status: setCachedItemAction.status,
87
- error: setCachedItemAction.error,
88
- item: setCachedItemAction.cachedItem
89
- }
90
- }
91
- }
92
- });
93
- }
94
- case NgssmCachingActionType.unsetCachedItem: {
95
- const unsetCachedItemAction = action;
96
- if (selectNgssmCachedItem(state, unsetCachedItemAction.cachedItemKey) === undefined) {
97
- return state;
98
- }
99
- return updateNgssmCachingState(state, {
100
- caches: { $unset: [unsetCachedItemAction.cachedItemKey] }
101
- });
102
- }
103
- }
104
- return state;
105
- }
106
- static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.1.1", ngImport: i0, type: CachedItemReducer, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
107
- static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.1.1", ngImport: i0, type: CachedItemReducer });
108
- }
109
- i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.1.1", ngImport: i0, type: CachedItemReducer, decorators: [{
110
- type: Injectable
111
- }] });
112
-
49
+ var CachedItemReducer = class CachedItemReducer {
50
+ processedActions = [NgssmCachingActionType.setCachedItem, NgssmCachingActionType.unsetCachedItem];
51
+ updateState(state, action) {
52
+ switch (action.type) {
53
+ case NgssmCachingActionType.setCachedItem: {
54
+ const setCachedItemAction = action;
55
+ return updateNgssmCachingState(state, { caches: { [setCachedItemAction.cachedItemKey]: { $set: {
56
+ status: setCachedItemAction.status,
57
+ error: setCachedItemAction.error,
58
+ item: setCachedItemAction.cachedItem
59
+ } } } });
60
+ }
61
+ case NgssmCachingActionType.unsetCachedItem: {
62
+ const unsetCachedItemAction = action;
63
+ if (selectNgssmCachedItem(state, unsetCachedItemAction.cachedItemKey) === void 0) return state;
64
+ return updateNgssmCachingState(state, { caches: { $unset: [unsetCachedItemAction.cachedItemKey] } });
65
+ }
66
+ }
67
+ return state;
68
+ }
69
+ static ɵfac = i0.ɵɵngDeclareFactory({
70
+ minVersion: "12.0.0",
71
+ version: "22.2.0",
72
+ ngImport: i0,
73
+ type: CachedItemReducer,
74
+ deps: [],
75
+ target: i0.ɵɵFactoryTarget.Injectable
76
+ });
77
+ static ɵprov = i0.ɵɵngDeclareInjectable({
78
+ minVersion: "12.0.0",
79
+ version: "22.2.0",
80
+ ngImport: i0,
81
+ type: CachedItemReducer
82
+ });
83
+ };
84
+ i0.ɵɵngDeclareClassMetadata({
85
+ minVersion: "12.0.0",
86
+ version: "22.2.0",
87
+ ngImport: i0,
88
+ type: CachedItemReducer,
89
+ decorators: [{ type: Injectable }]
90
+ });
113
91
  const provideNgssmCaching = () => {
114
- return makeEnvironmentProviders([provideReducers(CachedItemReducer)]);
92
+ return makeEnvironmentProviders([provideReducers(CachedItemReducer)]);
115
93
  };
116
-
117
94
  const cachedItemToSignal = (key, options) => {
118
- const store = inject(Store);
119
- const usedOptions = options ?? { type: 'item' };
120
- if (!usedOptions.type) {
121
- usedOptions.type = 'item';
122
- }
123
- switch (usedOptions.type) {
124
- case 'status':
125
- return {
126
- key,
127
- value: computed(() => selectNgssmCachedItem(store.state(), key)?.status ?? options?.defaultValue ?? CachedItemStatus.notSet)
128
- };
129
- case 'error':
130
- return {
131
- key,
132
- value: computed(() => selectNgssmCachedItem(store.state(), key)?.error ?? options?.defaultValue)
133
- };
134
- case 'item':
135
- return {
136
- key,
137
- value: computed(() => selectNgssmCachedItem(store.state(), key)?.item ?? options?.defaultValue)
138
- };
139
- }
95
+ const store = inject(Store);
96
+ const usedOptions = options ?? { type: "item" };
97
+ if (!usedOptions.type) usedOptions.type = "item";
98
+ switch (usedOptions.type) {
99
+ case "status": return {
100
+ key,
101
+ value: computed(() => selectNgssmCachedItem(store.state(), key)?.status ?? options?.defaultValue ?? CachedItemStatus.notSet)
102
+ };
103
+ case "error": return {
104
+ key,
105
+ value: computed(() => selectNgssmCachedItem(store.state(), key)?.error ?? options?.defaultValue)
106
+ };
107
+ case "item": return {
108
+ key,
109
+ value: computed(() => selectNgssmCachedItem(store.state(), key)?.item ?? options?.defaultValue)
110
+ };
111
+ }
140
112
  };
141
-
142
- /**
143
- * Generated bundle index. Do not edit.
144
- */
145
-
146
113
  export { CachedItemStatus, NgssmCachingActionType, NgssmCachingStateSpecification, SetCachedItemAction, UnsetCachedItemAction, cachedItemToSignal, provideNgssmCaching, selectNgssmCachedItem, selectNgssmCachingState, updateNgssmCachingState };
147
- //# sourceMappingURL=ngssm-store-caching.mjs.map
114
+
115
+ //# sourceMappingURL=ngssm-store-caching.mjs.map