@bettergi/utils 0.0.11 → 0.1.1

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,74 @@
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 (i === 0 && condition())
17
+ return true; // fast path
18
+ await retryAction?.();
19
+ await sleep(retryInterval);
20
+ if (condition())
21
+ return true;
22
+ }
23
+ return false;
24
+ };
25
+ /**
26
+ * 等待某个区域出现,期间执行重试操作
27
+ * @param regionProvider 返回区域的函数
28
+ * @param retryAction 每次重试时执行的操作(可选)
29
+ * @param options 配置选项
30
+ * @returns - true 区域出现
31
+ * - false 达到最大重试次数
32
+ */
33
+ export const waitForRegionAppear = async (regionProvider, retryAction, options) => {
34
+ return waitForAction(() => {
35
+ const region = regionProvider();
36
+ return region != null && region.isExist();
37
+ }, retryAction, options);
38
+ };
39
+ /**
40
+ * 等待某个区域消失,期间执行重试操作
41
+ * @param regionProvider 返回区域的函数
42
+ * @param retryAction 每次重试时执行的操作(可选)
43
+ * @param options 配置选项
44
+ * @returns - true 区域消失
45
+ * - false 达到最大重试次数
46
+ */
47
+ export const waitForRegionDisappear = async (regionProvider, retryAction, options) => {
48
+ return waitForAction(() => {
49
+ const region = regionProvider();
50
+ return !region || !region.isExist();
51
+ }, retryAction, options);
52
+ };
53
+ /**
54
+ * 等待整个画面上某个元素出现,期间执行重试操作
55
+ * @param recognitionObject 识别对象
56
+ * @param retryAction 每次重试时执行的操作(可选)
57
+ * @param options 配置选项
58
+ * @returns - true 整个画面上某个元素出现
59
+ * - false 达到最大重试次数
60
+ */
61
+ export const waitForElementAppear = async (recognitionObject, retryAction, options) => {
62
+ return waitForRegionAppear(() => captureGameRegion().find(recognitionObject), retryAction, options);
63
+ };
64
+ /**
65
+ * 等待整个画面上某个元素消失,期间执行重试操作
66
+ * @param recognitionObject 识别对象
67
+ * @param retryAction 每次重试时执行的操作(可选)
68
+ * @param options 配置选项
69
+ * @returns - true 整个画面上某个元素消失
70
+ * - false 达到最大重试次数
71
+ */
72
+ export const waitForElementDisappear = async (recognitionObject, retryAction, options) => {
73
+ return waitForRegionDisappear(() => captureGameRegion().find(recognitionObject), retryAction, options);
74
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bettergi/utils",
3
- "version": "0.0.11",
3
+ "version": "0.1.1",
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.14",
36
+ "@bettergi/types": "^0.1.1",
37
37
  "typescript": "^5.9.3"
38
38
  }
39
39
  }
package/dist/flow.d.ts DELETED
@@ -1,30 +0,0 @@
1
- /**
2
- * 等待直到条件满足或超时
3
- * @param condition 等待的条件判断函数,返回 true 表示条件满足
4
- * @param timeout 超时时间(毫秒),默认 3000 毫秒
5
- * @param interval 等待间隔(毫秒),默认 300 毫秒
6
- * @param action 每次等待循环中执行的操作(可选)
7
- * @returns - true 在超时前条件已满足
8
- * - false 在超时后条件仍未满足
9
- */
10
- export declare const waitUntil: (condition: (context: Record<string, any>) => boolean, timeout?: number, interval?: number, action?: (context: Record<string, any>) => Promise<void> | void) => Promise<boolean>;
11
- /**
12
- * 断言区域存在
13
- * @param 获取区域的函数
14
- * @param message 错误信息
15
- * @param timeout 超时时间(毫秒),默认 3000 毫秒
16
- * @param interval 等待间隔(毫秒),默认 300 毫秒
17
- * @param action 每次等待循环中执行的操作(可选)
18
- * @throws 如果区域在超时时间内未找到则抛出错误
19
- */
20
- export declare const assertExists: (regionProvider: () => Region | undefined, message?: string, timeout?: number, interval?: number, action?: (context: Record<string, any>) => Promise<void> | void) => Promise<void>;
21
- /**
22
- * 断言区域不存在
23
- * @param 获取区域的函数
24
- * @param message 错误信息
25
- * @param timeout 超时时间(毫秒),默认 3000 毫秒
26
- * @param interval 等待间隔(毫秒),默认 300 毫秒
27
- * @param action 每次等待循环中执行的操作(可选)
28
- * @throws 如果区域在超时时间内仍然存在则抛出错误
29
- */
30
- export declare const assertNotExists: (regionProvider: () => Region | undefined, message?: string, timeout?: number, interval?: number, action?: (context: Record<string, any>) => Promise<void> | void) => Promise<void>;
package/dist/flow.js DELETED
@@ -1,52 +0,0 @@
1
- /** 默认超时时间(毫秒) */
2
- const defaultTimeout = 3 * 1000;
3
- /** 默认等待间隔(毫秒) */
4
- const defaultInterval = 300;
5
- /**
6
- * 等待直到条件满足或超时
7
- * @param condition 等待的条件判断函数,返回 true 表示条件满足
8
- * @param timeout 超时时间(毫秒),默认 3000 毫秒
9
- * @param interval 等待间隔(毫秒),默认 300 毫秒
10
- * @param action 每次等待循环中执行的操作(可选)
11
- * @returns - true 在超时前条件已满足
12
- * - false 在超时后条件仍未满足
13
- */
14
- export const waitUntil = async (condition, timeout = defaultTimeout, interval = defaultInterval, action) => {
15
- const context = {};
16
- const deadline = Date.now() + timeout;
17
- while (Date.now() < deadline) {
18
- if (condition(context))
19
- return true;
20
- await action?.(context);
21
- await sleep(interval);
22
- }
23
- return false;
24
- };
25
- /**
26
- * 断言区域存在
27
- * @param 获取区域的函数
28
- * @param message 错误信息
29
- * @param timeout 超时时间(毫秒),默认 3000 毫秒
30
- * @param interval 等待间隔(毫秒),默认 300 毫秒
31
- * @param action 每次等待循环中执行的操作(可选)
32
- * @throws 如果区域在超时时间内未找到则抛出错误
33
- */
34
- export const assertExists = async (regionProvider, message = "断言区域存在失败", timeout = defaultTimeout, interval = defaultInterval, action) => {
35
- const ok = await waitUntil(() => regionProvider() !== undefined, timeout, interval, action);
36
- if (!ok)
37
- throw new Error(message);
38
- };
39
- /**
40
- * 断言区域不存在
41
- * @param 获取区域的函数
42
- * @param message 错误信息
43
- * @param timeout 超时时间(毫秒),默认 3000 毫秒
44
- * @param interval 等待间隔(毫秒),默认 300 毫秒
45
- * @param action 每次等待循环中执行的操作(可选)
46
- * @throws 如果区域在超时时间内仍然存在则抛出错误
47
- */
48
- export const assertNotExists = async (regionProvider, message = "断言区域不存在失败", timeout = defaultTimeout, interval = defaultInterval, action) => {
49
- const ok = await waitUntil(() => regionProvider() === undefined, timeout, interval, action);
50
- if (!ok)
51
- throw new Error(message);
52
- };