@whitesev/domutils 1.5.4 → 1.5.5
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.amd.js +288 -4
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +288 -4
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +288 -4
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +288 -4
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +288 -4
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +288 -4
- package/dist/index.umd.js.map +1 -1
- package/dist/types/src/DOMUtilsCommonUtils.d.ts +16 -0
- package/package.json +1 -1
- package/src/DOMUtils.ts +3 -3
- package/src/DOMUtilsCommonUtils.ts +52 -0
- package/src/DOMUtilsEvent.ts +1 -1
package/dist/index.umd.js
CHANGED
|
@@ -47,6 +47,238 @@
|
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
|
|
50
|
+
const createCache = (lastNumberWeakMap) => {
|
|
51
|
+
return (collection, nextNumber) => {
|
|
52
|
+
lastNumberWeakMap.set(collection, nextNumber);
|
|
53
|
+
return nextNumber;
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
/*
|
|
58
|
+
* The value of the constant Number.MAX_SAFE_INTEGER equals (2 ** 53 - 1) but it
|
|
59
|
+
* is fairly new.
|
|
60
|
+
*/
|
|
61
|
+
const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER === undefined ? 9007199254740991 : Number.MAX_SAFE_INTEGER;
|
|
62
|
+
const TWO_TO_THE_POWER_OF_TWENTY_NINE = 536870912;
|
|
63
|
+
const TWO_TO_THE_POWER_OF_THIRTY = TWO_TO_THE_POWER_OF_TWENTY_NINE * 2;
|
|
64
|
+
const createGenerateUniqueNumber = (cache, lastNumberWeakMap) => {
|
|
65
|
+
return (collection) => {
|
|
66
|
+
const lastNumber = lastNumberWeakMap.get(collection);
|
|
67
|
+
/*
|
|
68
|
+
* Let's try the cheapest algorithm first. It might fail to produce a new
|
|
69
|
+
* number, but it is so cheap that it is okay to take the risk. Just
|
|
70
|
+
* increase the last number by one or reset it to 0 if we reached the upper
|
|
71
|
+
* bound of SMIs (which stands for small integers). When the last number is
|
|
72
|
+
* unknown it is assumed that the collection contains zero based consecutive
|
|
73
|
+
* numbers.
|
|
74
|
+
*/
|
|
75
|
+
let nextNumber = lastNumber === undefined ? collection.size : lastNumber < TWO_TO_THE_POWER_OF_THIRTY ? lastNumber + 1 : 0;
|
|
76
|
+
if (!collection.has(nextNumber)) {
|
|
77
|
+
return cache(collection, nextNumber);
|
|
78
|
+
}
|
|
79
|
+
/*
|
|
80
|
+
* If there are less than half of 2 ** 30 numbers stored in the collection,
|
|
81
|
+
* the chance to generate a new random number in the range from 0 to 2 ** 30
|
|
82
|
+
* is at least 50%. It's benifitial to use only SMIs because they perform
|
|
83
|
+
* much better in any environment based on V8.
|
|
84
|
+
*/
|
|
85
|
+
if (collection.size < TWO_TO_THE_POWER_OF_TWENTY_NINE) {
|
|
86
|
+
while (collection.has(nextNumber)) {
|
|
87
|
+
nextNumber = Math.floor(Math.random() * TWO_TO_THE_POWER_OF_THIRTY);
|
|
88
|
+
}
|
|
89
|
+
return cache(collection, nextNumber);
|
|
90
|
+
}
|
|
91
|
+
// Quickly check if there is a theoretical chance to generate a new number.
|
|
92
|
+
if (collection.size > MAX_SAFE_INTEGER) {
|
|
93
|
+
throw new Error('Congratulations, you created a collection of unique numbers which uses all available integers!');
|
|
94
|
+
}
|
|
95
|
+
// Otherwise use the full scale of safely usable integers.
|
|
96
|
+
while (collection.has(nextNumber)) {
|
|
97
|
+
nextNumber = Math.floor(Math.random() * MAX_SAFE_INTEGER);
|
|
98
|
+
}
|
|
99
|
+
return cache(collection, nextNumber);
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const LAST_NUMBER_WEAK_MAP = new WeakMap();
|
|
104
|
+
const cache = createCache(LAST_NUMBER_WEAK_MAP);
|
|
105
|
+
const generateUniqueNumber = createGenerateUniqueNumber(cache, LAST_NUMBER_WEAK_MAP);
|
|
106
|
+
|
|
107
|
+
const isCallNotification = (message) => {
|
|
108
|
+
return message.method !== undefined && message.method === 'call';
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
const isClearResponse = (message) => {
|
|
112
|
+
return typeof message.id === 'number' && typeof message.result === 'boolean';
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const load = (url) => {
|
|
116
|
+
// Prefilling the Maps with a function indexed by zero is necessary to be compliant with the specification.
|
|
117
|
+
const scheduledIntervalFunctions = new Map([[0, () => { }]]); // tslint:disable-line no-empty
|
|
118
|
+
const scheduledTimeoutFunctions = new Map([[0, () => { }]]); // tslint:disable-line no-empty
|
|
119
|
+
const unrespondedRequests = new Map();
|
|
120
|
+
const worker = new Worker(url);
|
|
121
|
+
worker.addEventListener('message', ({ data }) => {
|
|
122
|
+
if (isCallNotification(data)) {
|
|
123
|
+
const { params: { timerId, timerType } } = data;
|
|
124
|
+
if (timerType === 'interval') {
|
|
125
|
+
const idOrFunc = scheduledIntervalFunctions.get(timerId);
|
|
126
|
+
if (typeof idOrFunc === undefined) {
|
|
127
|
+
throw new Error('The timer is in an undefined state.');
|
|
128
|
+
}
|
|
129
|
+
if (typeof idOrFunc === 'number') {
|
|
130
|
+
const timerIdAndTimerType = unrespondedRequests.get(idOrFunc);
|
|
131
|
+
if (timerIdAndTimerType === undefined ||
|
|
132
|
+
timerIdAndTimerType.timerId !== timerId ||
|
|
133
|
+
timerIdAndTimerType.timerType !== timerType) {
|
|
134
|
+
throw new Error('The timer is in an undefined state.');
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
else if (typeof idOrFunc === 'function') {
|
|
138
|
+
idOrFunc();
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
else if (timerType === 'timeout') {
|
|
142
|
+
const idOrFunc = scheduledTimeoutFunctions.get(timerId);
|
|
143
|
+
if (typeof idOrFunc === undefined) {
|
|
144
|
+
throw new Error('The timer is in an undefined state.');
|
|
145
|
+
}
|
|
146
|
+
if (typeof idOrFunc === 'number') {
|
|
147
|
+
const timerIdAndTimerType = unrespondedRequests.get(idOrFunc);
|
|
148
|
+
if (timerIdAndTimerType === undefined ||
|
|
149
|
+
timerIdAndTimerType.timerId !== timerId ||
|
|
150
|
+
timerIdAndTimerType.timerType !== timerType) {
|
|
151
|
+
throw new Error('The timer is in an undefined state.');
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
else if (typeof idOrFunc === 'function') {
|
|
155
|
+
idOrFunc();
|
|
156
|
+
// A timeout can be savely deleted because it is only called once.
|
|
157
|
+
scheduledTimeoutFunctions.delete(timerId);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
else if (isClearResponse(data)) {
|
|
162
|
+
const { id } = data;
|
|
163
|
+
const timerIdAndTimerType = unrespondedRequests.get(id);
|
|
164
|
+
if (timerIdAndTimerType === undefined) {
|
|
165
|
+
throw new Error('The timer is in an undefined state.');
|
|
166
|
+
}
|
|
167
|
+
const { timerId, timerType } = timerIdAndTimerType;
|
|
168
|
+
unrespondedRequests.delete(id);
|
|
169
|
+
if (timerType === 'interval') {
|
|
170
|
+
scheduledIntervalFunctions.delete(timerId);
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
scheduledTimeoutFunctions.delete(timerId);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
const { error: { message } } = data;
|
|
178
|
+
throw new Error(message);
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
const clearInterval = (timerId) => {
|
|
182
|
+
if (typeof scheduledIntervalFunctions.get(timerId) === 'function') {
|
|
183
|
+
const id = generateUniqueNumber(unrespondedRequests);
|
|
184
|
+
unrespondedRequests.set(id, { timerId, timerType: 'interval' });
|
|
185
|
+
scheduledIntervalFunctions.set(timerId, id);
|
|
186
|
+
worker.postMessage({
|
|
187
|
+
id,
|
|
188
|
+
method: 'clear',
|
|
189
|
+
params: { timerId, timerType: 'interval' }
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
const clearTimeout = (timerId) => {
|
|
194
|
+
if (typeof scheduledTimeoutFunctions.get(timerId) === 'function') {
|
|
195
|
+
const id = generateUniqueNumber(unrespondedRequests);
|
|
196
|
+
unrespondedRequests.set(id, { timerId, timerType: 'timeout' });
|
|
197
|
+
scheduledTimeoutFunctions.set(timerId, id);
|
|
198
|
+
worker.postMessage({
|
|
199
|
+
id,
|
|
200
|
+
method: 'clear',
|
|
201
|
+
params: { timerId, timerType: 'timeout' }
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
const setInterval = (func, delay = 0, ...args) => {
|
|
206
|
+
const timerId = generateUniqueNumber(scheduledIntervalFunctions);
|
|
207
|
+
scheduledIntervalFunctions.set(timerId, () => {
|
|
208
|
+
func(...args);
|
|
209
|
+
// Doublecheck if the interval should still be rescheduled because it could have been cleared inside of func().
|
|
210
|
+
if (typeof scheduledIntervalFunctions.get(timerId) === 'function') {
|
|
211
|
+
worker.postMessage({
|
|
212
|
+
id: null,
|
|
213
|
+
method: 'set',
|
|
214
|
+
params: {
|
|
215
|
+
delay,
|
|
216
|
+
now: performance.timeOrigin + performance.now(),
|
|
217
|
+
timerId,
|
|
218
|
+
timerType: 'interval'
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
worker.postMessage({
|
|
224
|
+
id: null,
|
|
225
|
+
method: 'set',
|
|
226
|
+
params: {
|
|
227
|
+
delay,
|
|
228
|
+
now: performance.timeOrigin + performance.now(),
|
|
229
|
+
timerId,
|
|
230
|
+
timerType: 'interval'
|
|
231
|
+
}
|
|
232
|
+
});
|
|
233
|
+
return timerId;
|
|
234
|
+
};
|
|
235
|
+
const setTimeout = (func, delay = 0, ...args) => {
|
|
236
|
+
const timerId = generateUniqueNumber(scheduledTimeoutFunctions);
|
|
237
|
+
scheduledTimeoutFunctions.set(timerId, () => func(...args));
|
|
238
|
+
worker.postMessage({
|
|
239
|
+
id: null,
|
|
240
|
+
method: 'set',
|
|
241
|
+
params: {
|
|
242
|
+
delay,
|
|
243
|
+
now: performance.timeOrigin + performance.now(),
|
|
244
|
+
timerId,
|
|
245
|
+
timerType: 'timeout'
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
return timerId;
|
|
249
|
+
};
|
|
250
|
+
return {
|
|
251
|
+
clearInterval,
|
|
252
|
+
clearTimeout,
|
|
253
|
+
setInterval,
|
|
254
|
+
setTimeout
|
|
255
|
+
};
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
const createLoadOrReturnBroker = (loadBroker, worker) => {
|
|
259
|
+
let broker = null;
|
|
260
|
+
return () => {
|
|
261
|
+
if (broker !== null) {
|
|
262
|
+
return broker;
|
|
263
|
+
}
|
|
264
|
+
const blob = new Blob([worker], { type: 'application/javascript; charset=utf-8' });
|
|
265
|
+
const url = URL.createObjectURL(blob);
|
|
266
|
+
broker = loadBroker(url);
|
|
267
|
+
// Bug #1: Edge up until v18 didn't like the URL to be revoked directly.
|
|
268
|
+
setTimeout(() => URL.revokeObjectURL(url));
|
|
269
|
+
return broker;
|
|
270
|
+
};
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
// This is the minified and stringified code of the worker-timers-worker package.
|
|
274
|
+
const worker = `(()=>{"use strict";const e=new Map,t=new Map,r=t=>{const r=e.get(t);return void 0!==r&&(clearTimeout(r),e.delete(t),!0)},s=e=>{const r=t.get(e);return void 0!==r&&(clearTimeout(r),t.delete(e),!0)},o=(e,t)=>{const r=performance.now(),s=e+t-r-performance.timeOrigin;return{expected:r+s,remainingDelay:s}},i=(e,t,r,s)=>{const o=r-performance.now();o>0?e.set(t,setTimeout(i,o,e,t,r,s)):(e.delete(t),postMessage({id:null,method:"call",params:{timerId:t,timerType:s}}))};addEventListener("message",(({data:n})=>{try{if("clear"===n.method){const{id:e,params:{timerId:t,timerType:o}}=n;if("interval"===o)postMessage({id:e,result:r(t)});else{if("timeout"!==o)throw new Error('The given type "'.concat(o,'" is not supported'));postMessage({id:e,result:s(t)})}}else{if("set"!==n.method)throw new Error('The given method "'.concat(n.method,'" is not supported'));{const{params:{delay:r,now:s,timerId:a,timerType:m}}=n;if("interval"===m)((t,r,s)=>{const{expected:n,remainingDelay:a}=o(t,s);e.set(r,setTimeout(i,a,e,r,n,"interval"))})(r,a,s);else{if("timeout"!==m)throw new Error('The given type "'.concat(m,'" is not supported'));((e,r,s)=>{const{expected:n,remainingDelay:a}=o(e,s);t.set(r,setTimeout(i,a,t,r,n,"timeout"))})(r,a,s)}}}}catch(e){postMessage({error:{message:e.message},id:n.id,result:null})}}))})();`; // tslint:disable-line:max-line-length
|
|
275
|
+
|
|
276
|
+
const loadOrReturnBroker = createLoadOrReturnBroker(load, worker);
|
|
277
|
+
const clearInterval = (timerId) => loadOrReturnBroker().clearInterval(timerId);
|
|
278
|
+
const clearTimeout = (timerId) => loadOrReturnBroker().clearTimeout(timerId);
|
|
279
|
+
const setInterval = (...args) => loadOrReturnBroker().setInterval(...args);
|
|
280
|
+
const setTimeout$1 = (...args) => loadOrReturnBroker().setTimeout(...args);
|
|
281
|
+
|
|
50
282
|
/** 通用工具类 */
|
|
51
283
|
const DOMUtilsCommonUtils = {
|
|
52
284
|
windowApi: new WindowApi({
|
|
@@ -180,6 +412,58 @@
|
|
|
180
412
|
delete target[propName];
|
|
181
413
|
}
|
|
182
414
|
},
|
|
415
|
+
/**
|
|
416
|
+
* 自动使用 Worker 执行 setTimeout
|
|
417
|
+
*/
|
|
418
|
+
setTimeout(callback, timeout = 0) {
|
|
419
|
+
try {
|
|
420
|
+
return setTimeout$1(callback, timeout);
|
|
421
|
+
}
|
|
422
|
+
catch (error) {
|
|
423
|
+
return globalThis.setTimeout(callback, timeout);
|
|
424
|
+
}
|
|
425
|
+
},
|
|
426
|
+
/**
|
|
427
|
+
* 配合 .setTimeout 使用
|
|
428
|
+
*/
|
|
429
|
+
clearTimeout(timeId) {
|
|
430
|
+
try {
|
|
431
|
+
if (timeId != null) {
|
|
432
|
+
clearTimeout(timeId);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
catch (error) {
|
|
436
|
+
}
|
|
437
|
+
finally {
|
|
438
|
+
globalThis.clearTimeout(timeId);
|
|
439
|
+
}
|
|
440
|
+
},
|
|
441
|
+
/**
|
|
442
|
+
* 自动使用 Worker 执行 setInterval
|
|
443
|
+
*/
|
|
444
|
+
setInterval(callback, timeout = 0) {
|
|
445
|
+
try {
|
|
446
|
+
return setInterval(callback, timeout);
|
|
447
|
+
}
|
|
448
|
+
catch (error) {
|
|
449
|
+
return globalThis.setInterval(callback, timeout);
|
|
450
|
+
}
|
|
451
|
+
},
|
|
452
|
+
/**
|
|
453
|
+
* 配合 .setInterval 使用
|
|
454
|
+
*/
|
|
455
|
+
clearInterval(timeId) {
|
|
456
|
+
try {
|
|
457
|
+
if (timeId != null) {
|
|
458
|
+
clearInterval(timeId);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
catch (error) {
|
|
462
|
+
}
|
|
463
|
+
finally {
|
|
464
|
+
globalThis.clearInterval(timeId);
|
|
465
|
+
}
|
|
466
|
+
},
|
|
183
467
|
};
|
|
184
468
|
|
|
185
469
|
/* 数据 */
|
|
@@ -600,7 +884,7 @@
|
|
|
600
884
|
}
|
|
601
885
|
if (checkDOMReadyState()) {
|
|
602
886
|
/* 检查document状态 */
|
|
603
|
-
setTimeout(callback);
|
|
887
|
+
DOMUtilsCommonUtils.setTimeout(callback);
|
|
604
888
|
}
|
|
605
889
|
else {
|
|
606
890
|
/* 添加监听 */
|
|
@@ -1066,7 +1350,7 @@
|
|
|
1066
1350
|
super(option);
|
|
1067
1351
|
}
|
|
1068
1352
|
/** 版本号 */
|
|
1069
|
-
version = "2025.5.
|
|
1353
|
+
version = "2025.5.26";
|
|
1070
1354
|
attr(element, attrName, attrValue) {
|
|
1071
1355
|
let DOMUtilsContext = this;
|
|
1072
1356
|
if (typeof element === "string") {
|
|
@@ -2048,7 +2332,7 @@
|
|
|
2048
2332
|
DOMUtilsContext.windowApi.globalThis.getComputedStyle(element)[prop];
|
|
2049
2333
|
to[prop] = styles[prop];
|
|
2050
2334
|
}
|
|
2051
|
-
let timer = setInterval(function () {
|
|
2335
|
+
let timer = DOMUtilsCommonUtils.setInterval(function () {
|
|
2052
2336
|
let timePassed = performance.now() - start;
|
|
2053
2337
|
let progress = timePassed / duration;
|
|
2054
2338
|
if (progress > 1) {
|
|
@@ -2059,7 +2343,7 @@
|
|
|
2059
2343
|
from[prop] + (to[prop] - from[prop]) * progress + "px";
|
|
2060
2344
|
}
|
|
2061
2345
|
if (progress === 1) {
|
|
2062
|
-
clearInterval(timer);
|
|
2346
|
+
DOMUtilsCommonUtils.clearInterval(timer);
|
|
2063
2347
|
if (callback) {
|
|
2064
2348
|
callback();
|
|
2065
2349
|
}
|