@hkdigital/lib-sveltekit 0.2.11 → 0.2.13

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,147 +1,149 @@
1
1
  /**
2
- * Base class for all services
2
+ * @typedef {import('./typedef.js').ServiceConfig} ServiceConfig
3
+ * @typedef {import('./typedef.js').ServiceOptions} ServiceOptions
4
+ * @typedef {import('./typedef.js').StopOptions} StopOptions
5
+ * @typedef {import('./typedef.js').HealthStatus} HealthStatus
6
+ * @typedef {import('./typedef.js').StateChangeEvent} StateChangeEvent
7
+ * @typedef {import('./typedef.js').HealthChangeEvent} HealthChangeEvent
8
+ * @typedef {import('./typedef.js').ServiceErrorEvent} ServiceErrorEvent
3
9
  */
4
- export default class ServiceBase {
10
+ /**
11
+ * Base class for all services with lifecycle management
12
+ * @extends EventEmitter
13
+ */
14
+ export class ServiceBase extends EventEmitter {
5
15
  /**
6
- * Create a new service
16
+ * Create a new service instance
7
17
  *
8
18
  * @param {string} name - Service name
9
- * @param {Object} [options] - Service options
10
- * @param {string} [options.logLevel=INFO] - Initial log level
11
- */
12
- constructor(name: string, options?: {
13
- logLevel?: string;
14
- });
15
- /**
16
- * Service name
17
- * @type {string}
19
+ * @param {ServiceOptions} [options={}] - Service options
18
20
  */
21
+ constructor(name: string, options?: ServiceOptions);
22
+ /** @type {string} */
19
23
  name: string;
20
- /**
21
- * Event emitter for service events
22
- * @type {EventEmitter}
23
- */
24
- events: EventEmitter;
25
- /**
26
- * Current service state
27
- * @type {string}
28
- */
24
+ /** @type {string} */
29
25
  state: string;
30
- /**
31
- * Last error that occurred
32
- * @type {Error|null}
33
- */
26
+ /** @type {boolean} */
27
+ healthy: boolean;
28
+ /** @type {Error|null} */
34
29
  error: Error | null;
35
- /**
36
- * Last stable state before error
37
- * @type {string|null}
38
- * @private
39
- */
40
- private _preErrorState;
41
- /**
42
- * Service logger
43
- * @type {Logger}
44
- */
30
+ /** @type {Logger} */
45
31
  logger: Logger;
32
+ /** @private @type {number} */
33
+ private _shutdownTimeout;
46
34
  /**
47
- * Set the service log level
35
+ * Initialize the service with configuration
48
36
  *
49
- * @param {string} level - New log level
50
- * @returns {boolean} True if level was set, false if invalid
51
- */
52
- setLogLevel(level: string): boolean;
53
- /**
54
- * Initialize the service
37
+ * @param {ServiceConfig} [config={}] - Service-specific configuration
55
38
  *
56
- * @param {Object} [config] - Service configuration
57
- * @returns {Promise<boolean>} True if initialized successfully
39
+ * @returns {Promise<boolean>} True if initialization succeeded
58
40
  */
59
- initialize(config?: any): Promise<boolean>;
41
+ initialize(config?: ServiceConfig): Promise<boolean>;
60
42
  /**
61
43
  * Start the service
62
44
  *
63
- * @returns {Promise<boolean>} True if started successfully
45
+ * @returns {Promise<boolean>} True if the service started successfully
64
46
  */
65
47
  start(): Promise<boolean>;
66
48
  /**
67
- * Stop the service
49
+ * Stop the service with optional timeout
68
50
  *
69
- * @returns {Promise<boolean>} True if stopped successfully
51
+ * @param {StopOptions} [options={}] - Stop options
52
+ *
53
+ * @returns {Promise<boolean>} True if the service stopped successfully
70
54
  */
71
- stop(): Promise<boolean>;
55
+ stop(options?: StopOptions): Promise<boolean>;
72
56
  /**
73
- * Recover the service
57
+ * Recover the service from error state
74
58
  *
75
- * @returns {Promise<boolean>} True if stopped successfully
59
+ * @returns {Promise<boolean>} True if recovery succeeded
76
60
  */
77
61
  recover(): Promise<boolean>;
78
62
  /**
79
- * Destroy the service
63
+ * Destroy the service and cleanup resources
80
64
  *
81
- * @returns {Promise<boolean>} True if destroyed successfully
65
+ * @returns {Promise<boolean>} True if destruction succeeded
82
66
  */
83
67
  destroy(): Promise<boolean>;
84
68
  /**
85
- * Add an event listener
69
+ * Get the current health status of the service
86
70
  *
87
- * @param {string} eventName - Event name
88
- * @param {Function} handler - Event handler
89
- * @returns {Function} Unsubscribe function
71
+ * @returns {Promise<HealthStatus>} Health status object
90
72
  */
91
- on(eventName: string, handler: Function): Function;
73
+ getHealth(): Promise<HealthStatus>;
92
74
  /**
93
- * Emit an event
75
+ * Set the service log level
94
76
  *
95
- * @param {string} eventName - Event name
96
- * @param {*} data - Event data
97
- * @returns {boolean} True if event had listeners
77
+ * @param {string} level - New log level
78
+ *
79
+ * @returns {boolean} True if the level was set successfully
98
80
  */
99
- emit(eventName: string, data: any): boolean;
81
+ setLogLevel(level: string): boolean;
100
82
  /**
101
- * Initialize the service (to be overridden)
83
+ * Initialize the service (override in subclass)
102
84
  *
103
85
  * @protected
104
- * @param {Object} config - Service configuration
86
+ * @param {ServiceConfig} config - Service configuration
87
+ *
105
88
  * @returns {Promise<void>}
106
89
  */
107
- protected _init(config: any): Promise<void>;
90
+ protected _init(config: ServiceConfig): Promise<void>;
108
91
  /**
109
- * Start the service (to be overridden)
92
+ * Start the service (override in subclass)
110
93
  *
111
94
  * @protected
95
+ *
112
96
  * @returns {Promise<void>}
113
97
  */
114
98
  protected _start(): Promise<void>;
115
99
  /**
116
- * Stop the service (to be overridden)
100
+ * Stop the service (override in subclass)
117
101
  *
118
102
  * @protected
103
+ *
119
104
  * @returns {Promise<void>}
120
105
  */
121
106
  protected _stop(): Promise<void>;
122
107
  /**
123
- * Destroy the service (to be overridden)
108
+ * Destroy the service (optional override)
124
109
  *
125
110
  * @protected
111
+ *
126
112
  * @returns {Promise<void>}
127
113
  */
128
114
  protected _destroy(): Promise<void>;
129
115
  /**
130
- * Recover the service from an error (to be overridden)
116
+ * Recover from error state (optional override)
131
117
  *
132
118
  * @protected
119
+ *
133
120
  * @returns {Promise<void>}
134
121
  */
135
122
  protected _recover(): Promise<void>;
136
123
  /**
137
- * Set the service state
124
+ * Perform health check (optional override)
125
+ *
126
+ * @protected
127
+ *
128
+ * @returns {Promise<Object>} Additional health information
129
+ */
130
+ protected _healthCheck(): Promise<any>;
131
+ /**
132
+ * Set the service state and emit event
138
133
  *
139
134
  * @private
140
- * @param {string} state - New state
135
+ * @param {string} newState - New state value
141
136
  */
142
137
  private _setState;
143
138
  /**
144
- * Set an error state
139
+ * Set the health status and emit event if changed
140
+ *
141
+ * @private
142
+ * @param {boolean} healthy - New health status
143
+ */
144
+ private _setHealthy;
145
+ /**
146
+ * Set error state and emit error event
145
147
  *
146
148
  * @private
147
149
  * @param {string} operation - Operation that failed
@@ -149,5 +151,13 @@ export default class ServiceBase {
149
151
  */
150
152
  private _setError;
151
153
  }
154
+ export default ServiceBase;
155
+ export type ServiceConfig = import("./typedef.js").ServiceConfig;
156
+ export type ServiceOptions = import("./typedef.js").ServiceOptions;
157
+ export type StopOptions = import("./typedef.js").StopOptions;
158
+ export type HealthStatus = import("./typedef.js").HealthStatus;
159
+ export type StateChangeEvent = import("./typedef.js").StateChangeEvent;
160
+ export type HealthChangeEvent = import("./typedef.js").HealthChangeEvent;
161
+ export type ServiceErrorEvent = import("./typedef.js").ServiceErrorEvent;
152
162
  import { EventEmitter } from '../events';
153
163
  import { Logger } from '../logging';