ngssm-store 22.0.1 → 22.2.0
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 +3 -52
- package/fesm2022/ngssm-store-caching-testing.mjs +61 -126
- package/fesm2022/ngssm-store-caching-testing.mjs.map +1 -1
- package/fesm2022/ngssm-store-caching.mjs +102 -134
- package/fesm2022/ngssm-store-caching.mjs.map +1 -1
- package/fesm2022/ngssm-store-testing.mjs +36 -64
- package/fesm2022/ngssm-store-testing.mjs.map +1 -1
- package/fesm2022/ngssm-store-visibility-testing.mjs +53 -75
- package/fesm2022/ngssm-store-visibility-testing.mjs.map +1 -1
- package/fesm2022/ngssm-store-visibility.mjs +441 -244
- package/fesm2022/ngssm-store-visibility.mjs.map +1 -1
- package/fesm2022/ngssm-store.mjs +385 -488
- package/fesm2022/ngssm-store.mjs.map +1 -1
- package/package.json +1 -1
- package/types/ngssm-store-caching-testing.d.ts +35 -39
- package/types/ngssm-store-caching.d.ts +43 -55
- package/types/ngssm-store-testing.d.ts +13 -19
- package/types/ngssm-store-visibility-testing.d.ts +29 -33
- package/types/ngssm-store-visibility.d.ts +107 -95
- package/types/ngssm-store.d.ts +183 -232
package/fesm2022/ngssm-store.mjs
CHANGED
|
@@ -1,518 +1,415 @@
|
|
|
1
|
-
import * as i0 from
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
const NGSSM_REDUCER = new InjectionToken('NGSSM_REDUCER');
|
|
1
|
+
import * as i0 from "@angular/core";
|
|
2
|
+
import { ApplicationRef, Directive, EnvironmentInjector, Injectable, InjectionToken, computed, inject, makeEnvironmentProviders, provideAppInitializer, runInInjectionContext, signal } from "@angular/core";
|
|
3
|
+
import update from "immutability-helper";
|
|
4
|
+
import { Subject, takeUntil } from "rxjs";
|
|
5
|
+
const NGSSM_REDUCER = new InjectionToken("NGSSM_REDUCER");
|
|
7
6
|
const provideReducer = (reducer) => {
|
|
8
|
-
|
|
7
|
+
return makeEnvironmentProviders([{
|
|
8
|
+
provide: NGSSM_REDUCER,
|
|
9
|
+
useClass: reducer,
|
|
10
|
+
multi: true
|
|
11
|
+
}]);
|
|
9
12
|
};
|
|
10
13
|
const provideReducers = (...reducers) => {
|
|
11
|
-
|
|
14
|
+
return makeEnvironmentProviders(reducers.map((reducer) => ({
|
|
15
|
+
provide: NGSSM_REDUCER,
|
|
16
|
+
useClass: reducer,
|
|
17
|
+
multi: true
|
|
18
|
+
})));
|
|
12
19
|
};
|
|
13
|
-
|
|
14
|
-
const NGSSM_EFFECT = new InjectionToken('NGSSM_EFFECT');
|
|
20
|
+
const NGSSM_EFFECT = new InjectionToken("NGSSM_EFFECT");
|
|
15
21
|
const provideEffect = (effect) => {
|
|
16
|
-
|
|
22
|
+
return makeEnvironmentProviders([{
|
|
23
|
+
provide: NGSSM_EFFECT,
|
|
24
|
+
useClass: effect,
|
|
25
|
+
multi: true
|
|
26
|
+
}]);
|
|
17
27
|
};
|
|
18
28
|
const provideEffects = (...effects) => {
|
|
19
|
-
|
|
29
|
+
return makeEnvironmentProviders(effects.map((effect) => ({
|
|
30
|
+
provide: NGSSM_EFFECT,
|
|
31
|
+
useClass: effect,
|
|
32
|
+
multi: true
|
|
33
|
+
})));
|
|
20
34
|
};
|
|
21
35
|
const provideEffectFunc = (actionType, effectFunc) => {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
multi: true
|
|
34
|
-
}
|
|
35
|
-
]);
|
|
36
|
+
return makeEnvironmentProviders([{
|
|
37
|
+
provide: NGSSM_EFFECT,
|
|
38
|
+
useFactory: () => {
|
|
39
|
+
return {
|
|
40
|
+
processedActions: [actionType],
|
|
41
|
+
processAction: (_, state, action) => effectFunc(state, action),
|
|
42
|
+
isFunc: true
|
|
43
|
+
};
|
|
44
|
+
},
|
|
45
|
+
multi: true
|
|
46
|
+
}]);
|
|
36
47
|
};
|
|
37
|
-
|
|
38
|
-
const NGSSM_STATE_INITIALIZER = new InjectionToken('NGSSM_STATE_INITIALIZER');
|
|
39
|
-
|
|
48
|
+
const NGSSM_STATE_INITIALIZER = new InjectionToken("NGSSM_STATE_INITIALIZER");
|
|
40
49
|
var LogLevel;
|
|
41
|
-
(function
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
50
|
+
(function(LogLevel) {
|
|
51
|
+
LogLevel["debug"] = "Debug";
|
|
52
|
+
LogLevel["information"] = "Information";
|
|
53
|
+
LogLevel["error"] = "Error";
|
|
45
54
|
})(LogLevel || (LogLevel = {}));
|
|
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
|
-
|
|
55
|
+
var Logger = class Logger {
|
|
56
|
+
_logEvents$ = new Subject();
|
|
57
|
+
get logEvents$() {
|
|
58
|
+
return this._logEvents$.asObservable();
|
|
59
|
+
}
|
|
60
|
+
debug(message, payload) {
|
|
61
|
+
this.log(LogLevel.debug, message, payload);
|
|
62
|
+
}
|
|
63
|
+
information(message, payload) {
|
|
64
|
+
this.log(LogLevel.information, message, payload);
|
|
65
|
+
}
|
|
66
|
+
error(message, payload) {
|
|
67
|
+
this.log(LogLevel.error, message, payload);
|
|
68
|
+
}
|
|
69
|
+
log(level, message, payload) {
|
|
70
|
+
this._logEvents$.next({
|
|
71
|
+
level,
|
|
72
|
+
message,
|
|
73
|
+
payload
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
static ɵfac = i0.ɵɵngDeclareFactory({
|
|
77
|
+
minVersion: "12.0.0",
|
|
78
|
+
version: "22.2.0",
|
|
79
|
+
ngImport: i0,
|
|
80
|
+
type: Logger,
|
|
81
|
+
deps: [],
|
|
82
|
+
target: i0.ɵɵFactoryTarget.Injectable
|
|
83
|
+
});
|
|
84
|
+
static ɵprov = i0.ɵɵngDeclareInjectable({
|
|
85
|
+
minVersion: "12.0.0",
|
|
86
|
+
version: "22.2.0",
|
|
87
|
+
ngImport: i0,
|
|
88
|
+
type: Logger,
|
|
89
|
+
providedIn: "root"
|
|
90
|
+
});
|
|
91
|
+
};
|
|
92
|
+
i0.ɵɵngDeclareClassMetadata({
|
|
93
|
+
minVersion: "12.0.0",
|
|
94
|
+
version: "22.2.0",
|
|
95
|
+
ngImport: i0,
|
|
96
|
+
type: Logger,
|
|
97
|
+
decorators: [{
|
|
98
|
+
type: Injectable,
|
|
99
|
+
args: [{ providedIn: "root" }]
|
|
100
|
+
}]
|
|
101
|
+
});
|
|
102
|
+
var ConsoleAppender = class ConsoleAppender {
|
|
103
|
+
logger = inject(Logger);
|
|
104
|
+
stopEvent$ = new Subject();
|
|
105
|
+
start(contextName) {
|
|
106
|
+
this.logger.logEvents$.pipe(takeUntil(this.stopEvent$)).subscribe((logEvent) => {
|
|
107
|
+
let logFunction;
|
|
108
|
+
switch (logEvent.level) {
|
|
109
|
+
case LogLevel.error:
|
|
110
|
+
logFunction = console.error;
|
|
111
|
+
break;
|
|
112
|
+
default: logFunction = console.log;
|
|
113
|
+
}
|
|
114
|
+
const now = (/* @__PURE__ */ new Date()).toLocaleString();
|
|
115
|
+
const prefix = contextName ? `[${contextName}] ` : "";
|
|
116
|
+
if (logEvent.payload) logFunction(`${prefix}[${now}] [${logEvent.level}] ${logEvent.message}`, logEvent.payload);
|
|
117
|
+
else logFunction(`${prefix}[${now}] [${logEvent.level}] ${logEvent.message}`);
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
stop() {
|
|
121
|
+
this.stopEvent$.next(true);
|
|
122
|
+
}
|
|
123
|
+
static ɵfac = i0.ɵɵngDeclareFactory({
|
|
124
|
+
minVersion: "12.0.0",
|
|
125
|
+
version: "22.2.0",
|
|
126
|
+
ngImport: i0,
|
|
127
|
+
type: ConsoleAppender,
|
|
128
|
+
deps: [],
|
|
129
|
+
target: i0.ɵɵFactoryTarget.Injectable
|
|
130
|
+
});
|
|
131
|
+
static ɵprov = i0.ɵɵngDeclareInjectable({
|
|
132
|
+
minVersion: "12.0.0",
|
|
133
|
+
version: "22.2.0",
|
|
134
|
+
ngImport: i0,
|
|
135
|
+
type: ConsoleAppender,
|
|
136
|
+
providedIn: "root"
|
|
137
|
+
});
|
|
138
|
+
};
|
|
139
|
+
i0.ɵɵngDeclareClassMetadata({
|
|
140
|
+
minVersion: "12.0.0",
|
|
141
|
+
version: "22.2.0",
|
|
142
|
+
ngImport: i0,
|
|
143
|
+
type: ConsoleAppender,
|
|
144
|
+
decorators: [{
|
|
145
|
+
type: Injectable,
|
|
146
|
+
args: [{ providedIn: "root" }]
|
|
147
|
+
}]
|
|
148
|
+
});
|
|
149
|
+
const NGSSM_CONSOLE_APPENDER_NAME = new InjectionToken("NGSSM_CONSOLE_APPENDER_NAME");
|
|
116
150
|
const startConsoleAppenderFactory = () => {
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
};
|
|
151
|
+
return () => {
|
|
152
|
+
const name = inject(NGSSM_CONSOLE_APPENDER_NAME);
|
|
153
|
+
inject(ConsoleAppender).start(name);
|
|
154
|
+
};
|
|
122
155
|
};
|
|
123
156
|
const provideConsoleAppender = (name) => {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
},
|
|
129
|
-
provideAppInitializer(startConsoleAppenderFactory())
|
|
130
|
-
]);
|
|
157
|
+
return makeEnvironmentProviders([{
|
|
158
|
+
provide: NGSSM_CONSOLE_APPENDER_NAME,
|
|
159
|
+
useValue: name
|
|
160
|
+
}, provideAppInitializer(startConsoleAppenderFactory())]);
|
|
131
161
|
};
|
|
132
|
-
|
|
133
162
|
const featureStateSpecifications = [];
|
|
134
163
|
const NgSsmFeatureState = (specification) => {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
};
|
|
164
|
+
return (target) => {
|
|
165
|
+
featureStateSpecifications.push(specification);
|
|
166
|
+
};
|
|
139
167
|
};
|
|
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
|
-
this.logger.information(`[processNextAction] Start processing action '${nextAction.type}...`, nextAction);
|
|
275
|
-
try {
|
|
276
|
-
// Apply reducers to update the state.
|
|
277
|
-
const updatedState = this.applyReducers(nextAction);
|
|
278
|
-
if (this.forceChangeDetectionAfterActionProcessed) {
|
|
279
|
-
this.applicationRef.tick();
|
|
280
|
-
}
|
|
281
|
-
// Execute effects for the action.
|
|
282
|
-
const effects = this.effectsPerActionType.get(nextAction.type) ?? [];
|
|
283
|
-
this.logger.debug(`[Store] ${effects.length} effects found to process the action ${nextAction.type}`, effects);
|
|
284
|
-
effects.forEach((effect) => {
|
|
285
|
-
if (effect.isFunc === true) {
|
|
286
|
-
runInInjectionContext(this.injector, () => this.runEffect(effect, updatedState, nextAction));
|
|
287
|
-
}
|
|
288
|
-
else {
|
|
289
|
-
this.runEffect(effect, updatedState, nextAction);
|
|
290
|
-
}
|
|
291
|
-
});
|
|
292
|
-
}
|
|
293
|
-
catch (error) {
|
|
294
|
-
this.logger.error(`Error when processing action ${nextAction.type}`, {
|
|
295
|
-
error,
|
|
296
|
-
action: nextAction
|
|
297
|
-
});
|
|
298
|
-
}
|
|
299
|
-
finally {
|
|
300
|
-
this.logger.information(`[processNextAction] action '${nextAction.type} processed.`, nextAction);
|
|
301
|
-
// Process the next action asynchronously
|
|
302
|
-
this.addTaskForNextAction();
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
/**
|
|
306
|
-
* Applies reducers to the current state based on the dispatched action.
|
|
307
|
-
* Updates the state if there are changes and sets the processed action signal.
|
|
308
|
-
*
|
|
309
|
-
* @param nextAction - The action being processed.
|
|
310
|
-
* @returns The updated state after applying reducers.
|
|
311
|
-
*/
|
|
312
|
-
applyReducers(nextAction) {
|
|
313
|
-
try {
|
|
314
|
-
const reducers = this.reducersPerActionType.get(nextAction.type) ?? [];
|
|
315
|
-
this.logger.debug(`[Store] ${reducers.length} reducers found to process the action ${nextAction.type}`, reducers);
|
|
316
|
-
const currentState = this._state$.getValue();
|
|
317
|
-
const updatedState = reducers.reduce((p, c) => c.updateState(p, nextAction), currentState);
|
|
318
|
-
if (updatedState !== currentState) {
|
|
319
|
-
this._state$.next(updatedState);
|
|
320
|
-
}
|
|
321
|
-
return updatedState;
|
|
322
|
-
}
|
|
323
|
-
finally {
|
|
324
|
-
this.logger.debug(`[Store] Notify action ${nextAction.type} as processed`);
|
|
325
|
-
this._processedAction.set(nextAction);
|
|
326
|
-
this._processedAction$.next(nextAction);
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
/**
|
|
330
|
-
* Executes an effect for a given action.
|
|
331
|
-
* Handles any errors that occur during effect execution.
|
|
332
|
-
*
|
|
333
|
-
* @param effect - The effect to execute.
|
|
334
|
-
* @param updatedState - The updated state after reducers have been applied.
|
|
335
|
-
* @param nextAction - The action being processed.
|
|
336
|
-
*/
|
|
337
|
-
runEffect(effect, updatedState, nextAction) {
|
|
338
|
-
try {
|
|
339
|
-
effect.processAction(this, updatedState, nextAction);
|
|
340
|
-
}
|
|
341
|
-
catch (error) {
|
|
342
|
-
this.logger.error(`Unable to process action ${nextAction.type} by effect ${effect}`, {
|
|
343
|
-
error,
|
|
344
|
-
action: nextAction,
|
|
345
|
-
processor: effect
|
|
346
|
-
});
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
/**
|
|
350
|
-
* Initializes reducers or effects and maps them to their corresponding action types.
|
|
351
|
-
*
|
|
352
|
-
* @param processors - The array of reducers or effects to initialize.
|
|
353
|
-
* @param processorMap - The map to store processors by action type.
|
|
354
|
-
* @param getProcessedActions - A function to retrieve the action types processed by a processor.
|
|
355
|
-
*/
|
|
356
|
-
initializeProcessors(processors, processorMap, getProcessedActions) {
|
|
357
|
-
processors.forEach((processor) => {
|
|
358
|
-
this.logger.information('[Store] ------> initialization of ', processor);
|
|
359
|
-
getProcessedActions(processor).forEach((actionType) => {
|
|
360
|
-
const storedProcessors = processorMap.get(actionType) ?? [];
|
|
361
|
-
if (storedProcessors.length === 0) {
|
|
362
|
-
processorMap.set(actionType, storedProcessors);
|
|
363
|
-
}
|
|
364
|
-
storedProcessors.push(processor);
|
|
365
|
-
});
|
|
366
|
-
});
|
|
367
|
-
}
|
|
368
|
-
addTaskForNextAction() {
|
|
369
|
-
if (this.useMacroTasks) {
|
|
370
|
-
setTimeout(() => this.processNextAction());
|
|
371
|
-
}
|
|
372
|
-
else {
|
|
373
|
-
Promise.resolve().then(() => this.processNextAction());
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "22.0.5", ngImport: i0, type: Store, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
|
|
377
|
-
static ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "22.0.5", ngImport: i0, type: Store, providedIn: 'root' });
|
|
378
|
-
}
|
|
379
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.5", ngImport: i0, type: Store, decorators: [{
|
|
380
|
-
type: Injectable,
|
|
381
|
-
args: [{
|
|
382
|
-
providedIn: 'root'
|
|
383
|
-
}]
|
|
384
|
-
}], ctorParameters: () => [] });
|
|
385
|
-
|
|
386
|
-
const NGSSM_COMPONENT_WITH_FEATURE_STATE = new InjectionToken('NGSSM_COMPONENT_WITH_FEATURE_STATE');
|
|
387
|
-
|
|
168
|
+
var Store = class Store {
|
|
169
|
+
useMacroTasks = true;
|
|
170
|
+
forceChangeDetectionAfterActionProcessed = false;
|
|
171
|
+
logger = inject(Logger);
|
|
172
|
+
reducers = inject(NGSSM_REDUCER, { optional: true });
|
|
173
|
+
effects = inject(NGSSM_EFFECT, { optional: true });
|
|
174
|
+
initializers = inject(NGSSM_STATE_INITIALIZER, { optional: true });
|
|
175
|
+
injector = inject(EnvironmentInjector);
|
|
176
|
+
applicationRef = inject(ApplicationRef);
|
|
177
|
+
actionQueue = [];
|
|
178
|
+
reducersPerActionType = /* @__PURE__ */ new Map();
|
|
179
|
+
effectsPerActionType = /* @__PURE__ */ new Map();
|
|
180
|
+
_stateSignal = signal({}, ...ngDevMode ? [{ debugName: "_stateSignal" }] : /* istanbul ignore next */ []);
|
|
181
|
+
_processedAction = signal({ type: "" }, ...ngDevMode ? [{ debugName: "_processedAction" }] : /* istanbul ignore next */ []);
|
|
182
|
+
constructor() {
|
|
183
|
+
this.logger.information("[Store] ---> state initialization...");
|
|
184
|
+
let state = this._stateSignal();
|
|
185
|
+
state = featureStateSpecifications.reduce((p, c) => update(p, { [c.featureStateKey]: { $set: c.initialState } }), state);
|
|
186
|
+
(this.initializers ?? []).forEach((initializer) => {
|
|
187
|
+
this.logger.information("[Store] ------> calling initializer", initializer);
|
|
188
|
+
state = initializer.initializeState(state);
|
|
189
|
+
});
|
|
190
|
+
this._stateSignal.set(state);
|
|
191
|
+
this.logger.information(`[Store] ---> initialization of ${(this.reducers ?? []).length} reducers...`);
|
|
192
|
+
this.initializeProcessors(this.reducers ?? [], this.reducersPerActionType, (r) => r.processedActions);
|
|
193
|
+
this.logger.information(`[Store] ---> initialization of ${(this.effects ?? []).length} effects...`);
|
|
194
|
+
this.initializeProcessors(this.effects ?? [], this.effectsPerActionType, (e) => e.processedActions);
|
|
195
|
+
}
|
|
196
|
+
get processedAction() {
|
|
197
|
+
return this._processedAction.asReadonly();
|
|
198
|
+
}
|
|
199
|
+
get state() {
|
|
200
|
+
return this._stateSignal.asReadonly();
|
|
201
|
+
}
|
|
202
|
+
dispatchAction(action) {
|
|
203
|
+
this.actionQueue.push(action);
|
|
204
|
+
this.logger.debug(`Action of type '${action.type}' added to the queue => ${this.actionQueue.length} pending actions`, action);
|
|
205
|
+
this.addTaskForNextAction();
|
|
206
|
+
}
|
|
207
|
+
dispatchActionType(type) {
|
|
208
|
+
this.dispatchAction({ type });
|
|
209
|
+
}
|
|
210
|
+
processNextAction() {
|
|
211
|
+
const nextAction = this.actionQueue.shift();
|
|
212
|
+
if (!nextAction) {
|
|
213
|
+
this.logger.debug("[processNextAction] No action to process");
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
this.logger.information(`[processNextAction] Start processing action '${nextAction.type}...`, nextAction);
|
|
217
|
+
try {
|
|
218
|
+
const updatedState = this.applyReducers(nextAction);
|
|
219
|
+
if (this.forceChangeDetectionAfterActionProcessed) this.applicationRef.tick();
|
|
220
|
+
const effects = this.effectsPerActionType.get(nextAction.type) ?? [];
|
|
221
|
+
this.logger.debug(`[Store] ${effects.length} effects found to process the action ${nextAction.type}`, effects);
|
|
222
|
+
effects.forEach((effect) => {
|
|
223
|
+
if (effect.isFunc === true) runInInjectionContext(this.injector, () => this.runEffect(effect, updatedState, nextAction));
|
|
224
|
+
else this.runEffect(effect, updatedState, nextAction);
|
|
225
|
+
});
|
|
226
|
+
} catch (error) {
|
|
227
|
+
this.logger.error(`Error when processing action ${nextAction.type}`, {
|
|
228
|
+
error,
|
|
229
|
+
action: nextAction
|
|
230
|
+
});
|
|
231
|
+
} finally {
|
|
232
|
+
this.logger.information(`[processNextAction] action '${nextAction.type} processed.`, nextAction);
|
|
233
|
+
this.addTaskForNextAction();
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
applyReducers(nextAction) {
|
|
237
|
+
try {
|
|
238
|
+
const reducers = this.reducersPerActionType.get(nextAction.type) ?? [];
|
|
239
|
+
this.logger.debug(`[Store] ${reducers.length} reducers found to process the action ${nextAction.type}`, reducers);
|
|
240
|
+
const currentState = this._stateSignal();
|
|
241
|
+
const updatedState = reducers.reduce((p, c) => c.updateState(p, nextAction), currentState);
|
|
242
|
+
if (updatedState !== currentState) this._stateSignal.set(updatedState);
|
|
243
|
+
return updatedState;
|
|
244
|
+
} finally {
|
|
245
|
+
this.logger.debug(`[Store] Notify action ${nextAction.type} as processed`);
|
|
246
|
+
this._processedAction.set(nextAction);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
runEffect(effect, updatedState, nextAction) {
|
|
250
|
+
try {
|
|
251
|
+
effect.processAction(this, updatedState, nextAction);
|
|
252
|
+
} catch (error) {
|
|
253
|
+
this.logger.error(`Unable to process action ${nextAction.type} by effect ${effect}`, {
|
|
254
|
+
error,
|
|
255
|
+
action: nextAction,
|
|
256
|
+
processor: effect
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
initializeProcessors(processors, processorMap, getProcessedActions) {
|
|
261
|
+
processors.forEach((processor) => {
|
|
262
|
+
this.logger.information("[Store] ------> initialization of ", processor);
|
|
263
|
+
getProcessedActions(processor).forEach((actionType) => {
|
|
264
|
+
const storedProcessors = processorMap.get(actionType) ?? [];
|
|
265
|
+
if (storedProcessors.length === 0) processorMap.set(actionType, storedProcessors);
|
|
266
|
+
storedProcessors.push(processor);
|
|
267
|
+
});
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
addTaskForNextAction() {
|
|
271
|
+
if (this.useMacroTasks) setTimeout(() => this.processNextAction());
|
|
272
|
+
else Promise.resolve().then(() => this.processNextAction());
|
|
273
|
+
}
|
|
274
|
+
static ɵfac = i0.ɵɵngDeclareFactory({
|
|
275
|
+
minVersion: "12.0.0",
|
|
276
|
+
version: "22.2.0",
|
|
277
|
+
ngImport: i0,
|
|
278
|
+
type: Store,
|
|
279
|
+
deps: [],
|
|
280
|
+
target: i0.ɵɵFactoryTarget.Injectable
|
|
281
|
+
});
|
|
282
|
+
static ɵprov = i0.ɵɵngDeclareInjectable({
|
|
283
|
+
minVersion: "12.0.0",
|
|
284
|
+
version: "22.2.0",
|
|
285
|
+
ngImport: i0,
|
|
286
|
+
type: Store,
|
|
287
|
+
providedIn: "root"
|
|
288
|
+
});
|
|
289
|
+
};
|
|
290
|
+
i0.ɵɵngDeclareClassMetadata({
|
|
291
|
+
minVersion: "12.0.0",
|
|
292
|
+
version: "22.2.0",
|
|
293
|
+
ngImport: i0,
|
|
294
|
+
type: Store,
|
|
295
|
+
decorators: [{
|
|
296
|
+
type: Injectable,
|
|
297
|
+
args: [{ providedIn: "root" }]
|
|
298
|
+
}],
|
|
299
|
+
ctorParameters: () => []
|
|
300
|
+
});
|
|
301
|
+
const NGSSM_COMPONENT_WITH_FEATURE_STATE = new InjectionToken("NGSSM_COMPONENT_WITH_FEATURE_STATE");
|
|
388
302
|
var NgssmStoreActionType;
|
|
389
|
-
(function
|
|
390
|
-
|
|
391
|
-
|
|
303
|
+
(function(NgssmStoreActionType) {
|
|
304
|
+
NgssmStoreActionType["registerFeatureState"] = "[NgssmStoreActionType] registerFeatureState";
|
|
305
|
+
NgssmStoreActionType["unregisterFeatureState"] = "[NgssmStoreActionType] unregisterFeatureState";
|
|
392
306
|
})(NgssmStoreActionType || (NgssmStoreActionType = {}));
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
|
|
307
|
+
var NgssmRegisterFeatureStateAction = class {
|
|
308
|
+
featureStateKey;
|
|
309
|
+
initialValue;
|
|
310
|
+
type = NgssmStoreActionType.registerFeatureState;
|
|
311
|
+
constructor(featureStateKey, initialValue) {
|
|
312
|
+
this.featureStateKey = featureStateKey;
|
|
313
|
+
this.initialValue = initialValue;
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
var NgssmUnregisterFeatureStateAction = class {
|
|
317
|
+
featureStateKey;
|
|
318
|
+
type = NgssmStoreActionType.unregisterFeatureState;
|
|
319
|
+
constructor(featureStateKey) {
|
|
320
|
+
this.featureStateKey = featureStateKey;
|
|
321
|
+
}
|
|
322
|
+
};
|
|
412
323
|
const registerFeatureState = (featureStateKey, initialValue) => {
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
324
|
+
return () => {
|
|
325
|
+
inject(Store).dispatchAction(new NgssmRegisterFeatureStateAction(featureStateKey, initialValue));
|
|
326
|
+
};
|
|
416
327
|
};
|
|
417
328
|
const provideNgssmFeatureState = (featureStateKey, initialValue) => {
|
|
418
|
-
|
|
329
|
+
return makeEnvironmentProviders([provideAppInitializer(registerFeatureState(featureStateKey, initialValue))]);
|
|
419
330
|
};
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "22.0.5", type: NgSsmComponent, isStandalone: true, ngImport: i0 });
|
|
493
|
-
}
|
|
494
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "22.0.5", ngImport: i0, type: NgSsmComponent, decorators: [{
|
|
495
|
-
type: Directive,
|
|
496
|
-
args: [{}]
|
|
497
|
-
}] });
|
|
498
|
-
|
|
499
|
-
const ACTION_DISPATCHER = new InjectionToken('ACTION_DISPATCHER');
|
|
331
|
+
var ProvideNgssmFeatureStateDirective = class ProvideNgssmFeatureStateDirective {
|
|
332
|
+
component = inject(NGSSM_COMPONENT_WITH_FEATURE_STATE);
|
|
333
|
+
store = inject(Store);
|
|
334
|
+
constructor() {
|
|
335
|
+
this.store.dispatchAction(new NgssmRegisterFeatureStateAction(this.component.featureStateKey, this.component.initialState));
|
|
336
|
+
}
|
|
337
|
+
ngOnDestroy() {
|
|
338
|
+
this.store.dispatchAction(new NgssmUnregisterFeatureStateAction(this.component.featureStateKey));
|
|
339
|
+
}
|
|
340
|
+
static ɵfac = i0.ɵɵngDeclareFactory({
|
|
341
|
+
minVersion: "12.0.0",
|
|
342
|
+
version: "22.2.0",
|
|
343
|
+
ngImport: i0,
|
|
344
|
+
type: ProvideNgssmFeatureStateDirective,
|
|
345
|
+
deps: [],
|
|
346
|
+
target: i0.ɵɵFactoryTarget.Directive
|
|
347
|
+
});
|
|
348
|
+
static ɵdir = i0.ɵɵngDeclareDirective({
|
|
349
|
+
minVersion: "14.0.0",
|
|
350
|
+
version: "22.2.0",
|
|
351
|
+
type: ProvideNgssmFeatureStateDirective,
|
|
352
|
+
isStandalone: true,
|
|
353
|
+
selector: "[provideNgssmFeatureState]",
|
|
354
|
+
ngImport: i0
|
|
355
|
+
});
|
|
356
|
+
};
|
|
357
|
+
i0.ɵɵngDeclareClassMetadata({
|
|
358
|
+
minVersion: "12.0.0",
|
|
359
|
+
version: "22.2.0",
|
|
360
|
+
ngImport: i0,
|
|
361
|
+
type: ProvideNgssmFeatureStateDirective,
|
|
362
|
+
decorators: [{
|
|
363
|
+
type: Directive,
|
|
364
|
+
args: [{ selector: "[provideNgssmFeatureState]" }]
|
|
365
|
+
}],
|
|
366
|
+
ctorParameters: () => []
|
|
367
|
+
});
|
|
368
|
+
var FeatureStateReducer = class FeatureStateReducer {
|
|
369
|
+
processedActions = [NgssmStoreActionType.registerFeatureState, NgssmStoreActionType.unregisterFeatureState];
|
|
370
|
+
updateState(state, action) {
|
|
371
|
+
switch (action.type) {
|
|
372
|
+
case NgssmStoreActionType.registerFeatureState: {
|
|
373
|
+
const registerFeatureStateAction = action;
|
|
374
|
+
return update(state, { [registerFeatureStateAction.featureStateKey]: { $set: registerFeatureStateAction.initialValue } });
|
|
375
|
+
}
|
|
376
|
+
case NgssmStoreActionType.unregisterFeatureState: return update(state, { $unset: [action.featureStateKey] });
|
|
377
|
+
}
|
|
378
|
+
return state;
|
|
379
|
+
}
|
|
380
|
+
static ɵfac = i0.ɵɵngDeclareFactory({
|
|
381
|
+
minVersion: "12.0.0",
|
|
382
|
+
version: "22.2.0",
|
|
383
|
+
ngImport: i0,
|
|
384
|
+
type: FeatureStateReducer,
|
|
385
|
+
deps: [],
|
|
386
|
+
target: i0.ɵɵFactoryTarget.Injectable
|
|
387
|
+
});
|
|
388
|
+
static ɵprov = i0.ɵɵngDeclareInjectable({
|
|
389
|
+
minVersion: "12.0.0",
|
|
390
|
+
version: "22.2.0",
|
|
391
|
+
ngImport: i0,
|
|
392
|
+
type: FeatureStateReducer
|
|
393
|
+
});
|
|
394
|
+
};
|
|
395
|
+
i0.ɵɵngDeclareClassMetadata({
|
|
396
|
+
minVersion: "12.0.0",
|
|
397
|
+
version: "22.2.0",
|
|
398
|
+
ngImport: i0,
|
|
399
|
+
type: FeatureStateReducer,
|
|
400
|
+
decorators: [{ type: Injectable }]
|
|
401
|
+
});
|
|
402
|
+
const ACTION_DISPATCHER = new InjectionToken("ACTION_DISPATCHER");
|
|
500
403
|
const provideNgssmStore = () => {
|
|
501
|
-
|
|
404
|
+
return makeEnvironmentProviders([provideReducers(FeatureStateReducer), {
|
|
405
|
+
provide: ACTION_DISPATCHER,
|
|
406
|
+
useExisting: Store
|
|
407
|
+
}]);
|
|
502
408
|
};
|
|
503
|
-
|
|
504
409
|
const createSignal = (selector) => {
|
|
505
|
-
|
|
506
|
-
|
|
410
|
+
const store = inject(Store);
|
|
411
|
+
return computed(() => selector(store.state()));
|
|
507
412
|
};
|
|
413
|
+
export { ACTION_DISPATCHER, ConsoleAppender, FeatureStateReducer, LogLevel, Logger, NGSSM_COMPONENT_WITH_FEATURE_STATE, NGSSM_CONSOLE_APPENDER_NAME, NGSSM_EFFECT, NGSSM_REDUCER, NGSSM_STATE_INITIALIZER, NgSsmFeatureState, NgssmRegisterFeatureStateAction, NgssmStoreActionType, NgssmUnregisterFeatureStateAction, ProvideNgssmFeatureStateDirective, Store, createSignal, provideConsoleAppender, provideEffect, provideEffectFunc, provideEffects, provideNgssmFeatureState, provideNgssmStore, provideReducer, provideReducers, startConsoleAppenderFactory };
|
|
508
414
|
|
|
509
|
-
|
|
510
|
-
* Public API Surface of ngssm-store
|
|
511
|
-
*/
|
|
512
|
-
|
|
513
|
-
/**
|
|
514
|
-
* Generated bundle index. Do not edit.
|
|
515
|
-
*/
|
|
516
|
-
|
|
517
|
-
export { ACTION_DISPATCHER, ConsoleAppender, FeatureStateReducer, LogLevel, Logger, NGSSM_COMPONENT_WITH_FEATURE_STATE, NGSSM_CONSOLE_APPENDER_NAME, NGSSM_EFFECT, NGSSM_REDUCER, NGSSM_STATE_INITIALIZER, NgSsmComponent, NgSsmFeatureState, NgssmRegisterFeatureStateAction, NgssmStoreActionType, NgssmUnregisterFeatureStateAction, ProvideNgssmFeatureStateDirective, Store, createSignal, provideConsoleAppender, provideEffect, provideEffectFunc, provideEffects, provideNgssmFeatureState, provideNgssmStore, provideReducer, provideReducers, startConsoleAppenderFactory };
|
|
518
|
-
//# sourceMappingURL=ngssm-store.mjs.map
|
|
415
|
+
//# sourceMappingURL=ngssm-store.mjs.map
|