@whitesev/utils 2.11.0 → 2.11.2

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.
@@ -1617,8 +1617,9 @@ declare class Utils {
1617
1617
  }) => any;
1618
1618
  /**
1619
1619
  * 判断页面中是否存在`worker-src`的CSP规则
1620
+ * @param timeout 超时时间,默认为`1500ms`
1620
1621
  */
1621
- hasWorkerCSP(): Promise<boolean>;
1622
+ hasWorkerCSP(timeout?: number): Promise<boolean>;
1622
1623
  }
1623
1624
  declare const utils: Utils;
1624
1625
  export { utils as Utils };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@whitesev/utils",
4
- "version": "2.11.0",
4
+ "version": "2.11.2",
5
5
  "description": "一个常用的工具库",
6
6
  "keywords": [
7
7
  "ScriptCat",
package/src/Utils.ts CHANGED
@@ -3268,12 +3268,26 @@ class Utils {
3268
3268
  }
3269
3269
  return result;
3270
3270
  } else {
3271
- return Array.from(uniqueArrayData).filter(
3272
- (item) =>
3273
- !Array.from(compareArrayData).some(function (item2) {
3274
- return compareFun(item, item2);
3275
- })
3276
- );
3271
+ const compareSet = new Set(compareArrayData);
3272
+ const result: T[] = [];
3273
+
3274
+ for (let i = 0; i < uniqueArrayData.length; i++) {
3275
+ const item = uniqueArrayData[i];
3276
+ let flag = false;
3277
+
3278
+ for (const compareItem of compareSet) {
3279
+ if (compareFun(item, compareItem)) {
3280
+ flag = true;
3281
+ break;
3282
+ }
3283
+ }
3284
+
3285
+ if (!flag) {
3286
+ result.push(item);
3287
+ }
3288
+ }
3289
+
3290
+ return result;
3277
3291
  }
3278
3292
  }
3279
3293
  /**
@@ -3868,10 +3882,13 @@ class Utils {
3868
3882
  }
3869
3883
  /**
3870
3884
  * 判断页面中是否存在`worker-src`的CSP规则
3885
+ * @param timeout 超时时间,默认为`1500ms`
3871
3886
  */
3872
- hasWorkerCSP() {
3887
+ hasWorkerCSP(timeout: number = 1500) {
3873
3888
  return new Promise<boolean>((resolve) => {
3874
3889
  let flag = true;
3890
+ let timeId: number | undefined = void 0;
3891
+ let worker: Worker | undefined = void 0;
3875
3892
  let workerBlobUrl: string | undefined = void 0;
3876
3893
 
3877
3894
  const workerJs = /*js*/ `
@@ -3888,11 +3905,26 @@ class Utils {
3888
3905
  }
3889
3906
  );
3890
3907
  })();`;
3908
+ /**
3909
+ * 返回结果
3910
+ */
3911
+ const finishCallBack = () => {
3912
+ clearTimeout(timeId);
3913
+ if (worker != null) {
3914
+ worker.terminate();
3915
+ }
3916
+ // 释放
3917
+ if (typeof workerBlobUrl === "string") {
3918
+ globalThis.URL.revokeObjectURL(workerBlobUrl);
3919
+ workerBlobUrl = void 0;
3920
+ }
3921
+ resolve(flag);
3922
+ };
3891
3923
  try {
3892
3924
  const workerScript = new Blob([workerJs], {
3893
3925
  type: "application/javascript",
3894
3926
  });
3895
- workerBlobUrl = window.URL.createObjectURL(workerScript);
3927
+ workerBlobUrl = globalThis.URL.createObjectURL(workerScript);
3896
3928
  // @ts-expect-error
3897
3929
  if (globalThis.trustedTypes && typeof globalThis.trustedTypes.createPolicy === "function") {
3898
3930
  // 使用这个后虽然不报错,但是仍会有blob错误
@@ -3904,23 +3936,25 @@ class Utils {
3904
3936
  });
3905
3937
  workerBlobUrl = workerPolicy.createScriptURL(workerBlobUrl);
3906
3938
  }
3907
- const worker = new Worker(workerBlobUrl!);
3939
+ worker = new Worker(workerBlobUrl!);
3908
3940
  worker.onmessage = (data) => {
3909
3941
  if (data.data.success) {
3910
3942
  flag = false;
3943
+ finishCallBack();
3911
3944
  }
3912
3945
  };
3913
- setTimeout(() => {
3914
- worker.terminate();
3915
- resolve(flag);
3916
- }, 500);
3946
+ timeId = setTimeout(() => {
3947
+ finishCallBack();
3948
+ }, timeout);
3917
3949
  worker.postMessage("test");
3918
3950
  } catch {
3919
3951
  flag = true;
3952
+ finishCallBack();
3920
3953
  } finally {
3921
3954
  // 释放
3922
3955
  if (typeof workerBlobUrl === "string") {
3923
3956
  globalThis.URL.revokeObjectURL(workerBlobUrl);
3957
+ workerBlobUrl = void 0;
3924
3958
  }
3925
3959
  }
3926
3960
  });