@zyrab/domo 1.5.0 → 1.5.1

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zyrab/domo",
3
- "version": "1.5.0",
3
+ "version": "1.5.1",
4
4
  "description": "Minimalist DOM builder and chaining-friendly micro-framework with router support.",
5
5
  "main": "./src/index.js",
6
6
  "type": "module",
@@ -47,7 +47,9 @@ class PropertiesClient extends BaseClient {
47
47
  if (typeof force === "boolean") {
48
48
  force ? this.element.setAttribute(attrName, "") : this.element.removeAttribute(attrName);
49
49
  } else {
50
- this.element.hasAttribute(attrName) ? this.element.removeAttribute(attrName) : this.element.setAttribute(attrName, "");
50
+ this.element.hasAttribute(attrName)
51
+ ? this.element.removeAttribute(attrName)
52
+ : this.element.setAttribute(attrName, "");
51
53
  }
52
54
  return this;
53
55
  }
@@ -41,7 +41,7 @@ class BaseServer {
41
41
  */
42
42
  island(component, enabled = true) {
43
43
  this.element._island = enabled;
44
- this._getOrSetId();
44
+ this._getOrSetId(component.name);
45
45
 
46
46
  if (enabled) {
47
47
  this.element.__island = component;
@@ -65,26 +65,33 @@ class BaseServer {
65
65
  });
66
66
 
67
67
  // Ensure element has a stable ID if it has a ref
68
- this._getOrSetId();
68
+ this._getOrSetId(callback);
69
69
  }
70
70
  return this;
71
71
  }
72
-
73
72
  /**
74
73
  * Generates or retrieves a stable, hash-based ID for metadata association.
75
74
  * @protected
76
75
  * @returns {string} The data-domo-id.
77
76
  */
78
- _getOrSetId() {
77
+ _getOrSetId(handler) {
79
78
  const existing = this.element._attr["data-domo-id"];
80
- if (existing) {
81
- return existing;
82
- }
83
- const hash = Math.random().toString(36).substring(2, 7);
79
+ if (existing) return existing;
80
+
81
+ let funcName = handler.name || handler.toString();
82
+ const hash = this._fastHash(funcName);
84
83
  const id = `d-${hash}`;
85
84
  this.element._attr["data-domo-id"] = id;
86
85
  return id;
87
86
  }
87
+
88
+ _fastHash(str) {
89
+ let hash = 5381;
90
+ for (let i = 0; i < str.length; i++) {
91
+ hash = (hash * 33) ^ str.charCodeAt(i);
92
+ }
93
+ return (hash >>> 0).toString(36);
94
+ }
88
95
  }
89
96
 
90
97
  export default BaseServer;
@@ -12,40 +12,35 @@ class EventsServer extends ClassesServer {
12
12
  * @param {object} [meta={}] - Injected by plugin: { _source, _name, ssg: boolean }
13
13
  */
14
14
  on(eventMapOrName, callback, meta = {}) {
15
- // If user passed options (meta), check if ssg is explicitly disabled
16
15
  if (meta.ssg === false) return this;
17
16
 
18
- const id = this._getOrSetId();
19
-
20
17
  if (typeof eventMapOrName === "object" && eventMapOrName !== null) {
21
18
  // In object mode: .on({ click: fn }, { _source: '...', _name: '...' })
22
19
  // callback actually acts as the 'meta' argument here
23
20
  const metadata = callback || {};
24
21
  for (const [event, value] of Object.entries(eventMapOrName)) {
25
22
  const handler = Array.isArray(value) ? value[0] : value;
26
- this._pushEvent(id, "direct", event, handler, null, metadata);
23
+ this._pushEvent("direct", event, handler, null, metadata);
27
24
  }
28
25
  } else if (typeof callback === "function") {
29
26
  // In single mode: .on('click', fn, { _source: '...', _name: '...' })
30
- this._pushEvent(id, "direct", eventMapOrName, callback, null, meta);
27
+ this._pushEvent("direct", eventMapOrName, callback, null, meta);
31
28
  }
32
29
  return this;
33
30
  }
34
31
 
35
32
  onClosest(event, selectors = {}, meta = {}) {
36
33
  if (meta.ssg === false) return this;
37
- const id = this._getOrSetId();
38
34
  for (const [selector, handler] of Object.entries(selectors)) {
39
- this._pushEvent(id, "closest", event, handler, selector, meta);
35
+ this._pushEvent("closest", event, handler, selector, meta);
40
36
  }
41
37
  return this;
42
38
  }
43
39
 
44
40
  onMatch(event, selectors = {}, meta = {}) {
45
41
  if (meta.ssg === false) return this;
46
- const id = this._getOrSetId();
47
42
  for (const [selector, handler] of Object.entries(selectors)) {
48
- this._pushEvent(id, "match", event, handler, selector, meta);
43
+ this._pushEvent("match", event, handler, selector, meta);
49
44
  }
50
45
  return this;
51
46
  }
@@ -54,25 +49,22 @@ class EventsServer extends ClassesServer {
54
49
  * Internal helper to push event metadata.
55
50
  * @private
56
51
  */
57
- _pushEvent(id, type, event, handler, selector = null, meta = {}) {
52
+ _pushEvent(type, event, handler, selector = null, meta = {}) {
58
53
  const evArr = (this.element._events ??= []);
54
+ const id = this._getOrSetId(handler);
59
55
  let existing = evArr.find((e) => e.id === id && e.event === event);
60
56
 
61
57
  if (!existing) {
62
58
  existing = { id, event, handlers: [] };
63
59
  evArr.push(existing);
64
60
  }
65
-
66
- // Capture path and name from the 'meta' object stamped by the plugin
61
+ console.log("testing", existing);
67
62
  existing.handlers.push({
68
63
  type,
69
64
  name: meta._name || handler.name || "anonymous",
70
65
  handler,
71
- path: meta._source || null, // STAMPED PATH
72
66
  ...(selector && { selector }),
73
67
  });
74
-
75
- // Tag as island so SSG knows to include domo.client.js
76
68
  }
77
69
  }
78
70