@trunkjs/browser-utils 1.0.13 → 1.0.15

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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## 1.0.15 (2025-08-24)
2
+
3
+ This was a version bump only for browser-utils to align it with other projects, there were no code changes.
4
+
5
+ ## 1.0.14 (2025-08-24)
6
+
7
+ This was a version bump only for browser-utils to align it with other projects, there were no code changes.
8
+
1
9
  ## 1.0.13 (2025-08-13)
2
10
 
3
11
  This was a version bump only for browser-utils to align it with other projects, there were no code changes.
package/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from './lib/create-element';
2
2
  export * from './lib/Debouncer';
3
3
  export * from './lib/get-error-location';
4
+ export * from './lib/Logger';
4
5
  export * from './lib/Stopwatch';
5
6
  export * from './lib/wait-for';
6
7
  export * from './mixins/LoggingMixin';
package/index.js CHANGED
@@ -1,78 +1,96 @@
1
- var h = Object.defineProperty;
2
- var c = (e) => {
3
- throw TypeError(e);
1
+ var g = Object.defineProperty;
2
+ var c = (t) => {
3
+ throw TypeError(t);
4
4
  };
5
- var f = (e, t, n) => t in e ? h(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
6
- var a = (e, t, n) => f(e, typeof t != "symbol" ? t + "" : t, n), d = (e, t, n) => t.has(e) || c("Cannot " + n);
7
- var o = (e, t, n) => (d(e, t, "read from private field"), n ? n.call(e) : t.get(e)), l = (e, t, n) => t.has(e) ? c("Cannot add the same private member more than once") : t instanceof WeakSet ? t.add(e) : t.set(e, n), m = (e, t, n, i) => (d(e, t, "write to private field"), i ? i.call(e, n) : t.set(e, n), n);
8
- function w(e, t = {}, n = []) {
5
+ var f = (t, e, n) => e in t ? g(t, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : t[e] = n;
6
+ var l = (t, e, n) => f(t, typeof e != "symbol" ? e + "" : e, n), d = (t, e, n) => e.has(t) || c("Cannot " + n);
7
+ var r = (t, e, n) => (d(t, e, "read from private field"), n ? n.call(t) : e.get(t)), u = (t, e, n) => e.has(t) ? c("Cannot add the same private member more than once") : e instanceof WeakSet ? e.add(t) : e.set(t, n), m = (t, e, n, i) => (d(t, e, "write to private field"), i ? i.call(t, n) : e.set(t, n), n);
8
+ function E(t, e = {}, n = []) {
9
9
  Array.isArray(n) || (n = [n]);
10
- const i = document.createElement(e);
11
- for (const s in t)
12
- t[s] !== null && t[s] !== void 0 && i.setAttribute(s, t[s] !== !0 ? t[s] : "");
10
+ const i = document.createElement(t);
11
+ for (const s in e)
12
+ e[s] !== null && e[s] !== void 0 && i.setAttribute(s, e[s] !== !0 ? e[s] : "");
13
13
  for (const s of n)
14
14
  i.append(typeof s == "string" ? document.createTextNode(s) : s);
15
15
  return i;
16
16
  }
17
- class L {
17
+ class y {
18
18
  /**
19
19
  *
20
20
  * @param delay Debounce delay in milliseconds
21
21
  * @param max_delay Maximum delay in milliseconds, if false then no maximum delay is applied
22
22
  */
23
- constructor(t, n = !1) {
24
- a(this, "timeout", null);
25
- a(this, "startTimeWithMs", 0);
26
- this.delay = t, this.max_delay = n;
23
+ constructor(e, n = !1) {
24
+ l(this, "timeout", null);
25
+ l(this, "startTimeWithMs", 0);
26
+ this.delay = e, this.max_delay = n;
27
27
  }
28
28
  async wait() {
29
- return this.startTimeWithMs === 0 && (this.startTimeWithMs = Date.now()), this.timeout && (this.max_delay === !1 || this.startTimeWithMs + this.max_delay > Date.now()) && clearTimeout(this.timeout), new Promise((t) => {
29
+ return this.startTimeWithMs === 0 && (this.startTimeWithMs = Date.now()), this.timeout && (this.max_delay === !1 || this.startTimeWithMs + this.max_delay > Date.now()) && clearTimeout(this.timeout), new Promise((e) => {
30
30
  this.timeout = setTimeout(() => {
31
- this.startTimeWithMs = 0, t(!0);
31
+ this.startTimeWithMs = 0, e(!0);
32
32
  }, this.delay);
33
33
  });
34
34
  }
35
- debounce(t) {
35
+ debounce(e) {
36
36
  this.timeout && clearTimeout(this.timeout), this.timeout = setTimeout(() => {
37
- t();
37
+ e();
38
38
  }, this.delay);
39
39
  }
40
40
  }
41
- function p(e) {
42
- if (typeof e.lineNumber == "number")
41
+ function I(t) {
42
+ if (typeof t.lineNumber == "number")
43
43
  return {
44
- file: e.fileName || e.sourceURL,
45
- line: e.lineNumber,
46
- column: e.columnNumber ?? void 0
44
+ file: t.fileName || t.sourceURL,
45
+ line: t.lineNumber,
46
+ column: t.columnNumber ?? void 0
47
47
  };
48
- if (typeof e.line == "number")
48
+ if (typeof t.line == "number")
49
49
  return {
50
- file: e.sourceURL,
51
- line: e.line,
52
- column: e.column
50
+ file: t.sourceURL,
51
+ line: t.line,
52
+ column: t.column
53
53
  };
54
- const n = String(e.stack || e.message || "").split(`
54
+ const n = String(t.stack || t.message || "").split(`
55
55
  `), i = /(.*?)(?:\(|@)?(.*?):(\d+):(\d+)\)?$/;
56
56
  for (const s of n) {
57
- const r = s.match(i);
58
- if (r)
59
- return { file: r[2], line: +r[3], column: +r[4] };
57
+ const a = s.match(i);
58
+ if (a)
59
+ return { file: a[2], line: +a[3], column: +a[4] };
60
60
  }
61
- return { file: e.fileName || e.sourceURL };
61
+ return { file: t.fileName || t.sourceURL };
62
62
  }
63
- class T {
64
- constructor(t, n = !0) {
65
- a(this, "label");
66
- a(this, "last");
67
- a(this, "startTime");
68
- a(this, "running", !1);
69
- a(this, "enabled");
70
- this.label = t, this.enabled = n, this.startTime = this.last = performance.now(), this.running = !0;
71
- }
72
- lap(t = "") {
63
+ class w {
64
+ constructor(e, n, i = "main") {
65
+ this._debug = e, this.myElementId = n, this.instanceId = i;
66
+ }
67
+ log(...e) {
68
+ this._debug && console.log(`[LOG][ID:${this.myElementId}:${this.instanceId}]`, ...e);
69
+ }
70
+ warn(...e) {
71
+ console.warn(`[WARN][ID:${this.myElementId}:${this.instanceId}]`, ...e);
72
+ }
73
+ error(...e) {
74
+ console.error(`[ERROR][ID:${this.myElementId}:${this.instanceId}]`, ...e);
75
+ }
76
+ throwError(...e) {
77
+ const n = `[ERROR][ID:${this.myElementId}:${this.instanceId}] ${e.join(" ")}`;
78
+ throw this.error(...e), new Error(n);
79
+ }
80
+ }
81
+ class p {
82
+ constructor(e, n = !0) {
83
+ l(this, "label");
84
+ l(this, "last");
85
+ l(this, "startTime");
86
+ l(this, "running", !1);
87
+ l(this, "enabled");
88
+ this.label = e, this.enabled = n, this.startTime = this.last = performance.now(), this.running = !0;
89
+ }
90
+ lap(e = "") {
73
91
  if (!this.enabled) return;
74
92
  const n = performance.now(), i = (n - this.last) / 1e3;
75
- this.last = n, console.debug(`[${this.label}] ${t} +${i.toFixed(3)}s`);
93
+ this.last = n, console.debug(`[${this.label}] ${e} +${i.toFixed(3)}s`);
76
94
  }
77
95
  elapsed() {
78
96
  return performance.now() - this.startTime;
@@ -90,43 +108,44 @@ class T {
90
108
  return this.running;
91
109
  }
92
110
  }
93
- function y(e, t, n) {
111
+ function T(t, e, n) {
94
112
  return new Promise((i, s) => {
95
- const r = (u) => {
96
- e.removeEventListener(t, r, n), i(u);
113
+ const a = (h) => {
114
+ t.removeEventListener(e, a, n), i(h);
97
115
  };
98
- e.addEventListener(t, r, n);
116
+ t.addEventListener(e, a, n);
99
117
  });
100
118
  }
101
- function E() {
102
- return document.readyState === "loading" ? new Promise((e) => {
103
- document.addEventListener("DOMContentLoaded", () => e());
119
+ function $() {
120
+ return document.readyState === "loading" ? new Promise((t) => {
121
+ document.addEventListener("DOMContentLoaded", () => t());
104
122
  }) : Promise.resolve();
105
123
  }
106
- function v() {
107
- return document.readyState === "complete" ? Promise.resolve() : new Promise((e) => {
108
- window.addEventListener("load", () => e());
124
+ function D() {
125
+ return document.readyState === "complete" ? Promise.resolve() : new Promise((t) => {
126
+ window.addEventListener("load", () => t());
109
127
  });
110
128
  }
111
- function D(e) {
112
- return new Promise((t) => setTimeout(t, e));
129
+ function v(t) {
130
+ return new Promise((e) => setTimeout(e, t));
113
131
  }
114
- function x(e) {
115
- return new Promise((t) => {
132
+ function R(t) {
133
+ return new Promise((e) => {
116
134
  const n = (i) => {
117
- e.removeEventListener("animationend", n), t(i);
135
+ t.removeEventListener("animationend", n), e(i);
118
136
  };
119
- e.addEventListener("animationend", n);
137
+ t.addEventListener("animationend", n);
120
138
  });
121
139
  }
122
- let g = 1;
123
- function M(e) {
124
- var n, i;
125
- class t extends e {
140
+ let b = 1;
141
+ function x(t) {
142
+ var n, i, s;
143
+ class e extends t {
126
144
  constructor() {
127
145
  super(...arguments);
128
- l(this, n, null);
129
- l(this, i, g++);
146
+ u(this, n, null);
147
+ u(this, i, b++);
148
+ u(this, s, null);
130
149
  }
131
150
  /**
132
151
  * Clears the cached debug flag so the attribute will be checked again
@@ -136,29 +155,36 @@ function M(e) {
136
155
  m(this, n, null);
137
156
  }
138
157
  get _debug() {
139
- return o(this, n) !== null ? o(this, n) : (this instanceof HTMLElement && m(this, n, this.hasAttribute("debug") && !["false", "0", "off", "no"].includes(this.getAttribute("debug") || "")), o(this, n) === !0 && console.log(`[DEBUG][ID:${o(this, i)}] LoggingMixin: Debug mode is enabled for <${this.tagName}>`, this), o(this, n));
158
+ return r(this, n) !== null ? r(this, n) : (this instanceof HTMLElement && m(this, n, this.hasAttribute("debug") && !["false", "0", "off", "no"].includes(this.getAttribute("debug") || "")), r(this, n) === !0 && console.log(`[DEBUG][ID:${r(this, i)}] LoggingMixin: Debug mode is enabled for <${this.tagName}>`, this), r(this, n) ?? !1);
159
+ }
160
+ getLogger(o = "main") {
161
+ return r(this, s) || m(this, s, new w(this._debug, `${r(this, i)}`, o)), r(this, s);
162
+ }
163
+ log(...o) {
164
+ this.getLogger().log(...o);
140
165
  }
141
- log(...u) {
142
- this._debug && console.log(`[LOG][ID:${o(this, i)}]`, ...u);
166
+ warn(...o) {
167
+ this.getLogger().warn(...o);
143
168
  }
144
- warn(...u) {
145
- console.warn(`[WARN][ID:${o(this, i)}]`, ...u);
169
+ error(...o) {
170
+ this.getLogger().error(...o);
146
171
  }
147
- error(...u) {
148
- console.error(`[ERROR][ID:${o(this, i)}]`, ...u);
172
+ throwError(...o) {
173
+ return this.getLogger().throwError(...o);
149
174
  }
150
175
  }
151
- return n = new WeakMap(), i = new WeakMap(), t;
176
+ return n = new WeakMap(), i = new WeakMap(), s = new WeakMap(), e;
152
177
  }
153
178
  export {
154
- L as Debouncer,
155
- M as LoggingMixin,
156
- T as Stopwatch,
157
- w as create_element,
158
- p as getErrorLocation,
159
- D as sleep,
160
- y as waitFor,
161
- x as waitForAnimationEnd,
162
- E as waitForDomContentLoaded,
163
- v as waitForLoad
179
+ y as Debouncer,
180
+ w as Logger,
181
+ x as LoggingMixin,
182
+ p as Stopwatch,
183
+ E as create_element,
184
+ I as getErrorLocation,
185
+ v as sleep,
186
+ T as waitFor,
187
+ R as waitForAnimationEnd,
188
+ $ as waitForDomContentLoaded,
189
+ D as waitForLoad
164
190
  };
@@ -0,0 +1,10 @@
1
+ export declare class Logger {
2
+ private _debug;
3
+ private myElementId;
4
+ private instanceId;
5
+ constructor(_debug: boolean, myElementId: string, instanceId?: string);
6
+ log(...args: any[]): void;
7
+ warn(...args: any[]): void;
8
+ error(...args: any[]): void;
9
+ throwError(...args: any[]): never;
10
+ }
@@ -1,3 +1,4 @@
1
+ import { Logger } from '../lib/Logger';
1
2
  type Constructor<T = object> = abstract new (...args: any[]) => T;
2
3
  /**
3
4
  * LoggingMixin
@@ -17,19 +18,27 @@ type Constructor<T = object> = abstract new (...args: any[]) => T;
17
18
  * // or with Lit:
18
19
  * class MyLitEl extends LoggingMixin(ReactiveElement) {}
19
20
  *
21
+ *
22
+ * // Create a new Logger Instance
23
+ * const logger = this.getLogger("loader1");
24
+ * logger.log("This is a log message from loader1");
25
+ *
20
26
  * <my-element debug></my-element> // enables debug logging
21
27
  */
22
28
  export declare function LoggingMixin<TBase extends Constructor<object>>(Base: TBase): (abstract new (...args: any[]) => {
23
- "__#1703@#debugCached": boolean | null;
24
- "__#1703@#myElementId": number;
29
+ "__#1729@#debugCached": boolean | null;
30
+ "__#1729@#myElementId": number;
25
31
  /**
26
32
  * Clears the cached debug flag so the attribute will be checked again
27
33
  * on the next log/warn/error call.
28
34
  */
29
35
  invalidateDebugCache(): void;
30
- readonly _debug: boolean | null;
36
+ "__#1729@#myLoggerInstance": Logger | null;
37
+ readonly _debug: boolean;
38
+ getLogger(instanceId?: string): Logger;
31
39
  log(...args: any[]): void;
32
40
  warn(...args: any[]): void;
33
41
  error(...args: any[]): void;
42
+ throwError(...args: any[]): never;
34
43
  }) & TBase;
35
44
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trunkjs/browser-utils",
3
- "version": "1.0.13",
3
+ "version": "1.0.15",
4
4
  "main": "./index.js",
5
5
  "dependencies": {},
6
6
  "type": "module",