alemonjs 2.0.5 → 2.0.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/lib/app/code.d.ts +22 -0
- package/lib/app/code.js +32 -0
- package/lib/app/define-bot.d.ts +2 -0
- package/lib/app/define-bot.js +16 -1
- package/lib/app/define-chidren.d.ts +2 -0
- package/lib/app/define-chidren.js +8 -0
- package/lib/app/event-middleware.d.ts +3 -0
- package/lib/app/event-middleware.js +32 -5
- package/lib/app/event-processor-cycle.js +19 -7
- package/lib/app/event-processor.d.ts +1 -0
- package/lib/app/event-response.d.ts +1 -0
- package/lib/app/event-response.js +7 -0
- package/lib/app/hook-use-api.d.ts +8 -6
- package/lib/app/hook-use-api.js +55 -20
- package/lib/app/hook-use-state.d.ts +3 -1
- package/lib/app/hook-use-state.js +53 -21
- package/lib/app/hook-use-subscribe.d.ts +2 -1
- package/lib/app/hook-use-subscribe.js +19 -1
- package/lib/app/load.d.ts +3 -1
- package/lib/app/load.js +37 -6
- package/lib/app/message-api.d.ts +14 -4
- package/lib/app/message-api.js +21 -29
- package/lib/app/message-format.d.ts +1 -0
- package/lib/app/message-format.js +0 -72
- package/lib/app/store.d.ts +2 -2
- package/lib/app/store.js +0 -1
- package/lib/app/utils.d.ts +23 -1
- package/lib/app/utils.js +35 -3
- package/lib/code.d.ts +19 -0
- package/lib/code.js +27 -0
- package/lib/config.d.ts +1 -0
- package/lib/config.js +23 -5
- package/lib/index.d.ts +2 -1
- package/lib/index.js +2 -1
- package/lib/jsx.d.ts +15 -4
- package/lib/jsx.js +6 -20
- package/lib/main.js +7 -9
- package/lib/typing/client/index.d.ts +16 -4
- package/package.json +1 -1
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 结果反馈码
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* 结果反馈码
|
|
6
|
+
* @description
|
|
7
|
+
* - 2000: 成功
|
|
8
|
+
* - 4000: 未知错误
|
|
9
|
+
* - 4001: 参数错误
|
|
10
|
+
* - 4002: 权限不足
|
|
11
|
+
*/
|
|
12
|
+
declare const ResultCode: {
|
|
13
|
+
readonly Ok: 2000;
|
|
14
|
+
readonly Fail: 4000;
|
|
15
|
+
readonly FailParams: 4001;
|
|
16
|
+
readonly Warn: 2100;
|
|
17
|
+
readonly FailAuth: 4002;
|
|
18
|
+
readonly FailInternal: 5000;
|
|
19
|
+
};
|
|
20
|
+
type ResultCode = typeof ResultCode[keyof typeof ResultCode];
|
|
21
|
+
|
|
22
|
+
export { ResultCode };
|
package/lib/app/code.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 结果反馈码
|
|
3
|
+
*/
|
|
4
|
+
// 成功码
|
|
5
|
+
const Ok = 2000; // 成功
|
|
6
|
+
// 警惕码
|
|
7
|
+
const Warn = 2100; // 任意警告
|
|
8
|
+
// 失败码
|
|
9
|
+
const Fail = 4000; // 未知错误
|
|
10
|
+
const FailParams = 4001; // 参数错误
|
|
11
|
+
// 权限错误
|
|
12
|
+
const FailAuth = 4002; // 权限不足
|
|
13
|
+
// 内部错误
|
|
14
|
+
const FailInternal = 5000; // 内部错误
|
|
15
|
+
/**
|
|
16
|
+
* 结果反馈码
|
|
17
|
+
* @description
|
|
18
|
+
* - 2000: 成功
|
|
19
|
+
* - 4000: 未知错误
|
|
20
|
+
* - 4001: 参数错误
|
|
21
|
+
* - 4002: 权限不足
|
|
22
|
+
*/
|
|
23
|
+
const ResultCode = {
|
|
24
|
+
Ok,
|
|
25
|
+
Fail,
|
|
26
|
+
FailParams,
|
|
27
|
+
Warn,
|
|
28
|
+
FailAuth,
|
|
29
|
+
FailInternal
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export { ResultCode };
|
package/lib/app/define-bot.d.ts
CHANGED
package/lib/app/define-bot.js
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
1
|
+
import { ResultCode } from '../code.js';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* 定义机器人
|
|
3
5
|
* @param callback
|
|
6
|
+
* @throws {Error} - 如果 callback 无效,抛出错误。
|
|
4
7
|
* @returns
|
|
5
8
|
*/
|
|
6
9
|
const definePlatform = callback => {
|
|
7
10
|
// 判断是否是函数
|
|
8
11
|
if (typeof callback !== 'function') {
|
|
12
|
+
logger.error({
|
|
13
|
+
code: ResultCode.FailParams,
|
|
14
|
+
message: 'Invalid callback: callback must be a function',
|
|
15
|
+
data: null
|
|
16
|
+
});
|
|
9
17
|
throw new Error('Invalid callback: callback must be a function');
|
|
10
18
|
}
|
|
11
19
|
return {
|
|
@@ -18,7 +26,14 @@ global.definePlatform = definePlatform;
|
|
|
18
26
|
* 废弃,请使用 definePlatform
|
|
19
27
|
* @deprecated
|
|
20
28
|
*/
|
|
21
|
-
const defineBot =
|
|
29
|
+
const defineBot = callback => {
|
|
30
|
+
logger.warn({
|
|
31
|
+
code: ResultCode.Warn,
|
|
32
|
+
message: 'defineBot is deprecated, please use definePlatform',
|
|
33
|
+
data: null
|
|
34
|
+
});
|
|
35
|
+
return definePlatform(callback);
|
|
36
|
+
};
|
|
22
37
|
global.defineBot = defineBot;
|
|
23
38
|
|
|
24
39
|
export { defineBot, definePlatform };
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
import { ResultCode } from '../code.js';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* 定义子事件
|
|
3
5
|
* @param callback
|
|
6
|
+
* @throws {Error} - 如果 callback 无效,抛出错误。
|
|
4
7
|
* @returns
|
|
5
8
|
*/
|
|
6
9
|
const defineChildren = callback => {
|
|
@@ -10,6 +13,11 @@ const defineChildren = callback => {
|
|
|
10
13
|
callback
|
|
11
14
|
};
|
|
12
15
|
}
|
|
16
|
+
logger.error({
|
|
17
|
+
code: ResultCode.FailParams,
|
|
18
|
+
message: 'Invalid callback: callback must be a object or function',
|
|
19
|
+
data: null
|
|
20
|
+
});
|
|
13
21
|
throw new Error('Invalid callback: callback must be a object or function');
|
|
14
22
|
};
|
|
15
23
|
global.defineChildren = defineChildren;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { OnMiddlewareReversalFunc, OnMiddlewareFunc } from '../typing/event/index.js';
|
|
2
|
+
import '../global.js';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* @fileoverview 中间件
|
|
@@ -10,6 +11,8 @@ import { OnMiddlewareReversalFunc, OnMiddlewareFunc } from '../typing/event/inde
|
|
|
10
11
|
* 中间件
|
|
11
12
|
* @param select 事件选择
|
|
12
13
|
* @param callback 回调函数,处理事件和 API
|
|
14
|
+
* @throws {Error} - 如果 select 无效,抛出错误。
|
|
15
|
+
* @throws {Error} - 如果 callback 无效,抛出错误。
|
|
13
16
|
* @returns
|
|
14
17
|
*/
|
|
15
18
|
declare const onMiddleware: OnMiddlewareReversalFunc;
|
|
@@ -1,19 +1,46 @@
|
|
|
1
|
+
import { ResultCode } from '../code.js';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* 中间件
|
|
3
5
|
* @param select 事件选择
|
|
4
6
|
* @param callback 回调函数,处理事件和 API
|
|
7
|
+
* @throws {Error} - 如果 select 无效,抛出错误。
|
|
8
|
+
* @throws {Error} - 如果 callback 无效,抛出错误。
|
|
5
9
|
* @returns
|
|
6
10
|
*/
|
|
7
|
-
const onMiddleware = (select, callback) =>
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
+
const onMiddleware = (select, callback) => {
|
|
12
|
+
// 参数检查
|
|
13
|
+
if (typeof callback !== 'function') {
|
|
14
|
+
logger.error({
|
|
15
|
+
code: ResultCode.FailParams,
|
|
16
|
+
message: 'Invalid callback: callback must be a function',
|
|
17
|
+
data: null
|
|
18
|
+
});
|
|
19
|
+
throw new Error('Invalid callback: callback must be a function');
|
|
20
|
+
}
|
|
21
|
+
if (typeof select === 'string' || typeof select === 'object') {
|
|
22
|
+
return { current: callback, select };
|
|
23
|
+
}
|
|
24
|
+
logger.error({
|
|
25
|
+
code: ResultCode.FailParams,
|
|
26
|
+
message: 'Invalid select: select must be a string or object',
|
|
27
|
+
data: null
|
|
28
|
+
});
|
|
29
|
+
throw new Error('Invalid select: select must be a string or object');
|
|
30
|
+
};
|
|
11
31
|
global.onMiddleware = onMiddleware;
|
|
12
32
|
/**
|
|
13
33
|
* 废弃,请使用 onMiddleware
|
|
14
34
|
* @deprecated
|
|
15
35
|
*/
|
|
16
|
-
const OnMiddleware = (callback, select) =>
|
|
36
|
+
const OnMiddleware = (callback, select) => {
|
|
37
|
+
logger.warn({
|
|
38
|
+
code: ResultCode.Warn,
|
|
39
|
+
message: 'OnMiddleware is deprecated, please use onMiddleware',
|
|
40
|
+
data: null
|
|
41
|
+
});
|
|
42
|
+
return ({ select, current: callback });
|
|
43
|
+
};
|
|
17
44
|
global.OnMiddleware = OnMiddleware;
|
|
18
45
|
|
|
19
46
|
export { OnMiddleware, onMiddleware };
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { ResultCode } from '../code.js';
|
|
1
2
|
import { expendEvent } from './event-processor-event.js';
|
|
2
3
|
import { expendMiddleware } from './event-processor-middleware.js';
|
|
3
4
|
import { expendSubscribeCreate, expendSubscribeMount, expendSubscribeUnmount } from './event-processor-subscribe.js';
|
|
@@ -8,19 +9,31 @@ import { expendSubscribeCreate, expendSubscribeMount, expendSubscribeUnmount } f
|
|
|
8
9
|
* @param select
|
|
9
10
|
*/
|
|
10
11
|
const showLog = (event, select) => {
|
|
11
|
-
const
|
|
12
|
+
const log = {
|
|
13
|
+
'Name': select
|
|
14
|
+
};
|
|
12
15
|
if (typeof event['ChannelId'] == 'string' && event['ChannelId'] != '') {
|
|
13
|
-
|
|
16
|
+
log['ChannelId'] = event['ChannelId'];
|
|
14
17
|
}
|
|
15
18
|
if (typeof event['UserKey'] == 'string' && event['UserKey'] != '') {
|
|
16
|
-
|
|
19
|
+
log['UserKey'] = event['UserKey'];
|
|
20
|
+
}
|
|
21
|
+
if (typeof event['UserId'] == 'string' && event['UserId'] != '') {
|
|
22
|
+
log['UserId'] = event['UserId'];
|
|
23
|
+
}
|
|
24
|
+
if (typeof event['MessageId'] == 'string' && event['MessageId'] != '') {
|
|
25
|
+
log['MessageId'] = event['MessageId'];
|
|
17
26
|
}
|
|
18
27
|
if (Array.isArray(event['MessageBody'])) {
|
|
19
28
|
const content = event['MessageBody'].filter(item => item.type == 'Text').map(item => item.value);
|
|
20
29
|
const txt = content.join('');
|
|
21
|
-
|
|
30
|
+
log['text'] = txt;
|
|
22
31
|
}
|
|
23
|
-
|
|
32
|
+
logger.info({
|
|
33
|
+
code: ResultCode.Ok,
|
|
34
|
+
message: 'new event',
|
|
35
|
+
data: log
|
|
36
|
+
});
|
|
24
37
|
};
|
|
25
38
|
/**
|
|
26
39
|
* 消息体处理机制
|
|
@@ -63,8 +76,7 @@ const expendCycle = async (valueEvent, select) => {
|
|
|
63
76
|
}
|
|
64
77
|
expendMiddleware(valueEvent, select, nextMount);
|
|
65
78
|
};
|
|
66
|
-
|
|
67
|
-
logger.info(log.join(''));
|
|
79
|
+
showLog(valueEvent, select);
|
|
68
80
|
// create
|
|
69
81
|
expendSubscribeCreate(valueEvent, select, nextCreate);
|
|
70
82
|
};
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { ResultCode } from '../code.js';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* 处理响应事件
|
|
3
5
|
* @param select 事件选择
|
|
@@ -13,6 +15,11 @@ global.onResponse = onResponse;
|
|
|
13
15
|
* @deprecated
|
|
14
16
|
*/
|
|
15
17
|
const OnResponse = (callback, select) => {
|
|
18
|
+
logger.warn({
|
|
19
|
+
code: ResultCode.Warn,
|
|
20
|
+
message: 'OnResponse is deprecated, please use onResponse',
|
|
21
|
+
data: null
|
|
22
|
+
});
|
|
16
23
|
return { current: callback, select };
|
|
17
24
|
};
|
|
18
25
|
global.OnResponse = OnResponse;
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
import { Result } from './utils.js';
|
|
1
2
|
import { EventKeys, Events } from '../typing/event/map.js';
|
|
2
|
-
import
|
|
3
|
+
import '../global.js';
|
|
3
4
|
import { DataEnums } from '../typing/message/index.js';
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* 使用提及。
|
|
7
8
|
* @param {Object} event - 事件对象,包含触发提及的相关信息。
|
|
9
|
+
* @throws {Error} - 如果 event 无效,抛出错误。
|
|
8
10
|
*/
|
|
9
11
|
declare const useMention: <T extends EventKeys>(event: Events[T]) => {
|
|
10
12
|
find: (options: {
|
|
@@ -13,25 +15,25 @@ declare const useMention: <T extends EventKeys>(event: Events[T]) => {
|
|
|
13
15
|
UserName?: string;
|
|
14
16
|
IsMaster?: boolean;
|
|
15
17
|
IsBot?: boolean;
|
|
16
|
-
}) => Promise<
|
|
18
|
+
}) => Promise<Result>;
|
|
17
19
|
findOne: (options?: {
|
|
18
20
|
UserId?: string;
|
|
19
21
|
UserKey?: string;
|
|
20
22
|
UserName?: string;
|
|
21
23
|
IsMaster?: boolean;
|
|
22
24
|
IsBot?: boolean;
|
|
23
|
-
}) => Promise<
|
|
25
|
+
}) => Promise<Result>;
|
|
24
26
|
}[];
|
|
25
27
|
/**
|
|
26
28
|
* 使用发送消息。
|
|
27
29
|
* @param {Object} event - 事件对象,包含触发发送的相关信息。
|
|
28
|
-
* @returns {Function} - 返回一个异步函数,用于发送消息。
|
|
29
30
|
* @throws {Error} - 如果 event 无效,抛出错误。
|
|
30
31
|
*/
|
|
31
|
-
declare const useSend: <T extends EventKeys>(event: Events[T]) => (...val: DataEnums[]) => Promise<
|
|
32
|
+
declare const useSend: <T extends EventKeys>(event: Events[T]) => (...val: DataEnums[]) => Promise<Result>;
|
|
32
33
|
/**
|
|
33
34
|
* 卸载模块
|
|
34
35
|
* @param name
|
|
36
|
+
* @throws {Error} - 如果 name 无效,抛出错误。
|
|
35
37
|
*/
|
|
36
38
|
declare const unChildren: (name?: string) => void;
|
|
37
39
|
/**
|
|
@@ -40,7 +42,7 @@ declare const unChildren: (name?: string) => void;
|
|
|
40
42
|
*/
|
|
41
43
|
declare const unMount: () => void;
|
|
42
44
|
/**
|
|
43
|
-
*
|
|
45
|
+
* 创建选择器
|
|
44
46
|
* @param values
|
|
45
47
|
* @returns
|
|
46
48
|
*/
|
package/lib/app/hook-use-api.js
CHANGED
|
@@ -1,18 +1,34 @@
|
|
|
1
|
+
import { ResultCode } from '../code.js';
|
|
1
2
|
import { ChildrenApp } from './store.js';
|
|
3
|
+
import { createResult } from './utils.js';
|
|
2
4
|
|
|
3
5
|
/**
|
|
4
6
|
* 使用提及。
|
|
5
7
|
* @param {Object} event - 事件对象,包含触发提及的相关信息。
|
|
8
|
+
* @throws {Error} - 如果 event 无效,抛出错误。
|
|
6
9
|
*/
|
|
7
10
|
const useMention = (event) => {
|
|
8
11
|
if (!event || typeof event !== 'object') {
|
|
12
|
+
logger.error({
|
|
13
|
+
code: ResultCode.FailParams,
|
|
14
|
+
message: 'Invalid event: event must be an object',
|
|
15
|
+
data: null
|
|
16
|
+
});
|
|
9
17
|
throw new Error('Invalid event: event must be an object');
|
|
10
18
|
}
|
|
11
19
|
let res = null;
|
|
12
20
|
const mention = {
|
|
13
21
|
find: async (options) => {
|
|
14
|
-
|
|
15
|
-
|
|
22
|
+
try {
|
|
23
|
+
if (!res) {
|
|
24
|
+
res = await alemonjsBot.api.use.mention(event);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
catch (err) {
|
|
28
|
+
return createResult(ResultCode.Fail, err?.message || 'Failed to get mention data', null);
|
|
29
|
+
}
|
|
30
|
+
if (!Array.isArray(res)) {
|
|
31
|
+
return createResult(ResultCode.Fail, 'No mention data found', null);
|
|
16
32
|
}
|
|
17
33
|
// 过滤出符合条件的数据
|
|
18
34
|
const data = res.filter(item => {
|
|
@@ -33,13 +49,21 @@ const useMention = (event) => {
|
|
|
33
49
|
}
|
|
34
50
|
return true;
|
|
35
51
|
});
|
|
36
|
-
return data;
|
|
52
|
+
return createResult(ResultCode.Ok, 'Successfully retrieved mention data', data);
|
|
37
53
|
},
|
|
38
54
|
findOne: async (options = {
|
|
39
55
|
IsBot: false
|
|
40
56
|
}) => {
|
|
41
|
-
|
|
42
|
-
|
|
57
|
+
try {
|
|
58
|
+
if (!res) {
|
|
59
|
+
res = await alemonjsBot.api.use.mention(event);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
catch (err) {
|
|
63
|
+
return createResult(ResultCode.Fail, err?.message || 'Failed to get mention data', null);
|
|
64
|
+
}
|
|
65
|
+
if (!Array.isArray(res)) {
|
|
66
|
+
return createResult(ResultCode.Fail, 'No mention data found', null);
|
|
43
67
|
}
|
|
44
68
|
// 根据条件查找
|
|
45
69
|
const data = res.find(item => {
|
|
@@ -60,7 +84,10 @@ const useMention = (event) => {
|
|
|
60
84
|
}
|
|
61
85
|
return true;
|
|
62
86
|
});
|
|
63
|
-
|
|
87
|
+
if (!data) {
|
|
88
|
+
return createResult(ResultCode.Fail, 'No mention data found', null);
|
|
89
|
+
}
|
|
90
|
+
return createResult(ResultCode.Ok, 'Successfully retrieved mention data', data);
|
|
64
91
|
}
|
|
65
92
|
};
|
|
66
93
|
return [mention];
|
|
@@ -68,33 +95,38 @@ const useMention = (event) => {
|
|
|
68
95
|
/**
|
|
69
96
|
* 使用发送消息。
|
|
70
97
|
* @param {Object} event - 事件对象,包含触发发送的相关信息。
|
|
71
|
-
* @returns {Function} - 返回一个异步函数,用于发送消息。
|
|
72
98
|
* @throws {Error} - 如果 event 无效,抛出错误。
|
|
73
99
|
*/
|
|
74
100
|
const useSend = (event) => {
|
|
75
101
|
if (!event || typeof event !== 'object') {
|
|
102
|
+
logger.error({
|
|
103
|
+
code: ResultCode.FailParams,
|
|
104
|
+
message: 'Invalid event: event must be an object',
|
|
105
|
+
data: null
|
|
106
|
+
});
|
|
76
107
|
throw new Error('Invalid event: event must be an object');
|
|
77
108
|
}
|
|
78
109
|
return async (...val) => {
|
|
79
110
|
if (!val || val.length === 0) {
|
|
80
|
-
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
try {
|
|
84
|
-
return await alemonjsBot.api.use.send(event, val);
|
|
85
|
-
}
|
|
86
|
-
catch (e) {
|
|
87
|
-
logger.error('Failed to send message:', e);
|
|
88
|
-
// 弹出错误
|
|
89
|
-
throw e;
|
|
111
|
+
return createResult(ResultCode.FailParams, 'Invalid val: val must be a non-empty array', null);
|
|
90
112
|
}
|
|
113
|
+
return await alemonjsBot.api.use.send(event, val);
|
|
91
114
|
};
|
|
92
115
|
};
|
|
93
116
|
/**
|
|
94
117
|
* 卸载模块
|
|
95
118
|
* @param name
|
|
119
|
+
* @throws {Error} - 如果 name 无效,抛出错误。
|
|
96
120
|
*/
|
|
97
121
|
const unChildren = (name = 'main') => {
|
|
122
|
+
if (!name || typeof name !== 'string') {
|
|
123
|
+
logger.error({
|
|
124
|
+
code: ResultCode.FailParams,
|
|
125
|
+
message: 'Invalid name: name must be a string',
|
|
126
|
+
data: null
|
|
127
|
+
});
|
|
128
|
+
throw new Error('Invalid name: name must be a string');
|
|
129
|
+
}
|
|
98
130
|
const app = new ChildrenApp(name);
|
|
99
131
|
app.un();
|
|
100
132
|
};
|
|
@@ -103,11 +135,14 @@ const unChildren = (name = 'main') => {
|
|
|
103
135
|
* @deprecated
|
|
104
136
|
*/
|
|
105
137
|
const unMount = () => {
|
|
106
|
-
|
|
107
|
-
|
|
138
|
+
logger.warn({
|
|
139
|
+
code: ResultCode.Warn,
|
|
140
|
+
message: 'unMount is deprecated, please use unChildren',
|
|
141
|
+
data: null
|
|
142
|
+
});
|
|
108
143
|
};
|
|
109
144
|
/**
|
|
110
|
-
*
|
|
145
|
+
* 创建选择器
|
|
111
146
|
* @param values
|
|
112
147
|
* @returns
|
|
113
148
|
*/
|
|
@@ -8,13 +8,14 @@
|
|
|
8
8
|
* 将会同时改变,因为状态是全局的
|
|
9
9
|
* @param name 功能名
|
|
10
10
|
* @param defaultValue 默认值,默认为 true
|
|
11
|
-
* @
|
|
11
|
+
* @throws {Error} - 如果 name 不是字符串,或者 defaultValue 不是布尔值,抛出错误。
|
|
12
12
|
*/
|
|
13
13
|
declare const useState: <T extends string>(name: T, defaultValue?: boolean) => [boolean, (value: boolean) => void];
|
|
14
14
|
/**
|
|
15
15
|
* 订阅状态变化
|
|
16
16
|
* @param name 功能名
|
|
17
17
|
* @param callback 回调函数
|
|
18
|
+
* @throws {Error} - 如果 callback 无效,抛出错误。
|
|
18
19
|
*/
|
|
19
20
|
declare const onState: <T extends string>(name: T, callback: (value: boolean) => void) => void;
|
|
20
21
|
/**
|
|
@@ -26,6 +27,7 @@ declare const eventState: <T extends string>(name: T, callback: (value: boolean)
|
|
|
26
27
|
* 取消订阅状态变化
|
|
27
28
|
* @param name 功能名
|
|
28
29
|
* @param callback 回调函数
|
|
30
|
+
* @throws {Error} - 如果 callback 无效,抛出错误。
|
|
29
31
|
*/
|
|
30
32
|
declare const unState: <T extends string>(name: T, callback: (value: boolean) => void) => void;
|
|
31
33
|
/**
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { getConfig } from '../config.js';
|
|
2
|
+
import { ResultCode } from '../code.js';
|
|
2
3
|
import { State, StateSubscribe } from './store.js';
|
|
3
4
|
|
|
4
5
|
/**
|
|
@@ -11,9 +12,26 @@ import { State, StateSubscribe } from './store.js';
|
|
|
11
12
|
* 将会同时改变,因为状态是全局的
|
|
12
13
|
* @param name 功能名
|
|
13
14
|
* @param defaultValue 默认值,默认为 true
|
|
14
|
-
* @
|
|
15
|
+
* @throws {Error} - 如果 name 不是字符串,或者 defaultValue 不是布尔值,抛出错误。
|
|
15
16
|
*/
|
|
16
17
|
const useState = (name, defaultValue = true) => {
|
|
18
|
+
// 检查参数
|
|
19
|
+
if (typeof name !== 'string') {
|
|
20
|
+
logger.error({
|
|
21
|
+
code: ResultCode.FailParams,
|
|
22
|
+
message: 'Invalid name: name must be a string',
|
|
23
|
+
data: null
|
|
24
|
+
});
|
|
25
|
+
throw new Error('Invalid name: name must be a string');
|
|
26
|
+
}
|
|
27
|
+
if (typeof defaultValue !== 'boolean') {
|
|
28
|
+
logger.error({
|
|
29
|
+
code: ResultCode.FailParams,
|
|
30
|
+
message: 'Invalid defaultValue: defaultValue must be a boolean',
|
|
31
|
+
data: null
|
|
32
|
+
});
|
|
33
|
+
throw new Error('Invalid defaultValue: defaultValue must be a boolean');
|
|
34
|
+
}
|
|
17
35
|
const state = new State(name, defaultValue);
|
|
18
36
|
// 设置值的函数
|
|
19
37
|
const setValue = (value) => {
|
|
@@ -47,9 +65,15 @@ const useState = (name, defaultValue = true) => {
|
|
|
47
65
|
* 订阅状态变化
|
|
48
66
|
* @param name 功能名
|
|
49
67
|
* @param callback 回调函数
|
|
68
|
+
* @throws {Error} - 如果 callback 无效,抛出错误。
|
|
50
69
|
*/
|
|
51
70
|
const onState = (name, callback) => {
|
|
52
71
|
if (typeof callback !== 'function') {
|
|
72
|
+
logger.error({
|
|
73
|
+
code: ResultCode.FailParams,
|
|
74
|
+
message: 'Callback must be a function',
|
|
75
|
+
data: null
|
|
76
|
+
});
|
|
53
77
|
throw new Error('Callback must be a function');
|
|
54
78
|
}
|
|
55
79
|
const sub = new StateSubscribe(name);
|
|
@@ -59,13 +83,31 @@ const onState = (name, callback) => {
|
|
|
59
83
|
* 废弃,请使用 onState
|
|
60
84
|
* @deprecated
|
|
61
85
|
*/
|
|
62
|
-
const eventState =
|
|
86
|
+
const eventState = (name, callback) => {
|
|
87
|
+
// 废弃警告
|
|
88
|
+
logger.warn({
|
|
89
|
+
code: ResultCode.Warn,
|
|
90
|
+
message: 'eventState is deprecated, please use onState',
|
|
91
|
+
data: null
|
|
92
|
+
});
|
|
93
|
+
return onState(name, callback);
|
|
94
|
+
};
|
|
63
95
|
/**
|
|
64
96
|
* 取消订阅状态变化
|
|
65
97
|
* @param name 功能名
|
|
66
98
|
* @param callback 回调函数
|
|
99
|
+
* @throws {Error} - 如果 callback 无效,抛出错误。
|
|
67
100
|
*/
|
|
68
101
|
const unState = (name, callback) => {
|
|
102
|
+
if (typeof callback !== 'function') {
|
|
103
|
+
logger.error({
|
|
104
|
+
code: ResultCode.FailParams,
|
|
105
|
+
message: 'Callback must be a function',
|
|
106
|
+
data: null
|
|
107
|
+
});
|
|
108
|
+
throw new Error('Callback must be a function');
|
|
109
|
+
}
|
|
110
|
+
// 取消订阅
|
|
69
111
|
const sub = new StateSubscribe(name);
|
|
70
112
|
sub.un(callback);
|
|
71
113
|
};
|
|
@@ -73,24 +115,14 @@ const unState = (name, callback) => {
|
|
|
73
115
|
* 废弃,请使用 unState
|
|
74
116
|
* @deprecated
|
|
75
117
|
*/
|
|
76
|
-
const unEventState =
|
|
77
|
-
//
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
// if (!hasChanged) {
|
|
86
|
-
// // 依赖未变化,返回缓存值
|
|
87
|
-
// return lastValue;
|
|
88
|
-
// }
|
|
89
|
-
// }
|
|
90
|
-
// // 依赖变化,重新计算
|
|
91
|
-
// lastValue = create();
|
|
92
|
-
// lastDependencies = dependencies;
|
|
93
|
-
// return lastValue;
|
|
94
|
-
// }
|
|
118
|
+
const unEventState = (name, callback) => {
|
|
119
|
+
// 废弃警告
|
|
120
|
+
logger.warn({
|
|
121
|
+
code: ResultCode.Warn,
|
|
122
|
+
message: 'unEventState is deprecated, please use unState',
|
|
123
|
+
data: null
|
|
124
|
+
});
|
|
125
|
+
return unState(name, callback);
|
|
126
|
+
};
|
|
95
127
|
|
|
96
128
|
export { eventState, onState, unEventState, unState, useState };
|
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import { EventKeys, Events } from '../typing/event/map.js';
|
|
2
2
|
import { Current } from '../typing/event/index.js';
|
|
3
|
+
import '../global.js';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* 使用订阅
|
|
6
7
|
* @param event
|
|
7
8
|
* @param option
|
|
8
|
-
* @
|
|
9
|
+
* @throws {Error} - 如果 event 不是对象,或者 select 不是字符串,抛出错误。
|
|
9
10
|
*/
|
|
10
11
|
declare const useSubscribe: <T extends EventKeys>(event: Events[T], select: T) => ((callback: Current<T>, keys: (keyof Events[T])[]) => void)[];
|
|
11
12
|
/**
|
|
@@ -1,12 +1,30 @@
|
|
|
1
|
+
import { ResultCode } from '../code.js';
|
|
1
2
|
import { SubscribeList } from './store.js';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* 使用订阅
|
|
5
6
|
* @param event
|
|
6
7
|
* @param option
|
|
7
|
-
* @
|
|
8
|
+
* @throws {Error} - 如果 event 不是对象,或者 select 不是字符串,抛出错误。
|
|
8
9
|
*/
|
|
9
10
|
const useSubscribe = (event, select) => {
|
|
11
|
+
// 检查参数
|
|
12
|
+
if (typeof event !== 'object') {
|
|
13
|
+
logger.error({
|
|
14
|
+
code: ResultCode.FailParams,
|
|
15
|
+
message: 'event is not object',
|
|
16
|
+
data: null
|
|
17
|
+
});
|
|
18
|
+
throw new Error('event is not object');
|
|
19
|
+
}
|
|
20
|
+
if (typeof select !== 'string') {
|
|
21
|
+
logger.error({
|
|
22
|
+
code: ResultCode.FailParams,
|
|
23
|
+
message: 'select is not string',
|
|
24
|
+
data: null
|
|
25
|
+
});
|
|
26
|
+
throw new Error('select is not string');
|
|
27
|
+
}
|
|
10
28
|
const run = (callback, keys, chioce) => {
|
|
11
29
|
const subList = new SubscribeList(chioce, select);
|
|
12
30
|
// 没有选择
|