@tb-dev/prototype-dom 6.0.1 → 6.1.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/dist/index.d.ts CHANGED
@@ -7,6 +7,7 @@ declare global {
7
7
  queryAsArray: <T = Element>(selector: string, valueSelector?: (element: Element) => T) => T[];
8
8
  queryAsSet: <T = Element>(selector: string, valueSelector?: (element: Element) => T) => Set<T>;
9
9
  queryAsMap: <T extends Element, K, V = T>(selector: string, keySelector: (element: T) => K, valueSelector?: (element: T) => V) => Map<K, V>;
10
+ waitChild: (selector: string, timeout?: number) => Promise<Element>;
10
11
  }
11
12
  interface Element {
12
13
  getAttributeStrict: <T extends string>(qualifiedName: string) => T;
@@ -19,6 +20,7 @@ declare global {
19
20
  queryAsSet: <T = Element>(selector: string, valueSelector?: (element: Element) => T) => Set<T>;
20
21
  queryStrict: <T extends Element>(selector: string) => T;
21
22
  queryAsMap: <T extends Element, K, V = T>(selector: string, keySelector: (element: T) => K, valueSelector?: (element: T) => V) => Map<K, V>;
23
+ waitChild: (selector: string, timeout?: number) => Promise<Element>;
22
24
  }
23
25
  interface URLSearchParams {
24
26
  getStrict: <T extends string>(name: string) => T;
package/dist/index.js CHANGED
@@ -27,6 +27,55 @@
27
27
  }
28
28
  return map;
29
29
  };
30
+ Document.prototype.waitChild = function (selector, timeoutMillis = 300_000) {
31
+ const el = this.querySelector(selector);
32
+ if (el)
33
+ return Promise.resolve(el);
34
+ return new Promise((resolve, reject) => {
35
+ let timeout = null;
36
+ let interval = null;
37
+ const observer = new MutationObserver((mutations) => {
38
+ const element = this.querySelector(selector);
39
+ if (element) {
40
+ onElementFound(element);
41
+ return;
42
+ }
43
+ for (const mutation of mutations) {
44
+ if (mutation.type !== 'childList')
45
+ continue;
46
+ for (const node of Array.from(mutation.addedNodes)) {
47
+ if (node instanceof Element && node.matches(`:has(${selector})`)) {
48
+ onElementFound(node);
49
+ return;
50
+ }
51
+ }
52
+ }
53
+ });
54
+ function onElementFound(element) {
55
+ if (typeof timeout === 'number')
56
+ clearTimeout(timeout);
57
+ if (typeof interval === 'number')
58
+ clearInterval(interval);
59
+ observer.disconnect();
60
+ resolve(element);
61
+ }
62
+ function onInterval() {
63
+ const element = this.querySelector(selector);
64
+ if (element) {
65
+ onElementFound(element);
66
+ }
67
+ }
68
+ function onTimeout() {
69
+ if (typeof interval === 'number')
70
+ clearInterval(interval);
71
+ observer.disconnect();
72
+ reject(new Error(`timeout waiting for element: ${selector}`));
73
+ }
74
+ interval = setInterval(onInterval.bind(this), 100);
75
+ timeout = setTimeout(onTimeout, timeoutMillis);
76
+ observer.observe(this, { childList: true, subtree: true });
77
+ });
78
+ };
30
79
  Element.prototype.getAttributeStrict = function (attribute) {
31
80
  const value = this.getAttribute(attribute)?.trim();
32
81
  if (typeof value !== 'string' || value.length === 0) {
@@ -98,6 +147,55 @@
98
147
  }
99
148
  return map;
100
149
  };
150
+ Element.prototype.waitChild = function (selector, timeoutMillis = 300_000) {
151
+ const el = this.querySelector(selector);
152
+ if (el)
153
+ return Promise.resolve(el);
154
+ return new Promise((resolve, reject) => {
155
+ let timeout = null;
156
+ let interval = null;
157
+ const observer = new MutationObserver((mutations) => {
158
+ const element = this.querySelector(selector);
159
+ if (element) {
160
+ onElementFound(element);
161
+ return;
162
+ }
163
+ for (const mutation of mutations) {
164
+ if (mutation.type !== 'childList')
165
+ continue;
166
+ for (const node of Array.from(mutation.addedNodes)) {
167
+ if (node instanceof Element && node.matches(`:has(${selector})`)) {
168
+ onElementFound(node);
169
+ return;
170
+ }
171
+ }
172
+ }
173
+ });
174
+ function onElementFound(element) {
175
+ if (typeof timeout === 'number')
176
+ clearTimeout(timeout);
177
+ if (typeof interval === 'number')
178
+ clearInterval(interval);
179
+ observer.disconnect();
180
+ resolve(element);
181
+ }
182
+ function onInterval() {
183
+ const element = this.querySelector(selector);
184
+ if (element) {
185
+ onElementFound(element);
186
+ }
187
+ }
188
+ function onTimeout() {
189
+ if (typeof interval === 'number')
190
+ clearInterval(interval);
191
+ observer.disconnect();
192
+ reject(new Error(`timeout waiting for element: ${selector}`));
193
+ }
194
+ interval = setInterval(onInterval.bind(this), 100);
195
+ timeout = setTimeout(onTimeout, timeoutMillis);
196
+ observer.observe(this, { childList: true, subtree: true });
197
+ });
198
+ };
101
199
  URLSearchParams.prototype.getStrict = function (name) {
102
200
  const item = this.get(name);
103
201
  if (item === null) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tb-dev/prototype-dom",
3
- "version": "6.0.1",
3
+ "version": "6.1.0",
4
4
  "description": "Adds prototype methods to DOM objects",
5
5
  "packageManager": "pnpm@8.15.4",
6
6
  "license": "MIT",
@@ -25,7 +25,6 @@
25
25
  "devDependencies": {
26
26
  "@rollup/plugin-typescript": "^11.1.6",
27
27
  "@tb-dev/eslint-config": "^2.0.3",
28
- "@types/node": "^20.11.20",
29
28
  "@vitest/ui": "^1.3.1",
30
29
  "eslint": "^8.57.0",
31
30
  "husky": "^9.0.11",