@hkdigital/lib-core 0.4.43 → 0.4.45

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.
@@ -34,6 +34,8 @@
34
34
 
35
35
  /** @typedef {import('../../../generic/typedef.js').ErrorDetails} ErrorDetails */
36
36
 
37
+ import * as expect from '../../../util/expect.js';
38
+
37
39
  import { EventEmitter } from '../../../generic/events.js';
38
40
 
39
41
  import {
@@ -248,6 +250,8 @@ export default class Logger extends EventEmitter {
248
250
  * @param {import('../../typedef.js').LogEventData} eventData
249
251
  */
250
252
  logFromEvent(eventData) {
253
+ expect.object(eventData);
254
+
251
255
  const level = eventData.level;
252
256
 
253
257
  // Check if this log level should be filtered
@@ -10,7 +10,8 @@ import {
10
10
  LOAD,
11
11
  LOADED,
12
12
  ABORT,
13
- ABORTED
13
+ ABORTED,
14
+ ERROR
14
15
  } from '../../../state/machines.js';
15
16
 
16
17
  import { waitForState } from '../../../util/svelte.js';
@@ -89,25 +90,39 @@ export default class SceneBase {
89
90
  const state = this.#state;
90
91
 
91
92
  $effect(() => {
92
- if (state.current === STATE_LOADING) {
93
+ if (this.#state.current === STATE_LOADING) {
93
94
  const { sourcesLoaded, numberOfSources } = this.#progress;
94
95
 
95
- if (sourcesLoaded === numberOfSources) {
96
+ if (sourcesLoaded === numberOfSources && numberOfSources > 0) {
96
97
  this.#state.send(LOADED);
97
98
  }
98
99
  }
99
100
  });
100
101
 
101
102
  $effect(() => {
102
- if (state.current === STATE_ABORTING) {
103
+ if (this.#state.current === STATE_ABORTING) {
103
104
  const { sourcesAborted, numberOfSources } = this.#abortProgress;
104
105
 
105
- if (sourcesAborted === numberOfSources) {
106
+ if (sourcesAborted === numberOfSources && numberOfSources > 0) {
106
107
  this.#state.send(ABORTED);
107
108
  }
108
109
  }
109
110
  });
110
111
 
112
+ $effect(() => {
113
+ if (this.#state.current === STATE_LOADING) {
114
+ // Check if any source failed during loading
115
+ const sources = this.sources;
116
+ for (const source of sources) {
117
+ const loader = this.getLoaderFromSource(source);
118
+ if (loader.state === STATE_ERROR) {
119
+ this.#state.send(ERROR, loader.error || new Error('Source loading failed'));
120
+ break;
121
+ }
122
+ }
123
+ }
124
+ });
125
+
111
126
  state.onenter = (currentState) => {
112
127
  if (currentState === STATE_LOADING) {
113
128
  this.#startLoading();
@@ -253,7 +268,7 @@ export default class SceneBase {
253
268
  if (isAborted || this.state === STATE_ABORTED) {
254
269
  reject(new Error('Preload was aborted'));
255
270
  } else if (this.state === STATE_ERROR) {
256
- reject(new Error('Preload failed due to error'));
271
+ reject(this.#state.error);
257
272
  } else if (this.loaded) {
258
273
  resolve(this);
259
274
  } else {
@@ -28,8 +28,24 @@ export class ServiceManager extends EventEmitter {
28
28
  services: Map<string, ServiceEntry>;
29
29
  /** @type {Logger} */
30
30
  logger: Logger;
31
- /** @type {ServiceManagerConfig} */
32
- config: ServiceManagerConfig;
31
+ /**
32
+ * @type {{
33
+ * debug: boolean,
34
+ * autoStart: boolean,
35
+ * stopTimeout: number,
36
+ * defaultLogLevel: LogLevel,
37
+ * managerLogLevel: LogLevel,
38
+ * serviceLogLevels?: Record<string,LogLevel>
39
+ * }}
40
+ **/
41
+ config: {
42
+ debug: boolean;
43
+ autoStart: boolean;
44
+ stopTimeout: number;
45
+ defaultLogLevel: LogLevel;
46
+ managerLogLevel: LogLevel;
47
+ serviceLogLevels?: Record<string, LogLevel>;
48
+ };
33
49
  /**
34
50
  * Attach a plugin to the ServiceManager
35
51
  *
@@ -164,22 +180,20 @@ export class ServiceManager extends EventEmitter {
164
180
  /**
165
181
  * Set log level for the ServiceManager itself
166
182
  *
167
- * @param {string} level - Log level to set for the ServiceManager
183
+ * @param {LogLevel} level - Log level to set for the ServiceManager
168
184
  */
169
- setManagerLogLevel(level: string): void;
185
+ setManagerLogLevel(level: LogLevel): void;
170
186
  /**
171
187
  * Set log level for individual services
172
188
  *
173
- * @param {string|Object<string,string>} nameOrConfig
189
+ * @param {string|Record<string,LogLevel>} nameOrConfig
174
190
  * Service configuration:
175
191
  * - String with service name: 'auth' (requires level parameter)
176
192
  * - String with config: 'auth:debug,database:info'
177
193
  * - Object: { auth: 'debug', database: 'info' }
178
194
  * @param {LogLevel} [level] - Log level (required when nameOrConfig is service name)
179
195
  */
180
- setServiceLogLevel(nameOrConfig: string | {
181
- [x: string]: string;
182
- }, level?: LogLevel): void;
196
+ setServiceLogLevel(nameOrConfig: string | Record<string, LogLevel>, level?: LogLevel): void;
183
197
  /**
184
198
  * Get all services with a specific tag
185
199
  *
@@ -130,14 +130,24 @@ export class ServiceManager extends EventEmitter {
130
130
  /** @type {Logger} */
131
131
  this.logger = new Logger('ServiceManager', managerLogLevel);
132
132
 
133
- /** @type {ServiceManagerConfig} */
133
+ /**
134
+ * @type {{
135
+ * debug: boolean,
136
+ * autoStart: boolean,
137
+ * stopTimeout: number,
138
+ * defaultLogLevel: LogLevel,
139
+ * managerLogLevel: LogLevel,
140
+ * serviceLogLevels?: Record<string,LogLevel>
141
+ * }}
142
+ **/
143
+ // @ts-ignore (managerLogLevel set later)
134
144
  this.config = {
135
145
  debug: config.debug ?? false,
136
146
  autoStart: config.autoStart ?? false,
137
147
  stopTimeout: config.stopTimeout || 10000,
138
148
  defaultLogLevel
139
149
  // managerLogLevel will be set bysetManagerLogLevel()
140
- // serviceLogLevels will be set by setServiceLogLevel()
150
+ // serviceLogLevels will be optionally set by setServiceLogLevel()
141
151
  };
142
152
 
143
153
  this.setManagerLogLevel(managerLogLevel);
@@ -145,7 +155,8 @@ export class ServiceManager extends EventEmitter {
145
155
  if (serviceLogLevels) {
146
156
  // Parse and store service log levels, but don't apply them yet
147
157
  // They will be applied when services are created in get()
148
- /** @type {{[name:string]: LogLevel}} */
158
+
159
+ /** @type {Record<string,LogLevel>} */
149
160
  let parsedServiceLevels = {};
150
161
 
151
162
  if (typeof serviceLogLevels === 'string') {
@@ -591,7 +602,7 @@ export class ServiceManager extends EventEmitter {
591
602
  /**
592
603
  * Set log level for the ServiceManager itself
593
604
  *
594
- * @param {string} level - Log level to set for the ServiceManager
605
+ * @param {LogLevel} level - Log level to set for the ServiceManager
595
606
  */
596
607
  setManagerLogLevel(level) {
597
608
  this.config.managerLogLevel = level;
@@ -601,7 +612,7 @@ export class ServiceManager extends EventEmitter {
601
612
  /**
602
613
  * Set log level for individual services
603
614
  *
604
- * @param {string|Object<string,string>} nameOrConfig
615
+ * @param {string|Record<string,LogLevel>} nameOrConfig
605
616
  * Service configuration:
606
617
  * - String with service name: 'auth' (requires level parameter)
607
618
  * - String with config: 'auth:debug,database:info'
@@ -609,7 +620,7 @@ export class ServiceManager extends EventEmitter {
609
620
  * @param {LogLevel} [level] - Log level (required when nameOrConfig is service name)
610
621
  */
611
622
  setServiceLogLevel(nameOrConfig, level) {
612
- /** @type {{[name:string]: LogLevel}} */
623
+ /** @type {Record<string,LogLevel>} */
613
624
  let serviceLevels = {};
614
625
 
615
626
  if (typeof nameOrConfig === 'string') {
@@ -630,7 +641,9 @@ export class ServiceManager extends EventEmitter {
630
641
  serviceLevels = nameOrConfig;
631
642
  }
632
643
 
644
+ // Ensure serviceLogLevels is initialized as an object
633
645
  if (!this.config.serviceLogLevels) {
646
+ /** @type {Record<string,LogLevel>} */
634
647
  this.config.serviceLogLevels = {};
635
648
  }
636
649
 
@@ -51,9 +51,7 @@ export type ServiceManagerConfig = {
51
51
  * - String: "auth:debug,database:info"
52
52
  * - Object: { auth: "debug", database: "info" }
53
53
  */
54
- serviceLogLevels?: string | {
55
- [x: string]: import("../../logging/typedef.js").LogLevel;
56
- } | undefined;
54
+ serviceLogLevels?: string | Record<string, import("../../logging/typedef.js").LogLevel> | undefined;
57
55
  };
58
56
  /**
59
57
  * Result of health check for all services
@@ -56,7 +56,7 @@
56
56
  * @property {number} [stopTimeout=10000] - Default timeout for stopping services
57
57
  * @property {LogLevel} [defaultLogLevel] - Default log level for new services
58
58
  * @property {LogLevel} [managerLogLevel] - Initial log level for ServiceManager
59
- * @property {string|Object<string,LogLevel>} [serviceLogLevels]
59
+ * @property {string|Record<string,LogLevel>} [serviceLogLevels]
60
60
  * Per-service log levels:
61
61
  * - String: "auth:debug,database:info"
62
62
  * - Object: { auth: "debug", database: "info" }
@@ -23,11 +23,11 @@ import { DEBUG, INFO, WARN, ERROR } from '../../logging/index.js';
23
23
  */
24
24
  export function parseServiceLogLevels(configString) {
25
25
  if (!configString || typeof configString !== 'string') {
26
- /** @type {Object<string, string>} */
26
+ /** @type {Record<string, LogLevel>} */
27
27
  return {};
28
28
  }
29
29
 
30
- /** @type {Object<string, string>} */
30
+ /** @type {Record<string, LogLevel>} */
31
31
  const result = {};
32
32
 
33
33
  const services = configString.split(',');
@@ -39,7 +39,7 @@ export function parseServiceLogLevels(configString) {
39
39
  const parts = trimmed.split(':');
40
40
  if (parts.length === 2) {
41
41
  const [serviceName, logLevel] = parts;
42
- result[serviceName.trim()] = logLevel.trim();
42
+ result[serviceName.trim()] = /** @type {LogLevel} */ (logLevel.trim());
43
43
  }
44
44
  }
45
45
 
@@ -65,6 +65,7 @@ export default class LoadingStateMachine extends FiniteStateMachine {
65
65
  },
66
66
  [STATE_ABORTED]: {
67
67
  [LOAD]: STATE_LOADING,
68
+ [LOADED]: STATE_LOADED,
68
69
  [UNLOAD]: STATE_UNLOADING
69
70
  },
70
71
  [STATE_TIMEOUT]: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hkdigital/lib-core",
3
- "version": "0.4.43",
3
+ "version": "0.4.45",
4
4
  "author": {
5
5
  "name": "HKdigital",
6
6
  "url": "https://hkdigital.nl"