@hkdigital/lib-core 0.5.56 → 0.5.58

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.
@@ -264,7 +264,7 @@ export default class Logger extends EventEmitter {
264
264
  ...this.#defaultContext
265
265
  };
266
266
 
267
- const childName = `${this.name}>${name}`;
267
+ const childName = `${this.name}:${name}`;
268
268
 
269
269
  const child = new Logger(childName, level ?? this.level, context);
270
270
 
@@ -1,3 +1,45 @@
1
+ /**
2
+ * Route-aware data manager for page groups
3
+ *
4
+ * Tracks navigation within a route group and manages business/domain data.
5
+ * Does NOT enforce transitions - allows free navigation via browser.
6
+ *
7
+ * Features:
8
+ * - Current route tracking and synchronization
9
+ * - Start path management
10
+ * - Data properties for business/domain state
11
+ * - Visited routes tracking
12
+ *
13
+ * Basic usage:
14
+ * ```javascript
15
+ * const machine = new PageMachine({
16
+ * startPath: '/intro/start',
17
+ * routes: ['/intro/start', '/intro/profile', '/intro/complete']
18
+ * });
19
+ *
20
+ * // Sync machine with URL changes
21
+ * $effect(() => {
22
+ * machine.syncFromPath($page.url.pathname);
23
+ * });
24
+ *
25
+ * // Check current route
26
+ * if (machine.current === '/intro/profile') {
27
+ * // Do something
28
+ * }
29
+ * ```
30
+ *
31
+ * Animations and page-specific logic should use $effect in pages:
32
+ * ```javascript
33
+ * // In +page.svelte
34
+ * const animations = new PageAnimations();
35
+ *
36
+ * $effect(() => {
37
+ * if (someCondition) {
38
+ * animations.start();
39
+ * }
40
+ * });
41
+ * ```
42
+ */
1
43
  export default class PageMachine {
2
44
  /**
3
45
  * Constructor
@@ -5,93 +47,58 @@ export default class PageMachine {
5
47
  * @param {Object} config - Configuration object
6
48
  * @param {string} config.startPath
7
49
  * Start path for this route group (e.g., '/game/play')
8
- * @param {Record<string, string>} [config.routeMap={}]
9
- * Map of states to route paths
50
+ * @param {string[]} [config.routes=[]]
51
+ * Optional list of valid routes (for validation/dev tools)
10
52
  * @param {Record<string, any>} [config.initialData={}]
11
53
  * Initial data properties (from server)
12
- * @param {Record<string, Function>} [config.onEnterHooks={}]
13
- * Map of states to onEnter hook functions
14
54
  * @param {import('../../../logging/client.js').Logger} [config.logger]
15
- * Logger instance (optional, if not provided no logging occurs)
55
+ * Logger instance (optional)
16
56
  *
17
57
  * @example
18
58
  * ```javascript
19
59
  * const machine = new PageMachine({
20
60
  * startPath: '/intro/start',
21
- * routeMap: {
22
- * [STATE_START]: '/intro/start',
23
- * [STATE_ANIMATE]: '/intro/animate'
24
- * },
61
+ * routes: ['/intro/start', '/intro/profile'],
25
62
  * initialData: {
26
63
  * INTRO_COMPLETED: false
27
- * },
28
- * onEnterHooks: {
29
- * [STATE_ANIMATE]: (done) => {
30
- * setTimeout(() => done(STATE_START), 1000);
31
- * return {
32
- * abort: () => clearTimeout(...),
33
- * complete: () => done(STATE_START)
34
- * };
35
- * }
36
64
  * }
37
65
  * });
38
66
  * ```
39
67
  */
40
- constructor({ startPath, routeMap, initialData, onEnterHooks, logger }: {
68
+ constructor({ startPath, routes, initialData, logger }: {
41
69
  startPath: string;
42
- routeMap?: Record<string, string> | undefined;
70
+ routes?: string[] | undefined;
43
71
  initialData?: Record<string, any> | undefined;
44
- onEnterHooks?: Record<string, Function> | undefined;
45
72
  logger?: import("../../../logging/client.js").Logger | undefined;
46
73
  });
47
74
  /**
48
- * Logger instance for state machine
75
+ * Logger instance
49
76
  * @type {import('../../../logging/client.js').Logger}
50
77
  */
51
78
  logger: import("../../../logging/client.js").Logger;
52
79
  /**
53
- * Synchronize machine state with URL path
80
+ * Synchronize machine with URL path
54
81
  *
55
- * Call this in a $effect that watches $page.url.pathname
56
- * Automatically tracks visited states and triggers onEnter hooks
82
+ * Call this in a $effect that watches $page.url.pathname.
83
+ * Automatically tracks visited routes.
57
84
  *
58
85
  * @param {string} currentPath - Current URL pathname
59
86
  *
60
- * @returns {boolean} True if state was changed
87
+ * @returns {boolean} True if route was changed
61
88
  */
62
89
  syncFromPath(currentPath: string): boolean;
63
90
  /**
64
- * Get route path for a given state
91
+ * Get current route
65
92
  *
66
- * @param {string} state - State name
67
- *
68
- * @returns {string} Route path or null if no mapping
69
- */
70
- getPathForState(state: string): string;
71
- /**
72
- * Navigate to the route path for a given state
73
- *
74
- * @param {string} state - State name
75
- */
76
- navigateToState(state: string): void;
77
- /**
78
- * Get route path for current state
79
- *
80
- * @returns {string|null} Route path or null if no mapping
81
- */
82
- getCurrentPath(): string | null;
83
- /**
84
- * Get current state
85
- *
86
- * @returns {string} Current state name
93
+ * @returns {string} Current route path
87
94
  */
88
95
  get current(): string;
89
96
  /**
90
- * Get the route map
97
+ * Get the routes list
91
98
  *
92
- * @returns {Record<string, string>} Copy of route map
99
+ * @returns {string[]} Copy of routes list
93
100
  */
94
- get routeMap(): Record<string, string>;
101
+ get routes(): string[];
95
102
  /**
96
103
  * Set a data property value
97
104
  *
@@ -134,7 +141,8 @@ export default class PageMachine {
134
141
  /**
135
142
  * Update multiple data properties at once
136
143
  *
137
- * @param {Record<string, any>} dataUpdates - Object with key-value pairs
144
+ * @param {Record<string, any>} dataUpdates
145
+ * Object with key-value pairs
138
146
  *
139
147
  * @example
140
148
  * ```javascript
@@ -147,24 +155,24 @@ export default class PageMachine {
147
155
  */
148
156
  updateData(dataUpdates: Record<string, any>): void;
149
157
  /**
150
- * Check if a state has been visited
158
+ * Check if a route has been visited
151
159
  *
152
- * @param {string} state - State name to check
160
+ * @param {string} route - Route path to check
153
161
  *
154
- * @returns {boolean} True if the state has been visited
162
+ * @returns {boolean} True if the route has been visited
155
163
  *
156
164
  * @example
157
165
  * ```javascript
158
- * if (machine.hasVisited(STATE_PROFILE)) {
166
+ * if (machine.hasVisited('/intro/profile')) {
159
167
  * // User has seen profile page, skip intro
160
168
  * }
161
169
  * ```
162
170
  */
163
- hasVisited(state: string): boolean;
171
+ hasVisited(route: string): boolean;
164
172
  /**
165
- * Check if the start state has been visited
173
+ * Check if the start route has been visited
166
174
  *
167
- * @returns {boolean} True if the start state has been visited
175
+ * @returns {boolean} True if the start route has been visited
168
176
  *
169
177
  * @example
170
178
  * ```javascript
@@ -175,28 +183,22 @@ export default class PageMachine {
175
183
  */
176
184
  get hasVisitedStart(): boolean;
177
185
  /**
178
- * Get all visited states
186
+ * Get all visited routes
179
187
  *
180
- * @returns {string[]} Array of visited state names
188
+ * @returns {string[]} Array of visited route paths
181
189
  */
182
- getVisitedStates(): string[];
190
+ getVisitedRoutes(): string[];
183
191
  /**
184
- * Reset visited states tracking
192
+ * Reset visited routes tracking
185
193
  * Useful for testing or resetting experience
186
194
  */
187
- resetVisitedStates(): void;
195
+ resetVisitedRoutes(): void;
188
196
  /**
189
197
  * Get the start path
190
198
  *
191
199
  * @returns {string} Start path
192
200
  */
193
201
  get startPath(): string;
194
- /**
195
- * Get the start state
196
- *
197
- * @returns {string} Start state name
198
- */
199
- get startState(): string;
200
202
  /**
201
203
  * Check if the supplied path matches the start path
202
204
  *
@@ -213,18 +215,18 @@ export default class PageMachine {
213
215
  */
214
216
  isStartPath(path: string): boolean;
215
217
  /**
216
- * Check if currently on the start state
218
+ * Check if currently on the start path
217
219
  *
218
- * @returns {boolean} True if current state is the start state
220
+ * @returns {boolean} True if current route is the start path
219
221
  *
220
222
  * @example
221
223
  * ```javascript
222
- * if (machine.isOnStartState) {
224
+ * if (machine.isOnStartPath) {
223
225
  * // Show onboarding
224
226
  * }
225
227
  * ```
226
228
  */
227
- get isOnStartState(): boolean;
229
+ get isOnStartPath(): boolean;
228
230
  /**
229
231
  * Navigate to the start path
230
232
  *
@@ -235,53 +237,5 @@ export default class PageMachine {
235
237
  * ```
236
238
  */
237
239
  redirectToStartPath(): void;
238
- /**
239
- * Abort current state's transitions
240
- * Cancels animations/operations immediately (incomplete state)
241
- *
242
- * @example
243
- * ```javascript
244
- * // User clicks "Cancel" button
245
- * machine.abortTransitions();
246
- * ```
247
- */
248
- abortTransitions(): void;
249
- /**
250
- * Complete current state's transitions immediately
251
- * Fast-forwards animations/operations to completion (complete state)
252
- *
253
- * @example
254
- * ```javascript
255
- * // User clicks "Skip" or "Next" button
256
- * machine.completeTransitions();
257
- * ```
258
- */
259
- completeTransitions(): void;
260
- /**
261
- * Check if current state has transitions that can be completed
262
- *
263
- * @returns {boolean} True if completeTransitions() can be called
264
- *
265
- * @example
266
- * ```svelte
267
- * {#if machine.canCompleteTransitions}
268
- * <button onclick={() => machine.completeTransitions()}>Skip</button>
269
- * {/if}
270
- * ```
271
- */
272
- get canCompleteTransitions(): boolean;
273
- /**
274
- * Check if current state has transitions that can be aborted
275
- *
276
- * @returns {boolean} True if abortTransitions() can be called
277
- *
278
- * @example
279
- * ```svelte
280
- * {#if machine.canAbortTransitions}
281
- * <button onclick={() => machine.abortTransitions()}>Cancel</button>
282
- * {/if}
283
- * ```
284
- */
285
- get canAbortTransitions(): boolean;
286
240
  #private;
287
241
  }