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