@hkdigital/lib-core 0.4.25 → 0.4.27

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.
@@ -8,7 +8,7 @@ import { isTestEnv } from '../../../util/env.js';
8
8
  import EventEmitter from '../../../generic/events/classes/EventEmitter.js';
9
9
  import { ENTER, EXIT } from './constants.js';
10
10
 
11
- /** @typedef {import('./typedef.js').StateTransitionMetadata} StateTransitionMetadata */
11
+ /** @typedef {import('./typedef.js').TransitionData} TransitionData */
12
12
  /** @typedef {import('./typedef.js').OnEnterCallback} OnEnterCallback */
13
13
  /** @typedef {import('./typedef.js').OnExitCallback} OnExitCallback */
14
14
 
@@ -57,17 +57,17 @@ export default class FiniteStateMachine extends EventEmitter {
57
57
  this.states = states;
58
58
 
59
59
  // synthetically trigger _enter for the initial state.
60
- const initialMetadata = {
60
+ const initialTransitionData = {
61
61
  from: null,
62
62
  to: initial,
63
63
  event: null,
64
64
  args: []
65
65
  };
66
66
 
67
- this.#executeAction('_enter', initialMetadata);
67
+ this.#executeAction('_enter', initialTransitionData);
68
68
 
69
69
  // Emit ENTER event for external listeners for initial state
70
- this.emit(ENTER, { state: initial, metadata: initialMetadata });
70
+ this.emit(ENTER, { state: initial, transition: initialTransitionData });
71
71
  }
72
72
 
73
73
  /**
@@ -78,24 +78,24 @@ export default class FiniteStateMachine extends EventEmitter {
78
78
  * @param {any[]} [args]
79
79
  */
80
80
  #transition(newState, event, args) {
81
- /** @type {StateTransitionMetadata} */
82
- const metadata = { from: this.#current, to: newState, event, args };
81
+ /** @type {TransitionData} */
82
+ const transition = { from: this.#current, to: newState, event, args };
83
83
 
84
84
  // Call onexit callback before leaving current state
85
- this.onexit?.(this.#current, metadata);
85
+ this.onexit?.(this.#current, transition);
86
86
 
87
87
  // Emit EXIT event for external listeners
88
- this.emit(EXIT, { state: this.#current, metadata });
88
+ this.emit(EXIT, { state: this.#current, transition });
89
89
 
90
- this.#executeAction('_exit', metadata);
90
+ this.#executeAction('_exit', transition);
91
91
  this.#current = newState;
92
- this.#executeAction('_enter', metadata);
92
+ this.#executeAction('_enter', transition);
93
93
 
94
94
  // Emit ENTER event for external listeners
95
- this.emit(ENTER, { state: newState, metadata });
95
+ this.emit(ENTER, { state: newState, transition });
96
96
 
97
97
  // Call onenter callback after state change
98
- this.onenter?.(newState, metadata);
98
+ this.onenter?.(newState, transition);
99
99
  }
100
100
 
101
101
  /**
@@ -116,7 +116,7 @@ export default class FiniteStateMachine extends EventEmitter {
116
116
  if (isLifecycleFnMeta(args[0])) {
117
117
  return action(args[0]);
118
118
  } else {
119
- throw new Error(`Invalid metadata passed to lifecycle function`);
119
+ throw new Error(`Invalid transition data passed to lifecycle function`);
120
120
  }
121
121
 
122
122
  // Normal state events
@@ -56,16 +56,16 @@ Each state can define:
56
56
  ```javascript
57
57
  const machine = new FiniteStateMachine('idle', {
58
58
  idle: {
59
- _enter: (metadata) => {
59
+ _enter: (transition) => {
60
60
  console.log('Entered idle state');
61
61
  },
62
- _exit: (metadata) => {
62
+ _exit: (transition) => {
63
63
  console.log('Leaving idle state');
64
64
  },
65
65
  start: 'running'
66
66
  },
67
67
  running: {
68
- _enter: (metadata) => {
68
+ _enter: (transition) => {
69
69
  console.log('Started running');
70
70
  },
71
71
  stop: 'idle'
@@ -73,14 +73,14 @@ const machine = new FiniteStateMachine('idle', {
73
73
  });
74
74
  ```
75
75
 
76
- ## Callback Metadata
76
+ ## Callback TransitionData
77
77
 
78
- Enter and exit callbacks receive metadata about the transition:
78
+ Enter and exit callbacks receive transition data about the state change:
79
79
 
80
80
  ```javascript
81
- /** @typedef {import('./typedef.js').StateTransitionMetadata} StateTransitionMetadata */
81
+ /** @typedef {import('./typedef.js').TransitionData} TransitionData */
82
82
 
83
- // Metadata structure:
83
+ // TransitionData structure:
84
84
  {
85
85
  from: 'previousState', // State being exited
86
86
  to: 'newState', // State being entered
@@ -94,18 +94,18 @@ Enter and exit callbacks receive metadata about the transition:
94
94
  For better type safety, import the type definitions:
95
95
 
96
96
  ```javascript
97
- /** @typedef {import('./typedef.js').StateTransitionMetadata} StateTransitionMetadata */
97
+ /** @typedef {import('./typedef.js').TransitionData} TransitionData */
98
98
  /** @typedef {import('./typedef.js').OnEnterCallback} OnEnterCallback */
99
99
  /** @typedef {import('./typedef.js').OnExitCallback} OnExitCallback */
100
100
 
101
101
  /** @type {OnEnterCallback} */
102
- const handleEnter = (state, metadata) => {
103
- console.log(`Entering ${state} from ${metadata.from}`);
102
+ const handleEnter = (currentState, transition) => {
103
+ console.log(`Entering ${currentState} from ${transition.from}`);
104
104
  };
105
105
 
106
106
  /** @type {OnExitCallback} */
107
- const handleExit = (state, metadata) => {
108
- console.log(`Leaving ${state} to ${metadata.to}`);
107
+ const handleExit = (currentState, transition) => {
108
+ console.log(`Leaving ${currentState} to ${transition.to}`);
109
109
  };
110
110
 
111
111
  machine.onenter = handleEnter;
@@ -209,8 +209,8 @@ const machine = new FiniteStateMachine('idle', {
209
209
  error: { retry: 'loading', reset: 'idle' }
210
210
  });
211
211
 
212
- machine.onexit = (state, metadata) => {
213
- switch (state) {
212
+ machine.onexit = (currentState, transition) => {
213
+ switch (currentState) {
214
214
  case 'loading':
215
215
  console.log('Leaving loading state...');
216
216
  // Cancel ongoing requests
@@ -224,8 +224,8 @@ machine.onexit = (state, metadata) => {
224
224
  }
225
225
  };
226
226
 
227
- machine.onenter = (state, metadata) => {
228
- switch (state) {
227
+ machine.onenter = (currentState, transition) => {
228
+ switch (currentState) {
229
229
  case 'loading':
230
230
  console.log('Started loading...');
231
231
  showSpinner();
@@ -236,7 +236,7 @@ machine.onenter = (state, metadata) => {
236
236
  break;
237
237
  case 'error':
238
238
  console.log('Loading failed');
239
- showError(metadata.args[0]);
239
+ showError(transition.args[0]);
240
240
  break;
241
241
  }
242
242
  };
@@ -264,8 +264,8 @@ const machine = new FiniteStateMachine('idle', {
264
264
  }
265
265
  });
266
266
 
267
- machine.onexit = (state) => console.log(`3. onexit ${state}`);
268
- machine.onenter = (state) => console.log(`6. onenter ${state}`);
267
+ machine.onexit = (currentState) => console.log(`3. onexit ${currentState}`);
268
+ machine.onenter = (currentState) => console.log(`6. onenter ${currentState}`);
269
269
 
270
270
  // Initial state triggers _enter and onenter
271
271
  // Output:
@@ -313,8 +313,8 @@ machine.onexit = (state) => {
313
313
  }
314
314
  };
315
315
 
316
- machine.onenter = (state) => {
317
- switch (state) {
316
+ machine.onenter = (label) => {
317
+ switch (label) {
318
318
  case 'loading':
319
319
  this.#startProcess(); // Start async process immediately
320
320
  break;
@@ -381,8 +381,8 @@ export default class TaskProcessor {
381
381
 
382
382
  constructor() {
383
383
  // onexit: Handle cleanup when leaving states
384
- this.#machine.onexit = (state) => {
385
- switch (state) {
384
+ this.#machine.onexit = (currentState) => {
385
+ switch (currentState) {
386
386
  case 'processing':
387
387
  this.#cancelTasks(); // Cancel ongoing tasks if interrupted
388
388
  break;
@@ -390,8 +390,8 @@ export default class TaskProcessor {
390
390
  };
391
391
 
392
392
  // onenter: Handle immediate state actions
393
- this.#machine.onenter = (state) => {
394
- switch (state) {
393
+ this.#machine.onenter = (currentState) => {
394
+ switch (currentState) {
395
395
  case 'processing':
396
396
  this.#startAllTasks(); // Start processing immediately
397
397
  break;
@@ -399,7 +399,7 @@ export default class TaskProcessor {
399
399
  this.#notifyComplete(); // Cleanup/notify when done
400
400
  break;
401
401
  }
402
- this.state = state;
402
+ this.state = currentState;
403
403
  };
404
404
 
405
405
  // $effect: Monitor reactive completion
@@ -435,8 +435,8 @@ export default class TaskProcessor {
435
435
  });
436
436
 
437
437
  // Handle state-specific actions
438
- machine.onexit = (state) => {
439
- switch (state) {
438
+ machine.onexit = (currentState) => {
439
+ switch (currentState) {
440
440
  case 'loading':
441
441
  // Cancel any ongoing requests
442
442
  cancelRequests();
@@ -444,8 +444,8 @@ export default class TaskProcessor {
444
444
  }
445
445
  };
446
446
 
447
- machine.onenter = (state) => {
448
- switch (state) {
447
+ machine.onenter = (currentState) => {
448
+ switch (currentState) {
449
449
  case 'loading':
450
450
  loadData();
451
451
  break;
@@ -526,7 +526,7 @@ const auth = new FiniteStateMachine('anonymous', {
526
526
  failure: 'anonymous'
527
527
  },
528
528
  authenticated: {
529
- _enter: (meta) => console.log('Welcome!'),
529
+ _enter: (transition) => console.log('Welcome!'),
530
530
  logout: 'anonymous',
531
531
  expire: 'anonymous'
532
532
  }
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Metadata object passed to state transition callbacks
3
3
  */
4
- export type StateTransitionMetadata = {
4
+ export type TransitionData = {
5
5
  /**
6
6
  * - The state being exited
7
7
  */
@@ -22,8 +22,8 @@ export type StateTransitionMetadata = {
22
22
  /**
23
23
  * Callback function called when entering a state
24
24
  */
25
- export type OnEnterCallback = (arg0: string, arg1: StateTransitionMetadata) => void;
25
+ export type OnEnterCallback = (currentState: string, transition: TransitionData) => void;
26
26
  /**
27
27
  * Callback function called when exiting a state
28
28
  */
29
- export type OnExitCallback = (arg0: string, arg1: StateTransitionMetadata) => void;
29
+ export type OnExitCallback = (currentState: string, transition: TransitionData) => void;
@@ -5,24 +5,30 @@
5
5
  /**
6
6
  * Metadata object passed to state transition callbacks
7
7
  *
8
- * @typedef {object} StateTransitionMetadata
8
+ * @typedef {object} TransitionData
9
9
  * @property {string} from - The state being exited
10
10
  * @property {string} to - The state being entered
11
11
  * @property {string} event - The event that triggered the transition
12
12
  * @property {any[]} args - Arguments passed to the send() method
13
13
  */
14
14
 
15
- /**
16
- * Callback function called when entering a state
17
- *
18
- * @typedef {function(string, StateTransitionMetadata): void} OnEnterCallback
19
- */
15
+ /**
16
+ * Callback function called when entering a state
17
+ *
18
+ * @callback OnEnterCallback
19
+ * @param {string} currentState - The state being entered
20
+ * @param {TransitionData} transition - Details about the transition
21
+ * @returns {void}
22
+ */
20
23
 
21
- /**
22
- * Callback function called when exiting a state
23
- *
24
- * @typedef {function(string, StateTransitionMetadata): void} OnExitCallback
25
- */
24
+ /**
25
+ * Callback function called when exiting a state
26
+ *
27
+ * @callback OnExitCallback
28
+ * @param {string} currentState - The state being exited
29
+ * @param {TransitionData} transition - Details about the transition
30
+ * @returns {void}
31
+ */
26
32
 
27
- // Export types for external use (this is just for JSDoc, no actual exports needed)
33
+ // Export types for JSdoc
28
34
  export {};
@@ -10,13 +10,13 @@ export default class LoadingStateMachine extends FiniteStateMachine {
10
10
  * - Only valid when currently loading
11
11
  * - Useful for external timeout management
12
12
  */
13
- doTimeout(): void;
13
+ timeout(): void;
14
14
  /**
15
- * Transition to cancelled state
15
+ * Transition to aborting state
16
16
  * - Only valid when currently loading
17
- * - Useful for external cancellation management
17
+ * - Useful for external abort management
18
18
  */
19
- doCancel(): void;
19
+ abort(): void;
20
20
  #private;
21
21
  }
22
22
  import FiniteStateMachine from '../finite-state-machine/FiniteStateMachine.svelte.js';
@@ -8,14 +8,16 @@ import {
8
8
  STATE_LOADING,
9
9
  STATE_UNLOADING,
10
10
  STATE_LOADED,
11
- STATE_CANCELLED,
11
+ STATE_ABORTING,
12
+ STATE_ABORTED,
12
13
  STATE_ERROR,
13
14
  STATE_TIMEOUT,
14
15
 
15
16
  // > Signals
16
17
  INITIAL,
17
18
  LOAD,
18
- CANCEL,
19
+ ABORT,
20
+ ABORTED,
19
21
  ERROR,
20
22
  LOADED,
21
23
  UNLOAD,
@@ -41,7 +43,7 @@ export default class LoadingStateMachine extends FiniteStateMachine {
41
43
  // _enter: () => {
42
44
  // console.log('LoadingStateMachine: enter LOADING');
43
45
  // },
44
- [CANCEL]: STATE_CANCELLED,
46
+ [ABORT]: STATE_ABORTING,
45
47
  [ERROR]: STATE_ERROR,
46
48
  [LOADED]: STATE_LOADED,
47
49
  [TIMEOUT]: STATE_TIMEOUT
@@ -57,7 +59,11 @@ export default class LoadingStateMachine extends FiniteStateMachine {
57
59
  [ERROR]: STATE_ERROR,
58
60
  [INITIAL]: STATE_INITIAL
59
61
  },
60
- [STATE_CANCELLED]: {
62
+ [STATE_ABORTING]: {
63
+ [ERROR]: STATE_ERROR,
64
+ [ABORTED]: STATE_ABORTED
65
+ },
66
+ [STATE_ABORTED]: {
61
67
  [LOAD]: STATE_LOADING,
62
68
  [UNLOAD]: STATE_UNLOADING
63
69
  },
@@ -99,16 +105,16 @@ export default class LoadingStateMachine extends FiniteStateMachine {
99
105
  * - Only valid when currently loading
100
106
  * - Useful for external timeout management
101
107
  */
102
- doTimeout() {
108
+ timeout() {
103
109
  this.send(TIMEOUT);
104
110
  }
105
111
 
106
112
  /**
107
- * Transition to cancelled state
113
+ * Transition to aborting state
108
114
  * - Only valid when currently loading
109
- * - Useful for external cancellation management
115
+ * - Useful for external abort management
110
116
  */
111
- doCancel() {
112
- this.send(CANCEL);
117
+ abort() {
118
+ this.send(ABORT);
113
119
  }
114
120
  }