@adapt-arch/utiliti-es 0.3.4 → 0.4.2

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,125 +1,65 @@
1
- var f = Object.defineProperty;
2
- var d = (i, s, e) => s in i ? f(i, s, { enumerable: !0, configurable: !0, writable: !0, value: e }) : i[s] = e;
3
- var r = (i, s, e) => d(i, typeof s != "symbol" ? s + "" : s, e);
4
- const h = (i, s) => {
5
- clearTimeout(s.ref), s.ref = void 0;
6
- const e = i.timeouts.indexOf(s);
7
- i.timeouts.splice(e, 1);
8
- };
9
- class y {
1
+ var r;
2
+ (function(i) {
3
+ i[i.Trace = 0] = "Trace", i[i.Debug = 1] = "Debug", i[i.Information = 2] = "Information", i[i.Warning = 3] = "Warning", i[i.Error = 4] = "Error", i[i.Critical = 5] = "Critical", i[i.None = 6] = "None";
4
+ })(r || (r = {}));
5
+ class a {
10
6
  /**
11
- *
7
+ * The timestamp of the log message.
12
8
  */
13
- constructor(s) {
14
- r(this, "_subscriptions", /* @__PURE__ */ new Map());
15
- r(this, "_options");
16
- var e;
17
- if (this._options = s, (e = this._options) != null && e.plugins)
18
- for (const t of this._options.plugins)
19
- t.init && t.init(this);
20
- }
21
- /** @inheritdoc */
22
- publish(s, e) {
23
- var a;
24
- const t = {
25
- topic: s,
26
- message: e
27
- };
28
- if ((a = this._options) != null && a.plugins)
29
- for (const l of this._options.plugins)
30
- l.onPublish && l.onPublish(t);
31
- if (!t.topic)
32
- throw new Error("Invalid topic.");
33
- if (!t.message)
34
- throw new Error("Invalid message.");
35
- const n = this._subscriptions.get(t.topic);
36
- if (n)
37
- for (const l of n.values()) {
38
- const u = {};
39
- l.timeouts.push(u), u.ref = setTimeout((p, _, g, m) => {
40
- p.handler(g, m), h(l, _);
41
- }, 0, l, u, structuredClone(t.topic), structuredClone(t.message));
42
- }
43
- }
44
- /** @inheritdoc */
45
- subscribe(s, e) {
46
- if (!s)
47
- throw new Error("Invalid topic.");
48
- if (!e)
49
- throw new Error("Invalid handler.");
50
- let t = this._subscriptions.get(s);
51
- t || (t = /* @__PURE__ */ new Map(), this._subscriptions.set(structuredClone(s), t));
52
- const n = `sub-${Date.now()}`;
53
- return t.set(n, { handler: e, timeouts: [] }), n;
54
- }
55
- /** @inheritdoc */
56
- unsubscribe(s) {
57
- if (s)
58
- for (const e of this._subscriptions.values()) {
59
- for (const t of e.values())
60
- for (; ; ) {
61
- const n = t.timeouts.pop();
62
- if (!n)
63
- break;
64
- h(t, n);
65
- }
66
- if (e.delete(s))
67
- return;
68
- }
69
- }
70
- [Symbol.dispose]() {
71
- var s;
72
- for (const e of this._subscriptions.values())
73
- for (const t of e.keys())
74
- this.unsubscribe(t);
75
- if (this._subscriptions.clear(), (s = this._options) != null && s.plugins)
76
- for (const e of this._options.plugins) {
77
- const t = e[Symbol.dispose];
78
- t && t.call(e);
79
- }
80
- return Promise.resolve();
81
- }
9
+ timestamp = Date.now();
10
+ /**
11
+ * The level of the log message.
12
+ */
13
+ level = r.None;
14
+ /**
15
+ * The name of the logger.
16
+ */
17
+ name = "";
18
+ /**
19
+ * The message to log.
20
+ */
21
+ message = "";
22
+ /**
23
+ * The error message.
24
+ */
25
+ errorMessage;
26
+ /**
27
+ * The stack trace of the error.
28
+ */
29
+ stackTrace;
30
+ /**
31
+ * Any extra parameters to log.
32
+ */
33
+ extraParams;
82
34
  }
83
- class P {
35
+ class f {
36
+ _valuesFn;
37
+ _overrideExisting;
84
38
  /**
85
39
  * Constructor.
86
40
  *
87
- * @param {Options} options The options.
41
+ * @param {DynamicValuesFunction} valuesFunction The values to add to the log.
42
+ * @param {boolean} overrideExisting Override a value if it already exists.
88
43
  */
89
- constructor(s) {
90
- r(this, "_options");
91
- r(this, "_channel");
92
- r(this, "_eventListeners", null);
93
- r(this, "_broadcastEnabled", !0);
94
- this._options = s, this._channel = new BroadcastChannel(this._options.channelName);
95
- }
96
- /** @inheritdoc */
97
- init(s) {
98
- this._eventListeners || (this._eventListeners = (e) => {
99
- const t = e.data;
100
- if (!(!t.topic || !t.message))
101
- try {
102
- this._broadcastEnabled = !1, s.publish(t.topic, t.message);
103
- } finally {
104
- this._broadcastEnabled = !0;
105
- }
106
- }, this._channel.addEventListener("message", this._eventListeners));
44
+ constructor(s, e) {
45
+ this._valuesFn = s, this._overrideExisting = e;
107
46
  }
108
- /** @inheritdoc */
109
- onPublish(s) {
110
- if (!this._broadcastEnabled || !s.topic || !s.message)
47
+ /**
48
+ * @inheritdoc
49
+ */
50
+ enrich(s) {
51
+ const e = typeof this._valuesFn == "function" ? this._valuesFn() : void 0;
52
+ if (!e)
111
53
  return;
112
- const e = {
113
- topic: s.topic,
114
- message: s.message
115
- };
116
- this._channel.postMessage(e);
117
- }
118
- [Symbol.dispose]() {
119
- this._eventListeners && this._channel.removeEventListener("message", this._eventListeners), this._channel.close();
54
+ s.extraParams = s.extraParams || {};
55
+ const t = Object.keys(s.extraParams);
56
+ for (const o in e)
57
+ t.indexOf(o) !== -1 && !this._overrideExisting || (s.extraParams[o] = e[o]);
120
58
  }
121
59
  }
122
- class T {
60
+ class d {
61
+ _values;
62
+ _overrideExisting;
123
63
  /**
124
64
  * Constructor.
125
65
  *
@@ -127,8 +67,6 @@ class T {
127
67
  * @param {boolean} overrideExisting Override a value if it already exists.
128
68
  */
129
69
  constructor(s, e) {
130
- r(this, "_values");
131
- r(this, "_overrideExisting");
132
70
  this._values = s, this._overrideExisting = e;
133
71
  }
134
72
  /**
@@ -143,75 +81,172 @@ class T {
143
81
  e.indexOf(t) !== -1 && !this._overrideExisting || (s.extraParams[t] = this._values[t]);
144
82
  }
145
83
  }
146
- class E {
84
+ class b {
85
+ _options;
147
86
  /**
148
87
  * Constructor.
149
88
  *
150
- * @param {DynamicValuesFunction} valuesFunction The values to add to the log.
151
- * @param {boolean} overrideExisting Override a value if it already exists.
89
+ * @param {LoggerOptions} options The logger options.
152
90
  */
153
- constructor(s, e) {
154
- r(this, "_valuesFn");
155
- r(this, "_overrideExisting");
156
- this._valuesFn = s, this._overrideExisting = e;
91
+ constructor(s) {
92
+ this._options = s;
93
+ }
94
+ /**
95
+ * The core logging method.
96
+ *
97
+ * @param {LogMessage} message The message to log.
98
+ */
99
+ logMessageCore(s) {
100
+ s.name = this._options.name;
101
+ for (const e of this._options.enrichers)
102
+ e.enrich(s);
103
+ this._options.reporter?.register(s);
157
104
  }
158
105
  /**
159
106
  * @inheritdoc
160
107
  */
161
- enrich(s) {
162
- const e = typeof this._valuesFn == "function" ? this._valuesFn() : void 0;
163
- if (!e)
164
- return;
165
- s.extraParams = s.extraParams || {};
166
- const t = Object.keys(s.extraParams);
167
- for (const n in e)
168
- t.indexOf(n) !== -1 && !this._overrideExisting || (s.extraParams[n] = e[n]);
108
+ async [Symbol.asyncDispose]() {
109
+ await this._options.reporter?.[Symbol.asyncDispose]();
110
+ }
111
+ /**
112
+ * @inheritdoc
113
+ */
114
+ isEnabled(s) {
115
+ return s !== r.None && s >= this._options.minimumLevel;
116
+ }
117
+ /**
118
+ * @inheritdoc
119
+ */
120
+ trace(s) {
121
+ const e = new a();
122
+ e.level = r.Trace, e.message = s, this.logMessage(e);
123
+ }
124
+ /**
125
+ * @inheritdoc
126
+ */
127
+ debug(s) {
128
+ const e = new a();
129
+ e.level = r.Debug, e.message = s, this.logMessage(e);
130
+ }
131
+ /**
132
+ * @inheritdoc
133
+ */
134
+ info(s) {
135
+ const e = new a();
136
+ e.level = r.Information, e.message = s, this.logMessage(e);
137
+ }
138
+ /**
139
+ * @inheritdoc
140
+ */
141
+ warn(s) {
142
+ const e = new a();
143
+ e.level = r.Warning, e.message = s, this.logMessage(e);
144
+ }
145
+ /**
146
+ * @inheritdoc
147
+ */
148
+ error(s) {
149
+ const e = new a();
150
+ e.level = r.Error, e.message = s, this.logMessage(e);
151
+ }
152
+ /**
153
+ * @inheritdoc
154
+ */
155
+ crit(s) {
156
+ const e = new a();
157
+ e.level = r.Critical, e.message = s, this.logMessage(e);
158
+ }
159
+ /**
160
+ * @inheritdoc
161
+ */
162
+ log(s, e, t, o) {
163
+ const n = new a(), c = this._extractErrorDetails(t);
164
+ n.level = s, n.message = e, n.errorMessage = c?.message, n.stackTrace = c?.stack, n.extraParams = o, this.logMessage(n);
165
+ }
166
+ /**
167
+ * @inheritdoc
168
+ */
169
+ logMessage(s) {
170
+ this.isEnabled(s.level) && setTimeout(() => {
171
+ this.logMessageCore(s);
172
+ }, 1);
173
+ }
174
+ _extractErrorDetails(s) {
175
+ if (s instanceof Error)
176
+ return { message: s.message, stack: s.stack };
177
+ switch (typeof s) {
178
+ case "string":
179
+ return { message: s };
180
+ case "object": {
181
+ const e = s;
182
+ if (e === null)
183
+ break;
184
+ const t = {
185
+ message: e.message ?? e.Message,
186
+ stack: e.stack ?? e.Stack ?? e.stackTrace ?? e.StackTrace
187
+ };
188
+ return t.message === void 0 && (t.message = JSON.stringify(s)), t;
189
+ }
190
+ default: {
191
+ const e = s;
192
+ if (typeof e?.toString == "function")
193
+ return { message: e.toString() };
194
+ break;
195
+ }
196
+ }
169
197
  }
170
198
  }
171
- var o;
172
- (function(i) {
173
- i[i.Trace = 0] = "Trace", i[i.Debug = 1] = "Debug", i[i.Information = 2] = "Information", i[i.Warning = 3] = "Warning", i[i.Error = 4] = "Error", i[i.Critical = 5] = "Critical", i[i.None = 6] = "None";
174
- })(o || (o = {}));
175
- class c {
176
- constructor() {
177
- /**
178
- * The timestamp of the log message.
179
- */
180
- r(this, "timestamp", (/* @__PURE__ */ new Date()).getTime());
181
- /**
182
- * The level of the log message.
183
- */
184
- r(this, "level", o.None);
185
- /**
186
- * The name of the logger.
187
- */
188
- r(this, "name", "");
189
- /**
190
- * The message to log.
191
- */
192
- r(this, "message", "");
193
- /**
194
- * The error message.
195
- */
196
- r(this, "errorMessage");
197
- /**
198
- * The stack trace of the error.
199
- */
200
- r(this, "stackTrace");
201
- /**
202
- * Any extra parameters to log.
203
- */
204
- r(this, "extraParams");
199
+ class v {
200
+ /**
201
+ * The name of the logger.
202
+ */
203
+ name = "";
204
+ /**
205
+ * The reporter for the messages.
206
+ */
207
+ reporter = null;
208
+ /**
209
+ * The minimum log level.
210
+ */
211
+ minimumLevel = r.Warning;
212
+ /**
213
+ * Log enrichers.
214
+ */
215
+ enrichers = [];
216
+ /**
217
+ * Get the LogLevel from a string value.
218
+ *
219
+ * @param {String} level The log level as string.
220
+ */
221
+ static getLevel(s) {
222
+ switch ((s || "").toUpperCase()) {
223
+ case "TRACE":
224
+ return r.Trace;
225
+ case "DEBUG":
226
+ return r.Debug;
227
+ case "INFORMATION":
228
+ return r.Information;
229
+ case "WARNING":
230
+ return r.Warning;
231
+ case "ERROR":
232
+ return r.Error;
233
+ case "CRITICAL":
234
+ return r.Critical;
235
+ case "NONE":
236
+ return r.None;
237
+ default:
238
+ return r.None;
239
+ }
205
240
  }
206
241
  }
207
- class M {
242
+ class y {
243
+ _console;
208
244
  /**
209
245
  * Constructor.
210
246
  *
211
247
  * @param {Console} console The current console reference.
212
248
  */
213
249
  constructor(s) {
214
- r(this, "_console");
215
250
  this._console = s;
216
251
  }
217
252
  /**
@@ -221,22 +256,22 @@ class M {
221
256
  let e;
222
257
  if (this._console)
223
258
  switch (s.level) {
224
- case o.Trace:
259
+ case r.Trace:
225
260
  e = this._console.trace || this._console.log;
226
261
  break;
227
- case o.Debug:
262
+ case r.Debug:
228
263
  e = this._console.debug || this._console.log;
229
264
  break;
230
- case o.Information:
265
+ case r.Information:
231
266
  e = this._console.info || this._console.log;
232
267
  break;
233
- case o.Warning:
268
+ case r.Warning:
234
269
  e = this._console.warn || this._console.log;
235
270
  break;
236
- case o.Error:
271
+ case r.Error:
237
272
  e = this._console.error || this._console.log;
238
273
  break;
239
- case o.Critical:
274
+ case r.Critical:
240
275
  e = this._console.error || this._console.log;
241
276
  break;
242
277
  // case LogLevel.None: // Do not log.
@@ -253,89 +288,8 @@ class M {
253
288
  return Promise.resolve();
254
289
  }
255
290
  }
256
- class x {
257
- constructor() {
258
- /**
259
- * Endpoint that receives the logs.
260
- */
261
- r(this, "endpoint", "");
262
- /**
263
- * HTTP verb used when calling the endpoint.
264
- */
265
- r(this, "verb", "POST");
266
- /**
267
- * The number of items to send in a batch.
268
- */
269
- r(this, "batchSize", 20);
270
- /**
271
- * The maximum interval, in milliseconds, to wait for the batch size to be achieved before reporting.
272
- */
273
- r(this, "interval", 2e3);
274
- /**
275
- * A function that can be used to transform the request before sending it.
276
- */
277
- r(this, "requestTransform");
278
- }
279
- }
280
- class R {
281
- constructor(s) {
282
- r(this, "_messageQueue");
283
- r(this, "_options");
284
- r(this, "_reportActionTimeoutRef");
285
- r(this, "_reportActionPromise");
286
- r(this, "_disposed");
287
- if (!s)
288
- throw new Error('Argument "options" is required');
289
- this._messageQueue = [], this._options = s, this._reportActionTimeoutRef = void 0, this._reportActionPromise = null, this._disposed = !1;
290
- }
291
- /**
292
- * @inheritdoc
293
- */
294
- register(s) {
295
- this._disposed || (this._messageQueue.push(s), this._scheduleNextProcessAction());
296
- }
297
- /**
298
- * @inheritdoc
299
- */
300
- async [Symbol.asyncDispose]() {
301
- if (this._disposed)
302
- return Promise.resolve();
303
- await (this._reportActionPromise ?? this._processMessages()), this._disposed = !0;
304
- }
305
- _scheduleNextProcessAction() {
306
- if (this._reportActionTimeoutRef)
307
- return;
308
- const s = this._messageQueue.length >= this._options.batchSize ? 0 : this._options.interval;
309
- this._reportActionTimeoutRef = setTimeout(() => {
310
- this._reportActionPromise = this._processMessages().then(() => {
311
- const e = this._reportActionTimeoutRef;
312
- this._reportActionTimeoutRef = void 0, clearTimeout(e), this._reportActionPromise = null, this._scheduleNextProcessAction();
313
- });
314
- }, s);
315
- }
316
- async _processMessages() {
317
- let s, e;
318
- for (; this._messageQueue.length > 0; )
319
- if (s = this._messageQueue.splice(0, Math.min(this._messageQueue.length, this._options.batchSize)), e = await this._sendMessagesBatch(s), !e) {
320
- this._messageQueue.unshift(...s);
321
- return;
322
- }
323
- }
324
- _sendMessagesBatch(s) {
325
- return new Promise((e) => {
326
- const t = () => {
327
- e(!1);
328
- }, n = new XMLHttpRequest();
329
- n.open(this._options.verb, this._options.endpoint), n.setRequestHeader("Content-Type", "application/json;charset=UTF-8"), this._options.requestTransform && this._options.requestTransform(n), n.onload = function() {
330
- e(this.status >= 200 && this.status < 300);
331
- }, n.onerror = t, n.onabort = t, n.send(JSON.stringify(s));
332
- });
333
- }
334
- }
335
- class k {
336
- constructor() {
337
- r(this, "_messages", []);
338
- }
291
+ class w {
292
+ _messages = [];
339
293
  get messages() {
340
294
  return this._messages.slice();
341
295
  }
@@ -352,9 +306,9 @@ class k {
352
306
  return Promise.resolve();
353
307
  }
354
308
  }
355
- class A {
309
+ class T {
310
+ _reporters;
356
311
  constructor(s) {
357
- r(this, "_reporters");
358
312
  this._reporters = s || [];
359
313
  }
360
314
  /**
@@ -368,160 +322,138 @@ class A {
368
322
  * @inheritdoc
369
323
  */
370
324
  async [Symbol.asyncDispose]() {
371
- const s = new Array();
325
+ const s = [];
372
326
  for (const e of this._reporters)
373
327
  s.push(e[Symbol.asyncDispose]());
374
328
  s.length && await Promise.all(s);
375
329
  }
376
330
  }
377
- class C {
331
+ class P {
378
332
  /**
379
- * Constructor.
380
- *
381
- * @param {LoggerOptions} options The logger options.
333
+ * Endpoint that receives the logs.
382
334
  */
383
- constructor(s) {
384
- r(this, "_options");
385
- this._options = s;
386
- }
335
+ endpoint = "";
387
336
  /**
388
- * The core logging method.
389
- *
390
- * @param {LogMessage} message The message to log.
337
+ * HTTP verb used when calling the endpoint.
391
338
  */
392
- logMessageCore(s) {
393
- var e;
394
- s.name = this._options.name;
395
- for (const t of this._options.enrichers)
396
- t.enrich(s);
397
- (e = this._options.reporter) == null || e.register(s);
398
- }
339
+ verb = "POST";
399
340
  /**
400
- * @inheritdoc
341
+ * The number of items to send in a batch.
401
342
  */
402
- async [Symbol.asyncDispose]() {
403
- var s;
404
- await ((s = this._options.reporter) == null ? void 0 : s[Symbol.asyncDispose]());
405
- }
343
+ batchSize = 20;
406
344
  /**
407
- * @inheritdoc
345
+ * The maximum interval, in milliseconds, to wait for the batch size to be achieved before reporting.
408
346
  */
409
- isEnabled(s) {
410
- return s !== o.None && s >= this._options.minimumLevel;
411
- }
347
+ interval = 2e3;
412
348
  /**
413
- * @inheritdoc
349
+ * A function that can be used to transform the request before sending it.
414
350
  */
415
- trace(s) {
416
- const e = new c();
417
- e.level = o.Trace, e.message = s, this.logMessage(e);
351
+ requestTransform;
352
+ }
353
+ class E {
354
+ _messageQueue;
355
+ _options;
356
+ _reportActionTimeoutRef;
357
+ _reportActionPromise;
358
+ _disposed;
359
+ constructor(s) {
360
+ if (!s)
361
+ throw new Error('Argument "options" is required');
362
+ this._messageQueue = [], this._options = s, this._reportActionTimeoutRef = void 0, this._reportActionPromise = null, this._disposed = !1;
418
363
  }
419
364
  /**
420
365
  * @inheritdoc
421
366
  */
422
- debug(s) {
423
- const e = new c();
424
- e.level = o.Debug, e.message = s, this.logMessage(e);
367
+ register(s) {
368
+ this._disposed || (this._messageQueue.push(s), this._scheduleNextProcessAction());
425
369
  }
426
370
  /**
427
371
  * @inheritdoc
428
372
  */
429
- info(s) {
430
- const e = new c();
431
- e.level = o.Information, e.message = s, this.logMessage(e);
373
+ async [Symbol.asyncDispose]() {
374
+ if (this._disposed)
375
+ return Promise.resolve();
376
+ await (this._reportActionPromise ?? this._processMessages()), this._disposed = !0;
432
377
  }
433
- /**
434
- * @inheritdoc
435
- */
436
- warn(s) {
437
- const e = new c();
438
- e.level = o.Warning, e.message = s, this.logMessage(e);
378
+ _scheduleNextProcessAction() {
379
+ if (this._reportActionTimeoutRef)
380
+ return;
381
+ const s = this._messageQueue.length >= this._options.batchSize ? 0 : this._options.interval;
382
+ this._reportActionTimeoutRef = setTimeout(() => {
383
+ this._reportActionPromise = this._processMessages().then(() => {
384
+ const e = this._reportActionTimeoutRef;
385
+ this._reportActionTimeoutRef = void 0, clearTimeout(e), this._reportActionPromise = null, this._scheduleNextProcessAction();
386
+ });
387
+ }, s);
439
388
  }
440
- /**
441
- * @inheritdoc
442
- */
443
- error(s) {
444
- const e = new c();
445
- e.level = o.Error, e.message = s, this.logMessage(e);
389
+ async _processMessages() {
390
+ let s, e;
391
+ for (; this._messageQueue.length > 0; )
392
+ if (s = this._messageQueue.splice(0, Math.min(this._messageQueue.length, this._options.batchSize)), e = await this._sendMessagesBatch(s), !e) {
393
+ this._messageQueue.unshift(...s);
394
+ return;
395
+ }
446
396
  }
447
- /**
448
- * @inheritdoc
449
- */
450
- crit(s) {
451
- const e = new c();
452
- e.level = o.Critical, e.message = s, this.logMessage(e);
397
+ _sendMessagesBatch(s) {
398
+ return new Promise((e) => {
399
+ const t = () => {
400
+ e(!1);
401
+ }, o = new XMLHttpRequest();
402
+ o.open(this._options.verb, this._options.endpoint), o.setRequestHeader("Content-Type", "application/json;charset=UTF-8"), this._options.requestTransform && this._options.requestTransform(o), o.onload = function() {
403
+ e(this.status >= 200 && this.status < 300);
404
+ }, o.onerror = t, o.onabort = t, o.send(JSON.stringify(s));
405
+ });
453
406
  }
407
+ }
408
+ class k {
409
+ _options;
410
+ _channel;
411
+ _eventListeners = null;
412
+ _broadcastEnabled = !0;
454
413
  /**
455
- * @inheritdoc
414
+ * Constructor.
415
+ *
416
+ * @param {Options} options The options.
456
417
  */
457
- log(s, e, t, n) {
458
- const a = new c();
459
- a.level = s, a.message = e, a.errorMessage = t == null ? void 0 : t.message, a.stackTrace = t == null ? void 0 : t.stack, a.extraParams = n, this.logMessage(a);
418
+ constructor(s) {
419
+ this._options = s, this._channel = new BroadcastChannel(this._options.channelName);
460
420
  }
461
- /**
462
- * @inheritdoc
463
- */
464
- logMessage(s) {
465
- this.isEnabled(s.level) && setTimeout(() => {
466
- this.logMessageCore(s);
467
- }, 1);
421
+ /** @inheritdoc */
422
+ init(s) {
423
+ this._eventListeners || (this._eventListeners = (e) => {
424
+ const t = e.data;
425
+ if (!(!t.topic || !t.message))
426
+ try {
427
+ this._broadcastEnabled = !1, s.publish(t.topic, t.message);
428
+ } finally {
429
+ this._broadcastEnabled = !0;
430
+ }
431
+ }, this._channel.addEventListener("message", this._eventListeners));
468
432
  }
469
- }
470
- class I {
471
- constructor() {
472
- /**
473
- * The name of the logger.
474
- */
475
- r(this, "name", "");
476
- /**
477
- * The reporter for the messages.
478
- */
479
- r(this, "reporter", null);
480
- /**
481
- * The minimum log level.
482
- */
483
- r(this, "minimumLevel", o.Warning);
484
- /**
485
- * Log enrichers.
486
- */
487
- r(this, "enrichers", []);
433
+ /** @inheritdoc */
434
+ onPublish(s) {
435
+ if (!this._broadcastEnabled || !s.topic || !s.message)
436
+ return;
437
+ const e = {
438
+ topic: s.topic,
439
+ message: s.message
440
+ };
441
+ this._channel.postMessage(e);
488
442
  }
489
- /**
490
- * Get the LogLevel from a string value.
491
- *
492
- * @param {String} level The log level as string.
493
- */
494
- static getLevel(s) {
495
- switch ((s || "").toUpperCase()) {
496
- case "TRACE":
497
- return o.Trace;
498
- case "DEBUG":
499
- return o.Debug;
500
- case "INFORMATION":
501
- return o.Information;
502
- case "WARNING":
503
- return o.Warning;
504
- case "ERROR":
505
- return o.Error;
506
- case "CRITICAL":
507
- return o.Critical;
508
- case "NONE":
509
- return o.None;
510
- default:
511
- return o.None;
512
- }
443
+ [Symbol.dispose]() {
444
+ this._eventListeners && this._channel.removeEventListener("message", this._eventListeners), this._channel.close();
513
445
  }
514
446
  }
515
- class N {
447
+ class M {
448
+ _logger;
449
+ _logLevel;
516
450
  /**
517
451
  * Constructor.
518
452
  *
519
453
  * @param {Logger} logger The logger.
520
454
  * @param {LogLevel} logLevel The log level.
521
455
  */
522
- constructor(s, e = o.Information) {
523
- r(this, "_logger");
524
- r(this, "_logLevel");
456
+ constructor(s, e = r.Information) {
525
457
  this._logger = s, this._logLevel = e;
526
458
  }
527
459
  /** @inheritdoc */
@@ -532,31 +464,107 @@ class N {
532
464
  });
533
465
  }
534
466
  }
535
- function b(i = 1, s) {
467
+ const l = (i, s) => {
468
+ clearTimeout(s.ref), s.ref = void 0;
469
+ const e = i.timeouts.indexOf(s);
470
+ i.timeouts.splice(e, 1);
471
+ };
472
+ class x {
473
+ _subscriptions = /* @__PURE__ */ new Map();
474
+ _options;
475
+ /**
476
+ *
477
+ */
478
+ constructor(s) {
479
+ if (this._options = s, this._options?.plugins)
480
+ for (const e of this._options.plugins)
481
+ e.init && e.init(this);
482
+ }
483
+ /** @inheritdoc */
484
+ publish(s, e) {
485
+ const t = {
486
+ topic: s,
487
+ message: e
488
+ };
489
+ if (this._options?.plugins)
490
+ for (const n of this._options.plugins)
491
+ n.onPublish && n.onPublish(t);
492
+ if (!t.topic)
493
+ throw new Error("Invalid topic.");
494
+ if (!t.message)
495
+ throw new Error("Invalid message.");
496
+ const o = this._subscriptions.get(t.topic);
497
+ if (o)
498
+ for (const n of o.values()) {
499
+ const c = {};
500
+ n.timeouts.push(c), c.ref = setTimeout((u, h, g, p) => {
501
+ u.handler(g, p), l(n, h);
502
+ }, 0, n, c, structuredClone(t.topic), structuredClone(t.message));
503
+ }
504
+ }
505
+ /** @inheritdoc */
506
+ subscribe(s, e) {
507
+ if (!s)
508
+ throw new Error("Invalid topic.");
509
+ if (!e)
510
+ throw new Error("Invalid handler.");
511
+ let t = this._subscriptions.get(s);
512
+ t || (t = /* @__PURE__ */ new Map(), this._subscriptions.set(structuredClone(s), t));
513
+ const o = `sub-${Date.now()}`;
514
+ return t.set(o, { handler: e, timeouts: [] }), o;
515
+ }
516
+ /** @inheritdoc */
517
+ unsubscribe(s) {
518
+ if (s)
519
+ for (const e of this._subscriptions.values()) {
520
+ for (const t of e.values())
521
+ for (; ; ) {
522
+ const o = t.timeouts.pop();
523
+ if (!o)
524
+ break;
525
+ l(t, o);
526
+ }
527
+ if (e.delete(s))
528
+ return;
529
+ }
530
+ }
531
+ [Symbol.dispose]() {
532
+ for (const s of this._subscriptions.values())
533
+ for (const e of s.keys())
534
+ this.unsubscribe(e);
535
+ if (this._subscriptions.clear(), this._options?.plugins)
536
+ for (const s of this._options.plugins) {
537
+ const e = s[Symbol.dispose];
538
+ e && e.call(s);
539
+ }
540
+ return Promise.resolve();
541
+ }
542
+ }
543
+ function _(i = 1, s) {
536
544
  return new Promise((e, t) => {
537
545
  setTimeout(() => {
538
546
  s ? t(s) : e();
539
547
  }, i);
540
548
  });
541
549
  }
542
- function v(i = 1) {
543
- return i <= 0 ? Promise.resolve() : b(0).then(() => v(i - 1));
550
+ function m(i = 1) {
551
+ return i <= 0 ? Promise.resolve() : _(0).then(() => m(i - 1));
544
552
  }
545
553
  export {
546
- P as BroadcastChannelPlugin,
547
- M as ConsoleReporter,
548
- E as DynamicValuesEnricher,
549
- k as InMemoryReporter,
550
- o as LogLevel,
551
- c as LogMessage,
552
- C as Logger,
553
- I as LoggerOptions,
554
- N as LoggerPlugin,
555
- A as MultipleReporter,
556
- y as PubSubHub,
557
- T as ValuesEnricher,
558
- R as XhrReporter,
559
- x as XhrReporterOptions,
560
- b as delay,
561
- v as nextTicks
554
+ k as BroadcastChannelPlugin,
555
+ y as ConsoleReporter,
556
+ f as DynamicValuesEnricher,
557
+ w as InMemoryReporter,
558
+ r as LogLevel,
559
+ a as LogMessage,
560
+ b as Logger,
561
+ v as LoggerOptions,
562
+ M as LoggerPlugin,
563
+ T as MultipleReporter,
564
+ x as PubSubHub,
565
+ d as ValuesEnricher,
566
+ E as XhrReporter,
567
+ P as XhrReporterOptions,
568
+ _ as delay,
569
+ m as nextTicks
562
570
  };