@bettergi/utils 0.0.10 → 0.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.
@@ -0,0 +1,72 @@
1
+ /** 默认最大重试次数 */
2
+ const defaultMaxAttempts = 5;
3
+ /** 默认重试间隔(毫秒) */
4
+ const defaultRetryInterval = 1000;
5
+ /**
6
+ * 等待直到条件满足或超时,期间执行重试操作
7
+ * @param condition 返回条件是否满足的函数
8
+ * @param retryAction 每次重试时执行的操作(可选)
9
+ * @param options 配置选项
10
+ * @returns - true 条件满足
11
+ * - false 达到最大重试次数
12
+ */
13
+ export const waitForAction = async (condition, retryAction, options) => {
14
+ const { maxAttempts = defaultMaxAttempts, retryInterval = defaultRetryInterval } = options || {};
15
+ for (let i = 0; i < maxAttempts; i++) {
16
+ if (condition())
17
+ return true;
18
+ await retryAction?.();
19
+ await sleep(retryInterval);
20
+ }
21
+ return false;
22
+ };
23
+ /**
24
+ * 等待某个区域出现,期间执行重试操作
25
+ * @param regionProvider 返回区域的函数
26
+ * @param retryAction 每次重试时执行的操作(可选)
27
+ * @param options 配置选项
28
+ * @returns - true 区域出现
29
+ * - false 达到最大重试次数
30
+ */
31
+ export const waitForRegionAppear = async (regionProvider, retryAction, options) => {
32
+ return waitForAction(() => {
33
+ const region = regionProvider();
34
+ return region != null && region.isExist();
35
+ }, retryAction, options);
36
+ };
37
+ /**
38
+ * 等待某个区域消失,期间执行重试操作
39
+ * @param regionProvider 返回区域的函数
40
+ * @param retryAction 每次重试时执行的操作(可选)
41
+ * @param options 配置选项
42
+ * @returns - true 区域消失
43
+ * - false 达到最大重试次数
44
+ */
45
+ export const waitForRegionDisappear = async (regionProvider, retryAction, options) => {
46
+ return waitForAction(() => {
47
+ const region = regionProvider();
48
+ return !region || !region.isExist();
49
+ }, retryAction, options);
50
+ };
51
+ /**
52
+ * 等待整个画面上某个元素出现,期间执行重试操作
53
+ * @param recognitionObject 识别对象
54
+ * @param retryAction 每次重试时执行的操作(可选)
55
+ * @param options 配置选项
56
+ * @returns - true 整个画面上某个元素出现
57
+ * - false 达到最大重试次数
58
+ */
59
+ export const waitForElementAppear = async (recognitionObject, retryAction, options) => {
60
+ return waitForRegionAppear(() => captureGameRegion().find(recognitionObject), retryAction, options);
61
+ };
62
+ /**
63
+ * 等待整个画面上某个元素消失,期间执行重试操作
64
+ * @param recognitionObject 识别对象
65
+ * @param retryAction 每次重试时执行的操作(可选)
66
+ * @param options 配置选项
67
+ * @returns - true 整个画面上某个元素消失
68
+ * - false 达到最大重试次数
69
+ */
70
+ export const waitForElementDisappear = async (recognitionObject, retryAction, options) => {
71
+ return waitForRegionDisappear(() => captureGameRegion().find(recognitionObject), retryAction, options);
72
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bettergi/utils",
3
- "version": "0.0.10",
3
+ "version": "0.1.0",
4
4
  "description": "开发 BetterGI 脚本常用工具集",
5
5
  "type": "module",
6
6
  "author": "Bread Grocery<https://github.com/breadgrocery>",
@@ -33,7 +33,7 @@
33
33
  "build": "tsc"
34
34
  },
35
35
  "devDependencies": {
36
- "@bettergi/types": "^0.0.13",
37
- "typescript": "5.6.3"
36
+ "@bettergi/types": "^0.1.1",
37
+ "typescript": "^5.9.3"
38
38
  }
39
39
  }