@whitesev/utils 2.6.5 → 2.6.7

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/src/Utils.ts CHANGED
@@ -23,6 +23,12 @@ import {
23
23
  type UtilsOwnObject,
24
24
  } from "./types/global";
25
25
  import type { WindowApiOption } from "./types/WindowApi";
26
+ import {
27
+ clearInterval as WorkerClearInterval,
28
+ clearTimeout as WorkerClearTimeout,
29
+ setInterval as WorkerSetInterval,
30
+ setTimeout as WorkerSetTimeout,
31
+ } from "worker-timers";
26
32
  import { ModuleRaid } from "./ModuleRaid";
27
33
 
28
34
  class Utils {
@@ -31,7 +37,7 @@ class Utils {
31
37
  this.windowApi = new WindowApi(option);
32
38
  }
33
39
  /** 版本号 */
34
- version = "2025.4.11";
40
+ version = "2025.5.26";
35
41
 
36
42
  /**
37
43
  * 在页面中增加style元素,如果html节点存在子节点,添加子节点第一个,反之,添加到html节点的子节点最后一个
@@ -421,11 +427,11 @@ class Utils {
421
427
  ): (...args: A) => void;
422
428
  debounce<A extends any[], R>(fn: (...args: A) => R, delay = 0) {
423
429
  let timer: any = null as any;
424
- const context = this;
430
+ let UtilsContext = this;
425
431
  return function (...args: A) {
426
- clearTimeout(timer);
427
- timer = setTimeout(function () {
428
- fn.apply(context, args);
432
+ UtilsContext.workerClearTimeout(timer);
433
+ timer = UtilsContext.workerSetTimeout(function () {
434
+ fn.apply(UtilsContext, args);
429
435
  }, delay);
430
436
  };
431
437
  }
@@ -519,6 +525,7 @@ class Utils {
519
525
  eventName: DOMUtils_EventType | DOMUtils_EventType[] | string,
520
526
  details?: any
521
527
  ) {
528
+ // let UtilsContext = this;
522
529
  let eventNameList: string[] = [];
523
530
  if (typeof eventName === "string") {
524
531
  eventNameList = [eventName];
@@ -548,6 +555,7 @@ class Utils {
548
555
  isIFrame?: boolean
549
556
  ): void;
550
557
  downloadBase64(base64Data: string, fileName: string, isIFrame = false) {
558
+ let UtilsContext = this;
551
559
  if (typeof base64Data !== "string") {
552
560
  throw new Error(
553
561
  "Utils.downloadBase64 参数 base64Data 必须为 string 类型"
@@ -562,7 +570,7 @@ class Utils {
562
570
  iframeElement.style.display = "none";
563
571
  iframeElement.src = base64Data;
564
572
  this.windowApi.document.body.appendChild(iframeElement);
565
- setTimeout(() => {
573
+ UtilsContext.workerSetTimeout(() => {
566
574
  iframeElement!.contentWindow!.document.execCommand(
567
575
  "SaveAs",
568
576
  true,
@@ -3507,7 +3515,7 @@ class Utils {
3507
3515
  throw new TypeError("Utils.setTimeout 参数 delayTime 必须为 number 类型");
3508
3516
  }
3509
3517
  return new Promise((resolve) => {
3510
- setTimeout(() => {
3518
+ UtilsContext.workerSetTimeout(() => {
3511
3519
  resolve(UtilsContext.tryCatch().run(callback));
3512
3520
  }, delayTime);
3513
3521
  });
@@ -3520,11 +3528,12 @@ class Utils {
3520
3528
  **/
3521
3529
  sleep(delayTime?: number): Promise<void>;
3522
3530
  sleep(delayTime: number = 0): Promise<void> {
3531
+ let UtilsContext = this;
3523
3532
  if (typeof delayTime !== "number") {
3524
3533
  throw new Error("Utils.sleep 参数 delayTime 必须为 number 类型");
3525
3534
  }
3526
3535
  return new Promise((resolve) => {
3527
- setTimeout(() => {
3536
+ UtilsContext.workerSetTimeout(() => {
3528
3537
  resolve(void 0);
3529
3538
  }, delayTime);
3530
3539
  });
@@ -4188,7 +4197,7 @@ class Utils {
4188
4197
  }
4189
4198
  );
4190
4199
  if (__timeout__ > 0) {
4191
- setTimeout(() => {
4200
+ UtilsContext.workerSetTimeout(() => {
4192
4201
  // 取消观察器
4193
4202
  if (typeof observer?.disconnect === "function") {
4194
4203
  observer.disconnect();
@@ -4913,12 +4922,13 @@ class Utils {
4913
4922
  intervalTimer: number = 250,
4914
4923
  maxTime: number = -1
4915
4924
  ): Promise<T> {
4925
+ let UtilsContext = this;
4916
4926
  if (checkObj == null) {
4917
4927
  throw new TypeError("checkObj 不能为空对象 ");
4918
4928
  }
4919
4929
  let isResolve = false;
4920
4930
  return new Promise((resolve, reject) => {
4921
- let interval = setInterval(() => {
4931
+ let interval = UtilsContext.workerSetInterval(() => {
4922
4932
  let obj = checkObj;
4923
4933
  if (typeof checkObj === "function") {
4924
4934
  obj = checkObj();
@@ -4934,14 +4944,14 @@ class Utils {
4934
4944
  Reflect.has(obj, checkPropertyName as string)
4935
4945
  ) {
4936
4946
  isResolve = true;
4937
- clearInterval(interval);
4947
+ UtilsContext.workerClearInterval(interval);
4938
4948
  resolve((obj as any)[checkPropertyName as string]);
4939
4949
  }
4940
4950
  }, intervalTimer);
4941
4951
  if (maxTime !== -1) {
4942
- setTimeout(() => {
4952
+ UtilsContext.workerSetTimeout(() => {
4943
4953
  if (!isResolve) {
4944
- clearInterval(interval);
4954
+ UtilsContext.workerClearInterval(interval);
4945
4955
  reject();
4946
4956
  }
4947
4957
  }, maxTime);
@@ -5197,6 +5207,22 @@ class Utils {
5197
5207
  }
5198
5208
  return new URL(text);
5199
5209
  }
5210
+ /**
5211
+ * 覆盖对象中的函数this指向
5212
+ * @param target 需要覆盖的对象
5213
+ * @param [objectThis] 覆盖的this指向,如果为传入,则默认为对象本身
5214
+ */
5215
+ coverObjectFunctionThis(target: any, objectThis?: any) {
5216
+ if (typeof target !== "object" || target === null) {
5217
+ throw new Error("target must be object");
5218
+ }
5219
+ objectThis = objectThis || target;
5220
+ Object.keys(target).forEach((key) => {
5221
+ if (typeof target[key] === "function") {
5222
+ target[key] = target[key].bind(objectThis);
5223
+ }
5224
+ });
5225
+ }
5200
5226
  /**
5201
5227
  * 生成uuid
5202
5228
  * @example
@@ -5217,6 +5243,58 @@ class Utils {
5217
5243
  */
5218
5244
  Vue = Vue;
5219
5245
  ModuleRaid = ModuleRaid;
5246
+ /**
5247
+ * 自动使用 Worker 执行 setTimeout
5248
+ * @param callback 回调函数
5249
+ * @param [timeout=0] 延迟时间,默认为0
5250
+ */
5251
+ workerSetTimeout(callback: Function, timeout: number = 0) {
5252
+ try {
5253
+ return WorkerSetTimeout(callback, timeout);
5254
+ } catch (error) {
5255
+ return globalThis.setTimeout(callback, timeout);
5256
+ }
5257
+ }
5258
+ /**
5259
+ * 配合 .setTimeout 使用
5260
+ * @param timeId setTimeout 返回的`id`
5261
+ */
5262
+ workerClearTimeout(timeId: number | undefined) {
5263
+ try {
5264
+ if (timeId != null) {
5265
+ WorkerClearTimeout(timeId);
5266
+ }
5267
+ } catch (error) {
5268
+ } finally {
5269
+ globalThis.clearTimeout(timeId);
5270
+ }
5271
+ }
5272
+ /**
5273
+ * 自动使用 Worker 执行 setInterval
5274
+ * @param callback 回调函数
5275
+ * @param timeout 间隔时间,默认为0
5276
+ */
5277
+ workerSetInterval(callback: Function, timeout: number = 0) {
5278
+ try {
5279
+ return WorkerSetInterval(callback, timeout);
5280
+ } catch (error) {
5281
+ return globalThis.setInterval(callback, timeout);
5282
+ }
5283
+ }
5284
+ /**
5285
+ * 配合 .setInterval 使用
5286
+ * @param timeId setInterval 返回的`id`
5287
+ */
5288
+ workerClearInterval(timeId: number | undefined) {
5289
+ try {
5290
+ if (timeId != null) {
5291
+ WorkerClearInterval(timeId);
5292
+ }
5293
+ } catch (error) {
5294
+ } finally {
5295
+ globalThis.clearInterval(timeId);
5296
+ }
5297
+ }
5220
5298
  }
5221
5299
 
5222
5300
  let utils = new Utils();
@@ -1210,12 +1210,7 @@ export declare interface HttpxRequestOption {
1210
1210
  /**
1211
1211
  * 自定义的配置请求
1212
1212
  */
1213
- export declare interface HttpxRequestOptionConfig extends HttpxRequestOption {
1214
- /**
1215
- * (可选)是否输出请求配置
1216
- */
1217
- logDetails?: boolean;
1218
- }
1213
+ export declare interface HttpxRequestOptionConfig extends HttpxInitOption {}
1219
1214
  /**
1220
1215
  * 响应的数据的data
1221
1216
  */
@@ -1334,3 +1329,26 @@ export declare interface HttpxHookErrorData {
1334
1329
  export declare interface HttpxPromise<T> extends Promise<T> {
1335
1330
  abort: () => void;
1336
1331
  }
1332
+
1333
+ /**
1334
+ * httpx的初始化配置
1335
+ */
1336
+ export declare interface HttpxInitOption extends HttpxRequestOption {
1337
+ /**
1338
+ * 实例化,可传入GM_xmlhttpRequest,未传入则使用window.fetch
1339
+ */
1340
+ xmlHttpRequest?: Function;
1341
+ /**
1342
+ * `baseURL` 将自动加在 `url` 前面,除非 `url` 是一个绝对 URL。
1343
+ */
1344
+ baseURL?: string | undefined;
1345
+ /**
1346
+ * 重试次数
1347
+ * @default 0
1348
+ */
1349
+ retry?: number;
1350
+ /**
1351
+ * (可选)是否输出请求配置
1352
+ */
1353
+ logDetails?: boolean;
1354
+ }