@hkdigital/lib-core 0.5.49 → 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.
@@ -1,6 +1,3 @@
1
- import { INFO } from '../../../logging/common.js';
2
- import { createClientLogger } from '../../../logging/client.js';
3
-
4
1
  /**
5
2
  * Base class for page state machines with URL route mapping
6
3
  *
@@ -220,17 +217,18 @@ export default class PageMachine {
220
217
  * Normalize onEnterHooks to ensure consistent format
221
218
  * Converts function to {onEnter: function} object
222
219
  *
223
- * @param {Record<string, Function|Object>} hooks - Raw hooks configuration
220
+ * @param {Record<string, Function|{onEnter: Function}>} hooks - Raw hooks configuration
224
221
  * @returns {Record<string, {onEnter: Function}>} Normalized hooks
225
222
  */
226
223
  #normalizeOnEnterHooks(hooks) {
224
+ /** @type {Record<string, {onEnter: Function} */
227
225
  const normalized = {};
228
226
 
229
227
  for (const [state, hook] of Object.entries(hooks)) {
230
228
  if (typeof hook === 'function') {
231
229
  // Simple function -> wrap in object
232
230
  normalized[state] = { onEnter: hook };
233
- } else if (hook && typeof hook === 'object' && hook.onEnter) {
231
+ } else if (hook?.onEnter) {
234
232
  // Already an object with onEnter
235
233
  normalized[state] = hook;
236
234
  }
@@ -253,9 +251,14 @@ export default class PageMachine {
253
251
  const targetState = this.#getStateFromPath(currentPath);
254
252
 
255
253
  if (targetState && targetState !== this.#current) {
254
+ const oldState = this.#current;
256
255
  this.#current = targetState;
257
256
  this.#visitedStates.add(targetState);
258
257
  this.#revision++;
258
+
259
+ // Log state transition from URL sync
260
+ this.logger?.debug(`syncFromPath (url): ${oldState} → ${targetState}`);
261
+
259
262
  return true;
260
263
  }
261
264
 
@@ -286,14 +289,15 @@ export default class PageMachine {
286
289
  this.#visitedStates.add(newState);
287
290
 
288
291
  // Log state transition
289
- this.logger?.debug(`${oldState} → ${newState}`);
292
+ this.logger?.debug(`setState: ${oldState} → ${newState}`);
290
293
 
291
294
  // Check if this state has an onEnter hook
292
295
  const hookConfig = this.#onEnterHooks[newState];
293
296
  if (hookConfig?.onEnter) {
294
297
  // Create done callback for auto-transition
295
298
  let doneCalled = false;
296
- const done = (nextState) => {
299
+
300
+ const done = ( /** @type {string} */ nextState) => {
297
301
  if (!doneCalled && nextState && nextState !== newState) {
298
302
  doneCalled = true;
299
303
  this.#isTransitioning = false;
@@ -322,7 +326,8 @@ export default class PageMachine {
322
326
  await handler;
323
327
  }
324
328
  } catch (error) {
325
- console.error(`Error in onEnter hook for state ${newState}:`, error);
329
+ const logger = this.logger ?? console;
330
+ logger.error(`Error in onEnter hook for state ${newState}:`, error);
326
331
  }
327
332
  }
328
333
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hkdigital/lib-core",
3
- "version": "0.5.49",
3
+ "version": "0.5.50",
4
4
  "author": {
5
5
  "name": "HKdigital",
6
6
  "url": "https://hkdigital.nl"