@gesslar/toolkit 3.33.0 → 3.34.0

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/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "name": "gesslar",
6
6
  "url": "https://gesslar.dev"
7
7
  },
8
- "version": "3.33.0",
8
+ "version": "3.34.0",
9
9
  "license": "Unlicense",
10
10
  "homepage": "https://github.com/gesslar/toolkit#readme",
11
11
  "repository": {
@@ -1,7 +1,7 @@
1
1
  /**
2
- * Thin wrapper around `window` event handling to centralize emit/on/off
3
- * helpers. Used to dispatch simple CustomEvents and manage listeners in one
4
- * place.
2
+ * Thin wrapper around event dispatching to centralize emit/on/off helpers.
3
+ * Uses `globalThis` for safe resolution in server-side build environments
4
+ * (e.g. esm.sh) while defaulting to `window` at runtime.
5
5
  */
6
6
 
7
7
  /**
@@ -14,6 +14,15 @@ export class Notify {
14
14
  /** @type {string} Display name for debugging. */
15
15
  name = "Notify"
16
16
 
17
+ /**
18
+ * Returns the default event target (window or globalThis).
19
+ *
20
+ * @returns {EventTarget} The default event target.
21
+ */
22
+ get #target() {
23
+ return globalThis.window ?? globalThis
24
+ }
25
+
17
26
  /**
18
27
  * Emits a CustomEvent without expecting a return value.
19
28
  *
@@ -24,7 +33,7 @@ export class Notify {
24
33
  */
