@hkdigital/lib-core 0.5.48 → 0.5.50
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.
|
@@ -69,10 +69,8 @@ export default class PageMachine {
|
|
|
69
69
|
* Initial data properties (from server)
|
|
70
70
|
* @param {Record<string, Function>} [config.onEnterHooks={}]
|
|
71
71
|
* Map of states to onEnter hook functions
|
|
72
|
-
* @param {
|
|
73
|
-
*
|
|
74
|
-
* @param {import('../../../logging/typedef.js').LogLevel} [config.logLevel]
|
|
75
|
-
* Log level (defaults to INFO for state transitions)
|
|
72
|
+
* @param {import('../../../logging/client.js').Logger} [config.logger]
|
|
73
|
+
* Logger instance (optional, if not provided no logging occurs)
|
|
76
74
|
*
|
|
77
75
|
* @example
|
|
78
76
|
* ```javascript
|
|
@@ -97,19 +95,18 @@ export default class PageMachine {
|
|
|
97
95
|
* });
|
|
98
96
|
* ```
|
|
99
97
|
*/
|
|
100
|
-
constructor({ startPath, routeMap, initialData, onEnterHooks,
|
|
98
|
+
constructor({ startPath, routeMap, initialData, onEnterHooks, logger }: {
|
|
101
99
|
startPath: string;
|
|
102
100
|
routeMap?: Record<string, string> | undefined;
|
|
103
101
|
initialData?: Record<string, any> | undefined;
|
|
104
102
|
onEnterHooks?: Record<string, Function> | undefined;
|
|
105
|
-
|
|
106
|
-
logLevel?: import("../../../logging/typedef.js").LogLevel | undefined;
|
|
103
|
+
logger?: import("../../../logging/client.js").Logger | undefined;
|
|
107
104
|
});
|
|
108
105
|
/**
|
|
109
106
|
* Logger instance for state machine
|
|
110
|
-
* @type {Logger}
|
|
107
|
+
* @type {import('../../../logging/client.js').Logger}
|
|
111
108
|
*/
|
|
112
|
-
logger: Logger;
|
|
109
|
+
logger: import("../../../logging/client.js").Logger;
|
|
113
110
|
/**
|
|
114
111
|
* Synchronize machine state with URL path
|
|
115
112
|
*
|
|
@@ -334,4 +331,3 @@ export default class PageMachine {
|
|
|
334
331
|
get canAbortTransitions(): boolean;
|
|
335
332
|
#private;
|
|
336
333
|
}
|
|
337
|
-
import { Logger } from '../../../logging/common.js';
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { Logger, INFO } from '../../../logging/common.js';
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
* Base class for page state machines with URL route mapping
|
|
5
3
|
*
|
|
@@ -61,7 +59,7 @@ import { Logger, INFO } from '../../../logging/common.js';
|
|
|
61
59
|
export default class PageMachine {
|
|
62
60
|
/**
|
|
63
61
|
* Logger instance for state machine
|
|
64
|
-
* @type {Logger}
|
|
62
|
+
* @type {import('../../../logging/client.js').Logger}
|
|
65
63
|
*/
|
|
66
64
|
logger;
|
|
67
65
|
/**
|
|
@@ -152,10 +150,8 @@ export default class PageMachine {
|
|
|
152
150
|
* Initial data properties (from server)
|
|
153
151
|
* @param {Record<string, Function>} [config.onEnterHooks={}]
|
|
154
152
|
* Map of states to onEnter hook functions
|
|
155
|
-
* @param {
|
|
156
|
-
*
|
|
157
|
-
* @param {import('../../../logging/typedef.js').LogLevel} [config.logLevel]
|
|
158
|
-
* Log level (defaults to INFO for state transitions)
|
|
153
|
+
* @param {import('../../../logging/client.js').Logger} [config.logger]
|
|
154
|
+
* Logger instance (optional, if not provided no logging occurs)
|
|
159
155
|
*
|
|
160
156
|
* @example
|
|
161
157
|
* ```javascript
|
|
@@ -185,14 +181,13 @@ export default class PageMachine {
|
|
|
185
181
|
routeMap = {},
|
|
186
182
|
initialData = {},
|
|
187
183
|
onEnterHooks = {},
|
|
188
|
-
|
|
189
|
-
logLevel = INFO
|
|
184
|
+
logger = null
|
|
190
185
|
}) {
|
|
191
186
|
if (!startPath) {
|
|
192
187
|
throw new Error('PageMachine requires startPath parameter');
|
|
193
188
|
}
|
|
194
189
|
|
|
195
|
-
this.logger =
|
|
190
|
+
this.logger = logger;
|
|
196
191
|
this.#startPath = startPath;
|
|
197
192
|
this.#routeMap = routeMap;
|
|
198
193
|
this.#data = initialData;
|
|
@@ -222,17 +217,18 @@ export default class PageMachine {
|
|
|
222
217
|
* Normalize onEnterHooks to ensure consistent format
|
|
223
218
|
* Converts function to {onEnter: function} object
|
|
224
219
|
*
|
|
225
|
-
* @param {Record<string, Function|
|
|
220
|
+
* @param {Record<string, Function|{onEnter: Function}>} hooks - Raw hooks configuration
|
|
226
221
|
* @returns {Record<string, {onEnter: Function}>} Normalized hooks
|
|
227
222
|
*/
|
|
228
223
|
#normalizeOnEnterHooks(hooks) {
|
|
224
|
+
/** @type {Record<string, {onEnter: Function} */
|
|
229
225
|
const normalized = {};
|
|
230
226
|
|
|
231
227
|
for (const [state, hook] of Object.entries(hooks)) {
|
|
232
228
|
if (typeof hook === 'function') {
|
|
233
229
|
// Simple function -> wrap in object
|
|
234
230
|
normalized[state] = { onEnter: hook };
|
|
235
|
-
} else if (hook
|
|
231
|
+
} else if (hook?.onEnter) {
|
|
236
232
|
// Already an object with onEnter
|
|
237
233
|
normalized[state] = hook;
|
|
238
234
|
}
|
|
@@ -255,9 +251,14 @@ export default class PageMachine {
|
|
|
255
251
|
const targetState = this.#getStateFromPath(currentPath);
|
|
256
252
|
|
|
257
253
|
if (targetState && targetState !== this.#current) {
|
|
254
|
+
const oldState = this.#current;
|
|
258
255
|
this.#current = targetState;
|
|
259
256
|
this.#visitedStates.add(targetState);
|
|
260
257
|
this.#revision++;
|
|
258
|
+
|
|
259
|
+
// Log state transition from URL sync
|
|
260
|
+
this.logger?.debug(`syncFromPath (url): ${oldState} → ${targetState}`);
|
|
261
|
+
|
|
261
262
|
return true;
|
|
262
263
|
}
|
|
263
264
|
|
|
@@ -288,14 +289,15 @@ export default class PageMachine {
|
|
|
288
289
|
this.#visitedStates.add(newState);
|
|
289
290
|
|
|
290
291
|
// Log state transition
|
|
291
|
-
this.logger
|
|
292
|
+
this.logger?.debug(`setState: ${oldState} → ${newState}`);
|
|
292
293
|
|
|
293
294
|
// Check if this state has an onEnter hook
|
|
294
295
|
const hookConfig = this.#onEnterHooks[newState];
|
|
295
296
|
if (hookConfig?.onEnter) {
|
|
296
297
|
// Create done callback for auto-transition
|
|
297
298
|
let doneCalled = false;
|
|
298
|
-
|
|
299
|
+
|
|
300
|
+
const done = ( /** @type {string} */ nextState) => {
|
|
299
301
|
if (!doneCalled && nextState && nextState !== newState) {
|
|
300
302
|
doneCalled = true;
|
|
301
303
|
this.#isTransitioning = false;
|
|
@@ -324,7 +326,8 @@ export default class PageMachine {
|
|
|
324
326
|
await handler;
|
|
325
327
|
}
|
|
326
328
|
} catch (error) {
|
|
327
|
-
|
|
329
|
+
const logger = this.logger ?? console;
|
|
330
|
+
logger.error(`Error in onEnter hook for state ${newState}:`, error);
|
|
328
331
|
}
|
|
329
332
|
}
|
|
330
333
|
|