25
34
  emit(type, payload=undefined, options=undefined) {
26
35
  const evt = new CustomEvent(type, this.#buildEventInit(payload, options))
27
- window.dispatchEvent(evt)
36
+ this.#target.dispatchEvent(evt)
28
37
  }
29
38
 
30
39
  /**
@@ -37,46 +46,55 @@ export class Notify {
37
46
  */
38
47
  request(type, payload={}, options=undefined) {
39
48
  const evt = new CustomEvent(type, this.#buildEventInit(payload, options))
40
- window.dispatchEvent(evt)
49
+ this.#target.dispatchEvent(evt)
41
50
 
42
51
  return evt.detail
43
52
  }
44
53
 
45
54
  /**
46
- * Registers a listener for the given event type on an HTMLElement (or
47
- * window, if not specified).
55
+ * Registers a listener for the given event type on an EventTarget.
56
+ * Defaults to window when no element is provided.
48
57
  *
49
58
  * @param {string} type - Event name to listen for.
50
- * @param {(evt: Notify) => void} handler - Listener callback.
51
- * @param {HTMLElement | Window} [element] - The object to which to attach the handler. Default is window.
59
+ * @param {(evt: Event) => void} handler - Listener callback.
60
+ * @param {EventTarget} [element] - The target to attach the handler to. Defaults to window.
52
61
  * @param {boolean | object} [options] - Options to pass to addEventListener.
53
62
  * @returns {() => void} Dispose function to unregister the handler.
54
63
  */
55
- on(type, handler, element=window, options=undefined) {
64
+ on(type, handler, element=undefined, options=undefined) {
56
65
  if(!(typeof type === "string" && type))
57
66
  throw new Error("No event 'type' specified to listen for.")
58
67
 
59
68
  if(typeof handler !== "function")
60
69
  throw new Error("No handler function specified.")
61
70
 
62
- element.addEventListener(type, handler, options)
71
+ const target = element ?? this.#target
72
+ target.addEventListener(type, handler, options)
63
73
 
64
- return () => this.off(type, handler, element, options)
74
+ return () => this.off(type, handler, target, options)
65
75
  }
66
76
 
67
77
  /**
68
78
  * Removes a previously registered listener for the given event type.
69
79
  *
70
80
  * @param {string} type - Event name to remove.
71
- * @param {(evt: Notify) => void} handler - Listener callback to detach.
72
- * @param {HTMLElement | Window} [element] - The object from which to remove the handler. Default is window.
81
+ * @param {(evt: Event) => void} handler - Listener callback to detach.
82
+ * @param {EventTarget} [element] - The target to remove the handler from. Defaults to window.
73
83
  * @param {boolean | object} [options] - Options to pass to removeEventListener.
74
84
  * @returns {void}
75
85
  */
76
- off(type, handler, element=window, options=undefined) {
77
- element.removeEventListener(type, handler, options)
86
+ off(type, handler, element=undefined, options=undefined) {
87
+ const target = element ?? this.#target
88
+ target.removeEventListener(type, handler, options)
78
89
  }
79
90
 
91
+ /**
92
+ * Builds the CustomEvent init object from detail and options.
93
+ *
94
+ * @param {unknown} detail - The event detail payload.
95
+ * @param {boolean | NotifyEventOptions} [options] - Event options.
96
+ * @returns {object} The event init object.
97
+ */
80
98
  #buildEventInit(detail, options) {
81
99
  if(typeof options === "boolean")
82
100
  return {detail, bubbles: options}
@@ -23,8 +23,7 @@ export default class Valid {
23
23
  static type(value, type, options) {
24
24
  Valid.assert(
25
25
  Data.isType(value, type, options),
26
- `Invalid type. Expected ${type}, got ${JSON.stringify(value)}`,
27
- 1,
26
+ `Invalid type. Expected ${type}, got ${Data.typeOf(value)}`
28
27
  )
29
28
  }
30
29
 
@@ -1,7 +1,7 @@
1
1
  /**
2
- * Thin wrapper around `window` event handling to centralize emit/on/off
3
- * helpers. Used to dispatch simple CustomEvents and manage listeners in one
4
- * place.
2
+ * Thin wrapper around event dispatching to centralize emit/on/off helpers.
3
+ * Uses `globalThis` for safe resolution in server-side build environments
4
+ * (e.g. esm.sh) while defaulting to `window` at runtime.
5
5
  */
6
6
  /**
7
7
  * @typedef {object} NotifyEventOptions
@@ -31,26 +31,26 @@ export class Notify {
31
31
  */
32
32
  request(type: string, payload?: unknown, options?: boolean | NotifyEventOptions): unknown;
33
33
  /**
34
- * Registers a listener for the given event type on an HTMLElement (or
35
- * window, if not specified).
34
+ * Registers a listener for the given event type on an EventTarget.
35
+ * Defaults to window when no element is provided.
36
36
  *
37
37
  * @param {string} type - Event name to listen for.
38
- * @param {(evt: Notify) => void} handler - Listener callback.
39
- * @param {HTMLElement | Window} [element] - The object to which to attach the handler. Default is window.
38
+ * @param {(evt: Event) => void} handler - Listener callback.
39
+ * @param {EventTarget} [element] - The target to attach the handler to. Defaults to window.
40
40
  * @param {boolean | object} [options] - Options to pass to addEventListener.
41
41
  * @returns {() => void} Dispose function to unregister the handler.
42
42
  */
43
- on(type: string, handler: (evt: Notify) => void, element?: HTMLElement | Window, options?: boolean | object): () => void;
43
+ on(type: string, handler: (evt: Event) => void, element?: EventTarget, options?: boolean | object): () => void;
44
44
  /**
45
45
  * Removes a previously registered listener for the given event type.
46
46
  *
47
47
  * @param {string} type - Event name to remove.
48
- * @param {(evt: Notify) => void} handler - Listener callback to detach.
49
- * @param {HTMLElement | Window} [element] - The object from which to remove the handler. Default is window.
48
+ * @param {(evt: Event) => void} handler - Listener callback to detach.
49
+ * @param {EventTarget} [element] - The target to remove the handler from. Defaults to window.
50
50
  * @param {boolean | object} [options] - Options to pass to removeEventListener.
51
51
  * @returns {void}
52
52
  */
53
- off(type: string, handler: (evt: Notify) => void, element?: HTMLElement | Window, options?: boolean | object): void;
53
+ off(type: string, handler: (evt: Event) => void, element?: EventTarget, options?: boolean | object): void;
54
54
  #private;
55
55
  }
56
56
  declare const _default: Notify;
@@ -1 +1 @@
1
- {"version":3,"file":"Notify.d.ts","sourceRoot":"","sources":["../../../src/browser/lib/Notify.js"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;;GAKG;AACH;IACE,iDAAiD;IACjD,MADW,MAAM,CACF;IAEf;;;;;;;OAOG;IACH,WALW,MAAM,YACN,OAAO,YACP,OAAO,GAAG,kBAAkB,GAC1B,IAAI,CAKhB;IAED;;;;;;;OAOG;IACH,cALW,MAAM,YACN,OAAO,YACP,OAAO,GAAG,kBAAkB,GAC1B,OAAO,CAOnB;IAED;;;;;;;;;OASG;IACH,SANW,MAAM,WACN,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,YACrB,WAAW,GAAG,MAAM,YACpB,OAAO,GAAG,MAAM,GACd,MAAM,IAAI,CAYtB;IAED;;;;;;;;OAQG;IACH,UANW,MAAM,WACN,CAAC,GAAG,EAAE,MAAM,KAAK,IAAI,YACrB,WAAW,GAAG,MAAM,YACpB,OAAO,GAAG,MAAM,GACd,IAAI,CAIhB;;CAWF;;;;;;;cAhFa,OAAO;;;;iBACP,OAAO;;;;eACP,OAAO"}
1
+ {"version":3,"file":"Notify.d.ts","sourceRoot":"","sources":["../../../src/browser/lib/Notify.js"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;;GAKG;AACH;IACE,iDAAiD;IACjD,MADW,MAAM,CACF;IAWf;;;;;;;OAOG;IACH,WALW,MAAM,YACN,OAAO,YACP,OAAO,GAAG,kBAAkB,GAC1B,IAAI,CAKhB;IAED;;;;;;;OAOG;IACH,cALW,MAAM,YACN,OAAO,YACP,OAAO,GAAG,kBAAkB,GAC1B,OAAO,CAOnB;IAED;;;;;;;;;OASG;IACH,SANW,MAAM,WACN,CAAC,GAAG,EAAE,KAAK,KAAK,IAAI,YACpB,WAAW,YACX,OAAO,GAAG,MAAM,GACd,MAAM,IAAI,CAatB;IAED;;;;;;;;OAQG;IACH,UANW,MAAM,WACN,CAAC,GAAG,EAAE,KAAK,KAAK,IAAI,YACpB,WAAW,YACX,OAAO,GAAG,MAAM,GACd,IAAI,CAKhB;;CAkBF;;;;;;;cAlGa,OAAO;;;;iBACP,OAAO;;;;eACP,OAAO"}
@@ -1 +1 @@
1
- {"version":3,"file":"Valid.d.ts","sourceRoot":"","sources":["../../../src/browser/lib/Valid.js"],"names":[],"mappings":"AAWA;;GAEG;AACH;IACE;;;;;;OAMG;IACH,mBAJW,OAAO,QACP,MAAM,YACN,MAAM,QAQhB;IAED;;;;;;;;OAQG;IACH,yBANW,OAAO,WACP,MAAM,QAEN,MAAM,QAkBhB;IAED,+CAAmE;IAEnE;;;;;;OAMG;IACH,0CAHW,KAAK,CAAC,MAAM,CAAC,QAYvB;CACF"}
1
+ {"version":3,"file":"Valid.d.ts","sourceRoot":"","sources":["../../../src/browser/lib/Valid.js"],"names":[],"mappings":"AAWA;;GAEG;AACH;IACE;;;;;;OAMG;IACH,mBAJW,OAAO,QACP,MAAM,YACN,MAAM,QAOhB;IAED;;;;;;;;OAQG;IACH,yBANW,OAAO,WACP,MAAM,QAEN,MAAM,QAkBhB;IAED,+CAAmE;IAEnE;;;;;;OAMG;IACH,0CAHW,KAAK,CAAC,MAAM,CAAC,QAYvB;CACF"}