alemonjs 2.1.53 → 2.1.55
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/define-router.d.ts +1 -0
- package/lib/app/define-router.js +6 -1
- package/lib/app/event-format.js +1 -1
- package/lib/app/event-processor-cycleFiles.js +2 -6
- package/lib/app/expose.d.ts +16 -0
- package/lib/app/expose.js +190 -0
- package/lib/app/index.d.ts +1 -0
- package/lib/app/index.js +2 -1
- package/lib/app/load_modules/loadChild.js +4 -0
- package/lib/app/store.d.ts +9 -9
- package/lib/app/store.js +2 -0
- package/lib/index.js +2 -1
- package/lib/types/event/index.d.ts +2 -0
- package/lib/types/expose/index.d.ts +41 -0
- package/lib/types/expose/index.js +1 -0
- package/lib/types/index.d.ts +1 -0
- package/package.json +1 -1
- package/lib/app/define/define-children.d.ts +0 -8
- package/lib/app/define/define-children.js +0 -25
- package/lib/app/define/define-middleware.d.ts +0 -2
- package/lib/app/define/define-middleware.js +0 -8
- package/lib/app/define/define-platform.d.ts +0 -13
- package/lib/app/define/define-platform.js +0 -40
- package/lib/app/define/define-response.d.ts +0 -2
- package/lib/app/define/define-response.js +0 -8
- package/lib/app/define/define-router.d.ts +0 -17
- package/lib/app/define/define-router.js +0 -34
- package/lib/app/event/event-group.d.ts +0 -9
- package/lib/app/event/event-group.js +0 -29
- package/lib/app/event/event-middleware.d.ts +0 -23
- package/lib/app/event/event-middleware.js +0 -44
- package/lib/app/event/event-response.d.ts +0 -16
- package/lib/app/event/event-response.js +0 -23
- package/lib/app/event/event-selects.d.ts +0 -13
- package/lib/app/event/event-selects.js +0 -15
- package/lib/app/event-processor/event-processor-callHandler.d.ts +0 -1
- package/lib/app/event-processor/event-processor-callHandler.js +0 -43
- package/lib/app/event-processor/event-processor-cycle.d.ts +0 -14
- package/lib/app/event-processor/event-processor-cycle.js +0 -97
- package/lib/app/event-processor/event-processor-cycleFiles.d.ts +0 -11
- package/lib/app/event-processor/event-processor-cycleFiles.js +0 -147
- package/lib/app/event-processor/event-processor-cycleRoute.d.ts +0 -10
- package/lib/app/event-processor/event-processor-cycleRoute.js +0 -133
- package/lib/app/event-processor/event-processor-event.d.ts +0 -24
- package/lib/app/event-processor/event-processor-event.js +0 -41
- package/lib/app/event-processor/event-processor-middleware.d.ts +0 -14
- package/lib/app/event-processor/event-processor-middleware.js +0 -31
- package/lib/app/event-processor/event-processor-subscribe.d.ts +0 -35
- package/lib/app/event-processor/event-processor-subscribe.js +0 -115
- package/lib/app/event-processor/event-processor.d.ts +0 -17
- package/lib/app/event-processor/event-processor.js +0 -207
- package/lib/app/hook-use/hook-use-api.d.ts +0 -6
- package/lib/app/hook-use/hook-use-api.js +0 -22
- package/lib/app/hook-use/hook-use-channel.d.ts +0 -8
- package/lib/app/hook-use/hook-use-channel.js +0 -33
- package/lib/app/hook-use/hook-use-client.d.ts +0 -7
- package/lib/app/hook-use/hook-use-client.js +0 -27
- package/lib/app/hook-use/hook-use-me.d.ts +0 -8
- package/lib/app/hook-use/hook-use-me.js +0 -45
- package/lib/app/hook-use/hook-use-menber.d.ts +0 -8
- package/lib/app/hook-use/hook-use-menber.js +0 -59
- package/lib/app/hook-use/hook-use-mention.d.ts +0 -23
- package/lib/app/hook-use/hook-use-mention.js +0 -105
- package/lib/app/hook-use/hook-use-message.d.ts +0 -29
- package/lib/app/hook-use/hook-use-message.js +0 -88
- package/lib/app/hook-use/hook-use-state.d.ts +0 -30
- package/lib/app/hook-use/hook-use-state.js +0 -101
- package/lib/app/hook-use/hook-use-subscribe.d.ts +0 -46
- package/lib/app/hook-use/hook-use-subscribe.js +0 -143
- package/lib/app/hook-use-api.d.ts +0 -268
- package/lib/app/hook-use-api.js +0 -1213
- package/lib/app/hook-use-state.d.ts +0 -3
- package/lib/app/hook-use-state.js +0 -68
- package/lib/app/hook-use-subscribe.d.ts +0 -73
- package/lib/app/hook-use-subscribe.js +0 -112
- package/lib/app/message/message-api.d.ts +0 -69
- package/lib/app/message/message-api.js +0 -105
- package/lib/app/message/message-format-old.d.ts +0 -143
- package/lib/app/message/message-format-old.js +0 -359
- package/lib/app/message/message-format.d.ts +0 -215
- package/lib/app/message/message-format.js +0 -382
- package/lib/app/message-controller.d.ts +0 -25
- package/lib/app/message-controller.js +0 -66
- package/lib/app/message-format.old.d.ts +0 -50
- package/lib/app/message-format.old.js +0 -217
- package/lib/cbp/processor/handle.d.ts +0 -3
- package/lib/cbp/processor/handle.js +0 -32
- package/lib/jsx/index.d.ts +0 -105
- package/lib/jsx/index.js +0 -211
- package/lib/jsx/jsx-dev-runtime.d.ts +0 -2
- package/lib/jsx/jsx-dev-runtime.js +0 -1
- package/lib/jsx/jsx-runtime.d.ts +0 -18
- package/lib/jsx/jsx-runtime.js +0 -24
- package/lib/process/client.js +0 -109
- package/lib/store/SinglyLinkedList.d.ts +0 -22
- package/lib/store/SinglyLinkedList.js +0 -97
- package/lib/store/store.d.ts +0 -127
- package/lib/store/store.js +0 -443
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import { ResultCode } from '../../core/variable.js';
|
|
2
|
-
import { createResult } from '../../core/utils.js';
|
|
3
|
-
import { sendAction } from '../../cbp/processor/actions.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* 使用提及。
|
|
7
|
-
* @param {Object} event - 事件对象,包含触发提及的相关信息。
|
|
8
|
-
* @throws {Error} - 如果 event 无效,抛出错误。
|
|
9
|
-
*/
|
|
10
|
-
const useMention = (event) => {
|
|
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
|
-
});
|
|
17
|
-
throw new Error('Invalid event: event must be an object');
|
|
18
|
-
}
|
|
19
|
-
// 提及数据缓存
|
|
20
|
-
let res = null;
|
|
21
|
-
/** 加载数据(带缓存) */
|
|
22
|
-
const load = async () => {
|
|
23
|
-
if (res) {
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
// 获取提及数据
|
|
27
|
-
const results = await sendAction({
|
|
28
|
-
action: 'mention.get',
|
|
29
|
-
payload: { event }
|
|
30
|
-
});
|
|
31
|
-
// 提及数据通常在 results 中,找到 code 为 Ok 的项目,并将其 data 作为提及数据缓存起来
|
|
32
|
-
const result = results.find(item => item.code === ResultCode.Ok);
|
|
33
|
-
if (result) {
|
|
34
|
-
res = result.data;
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
/** 按条件过滤,默认排除 bot */
|
|
38
|
-
const match = (item, options) => {
|
|
39
|
-
if (options.UserId !== undefined && item.UserId !== options.UserId) {
|
|
40
|
-
return false;
|
|
41
|
-
}
|
|
42
|
-
if (options.UserKey !== undefined && item.UserKey !== options.UserKey) {
|
|
43
|
-
return false;
|
|
44
|
-
}
|
|
45
|
-
if (options.UserName !== undefined && item.UserName !== options.UserName) {
|
|
46
|
-
return false;
|
|
47
|
-
}
|
|
48
|
-
if (options.IsMaster !== undefined && item.IsMaster !== options.IsMaster) {
|
|
49
|
-
return false;
|
|
50
|
-
}
|
|
51
|
-
if (options.IsBot !== undefined && item.IsBot !== options.IsBot) {
|
|
52
|
-
return false;
|
|
53
|
-
}
|
|
54
|
-
// 默认排除 bot
|
|
55
|
-
if (options.IsBot === undefined && item.IsBot) {
|
|
56
|
-
return false;
|
|
57
|
-
}
|
|
58
|
-
return true;
|
|
59
|
-
};
|
|
60
|
-
const mention = {
|
|
61
|
-
find: async (options = {}) => {
|
|
62
|
-
try {
|
|
63
|
-
await load();
|
|
64
|
-
}
|
|
65
|
-
catch (err) {
|
|
66
|
-
const result = createResult(ResultCode.Fail, err?.message || 'Failed to get mention data', null);
|
|
67
|
-
return {
|
|
68
|
-
...result,
|
|
69
|
-
count: 0
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
if (!Array.isArray(res)) {
|
|
73
|
-
return {
|
|
74
|
-
...createResult(ResultCode.Warn, 'No mention data found', null),
|
|
75
|
-
count: 0
|
|
76
|
-
};
|
|
77
|
-
}
|
|
78
|
-
const data = res.filter(item => match(item, options));
|
|
79
|
-
const result = createResult(ResultCode.Ok, 'Successfully retrieved mention data', data);
|
|
80
|
-
return {
|
|
81
|
-
...result,
|
|
82
|
-
count: data.length || 0
|
|
83
|
-
};
|
|
84
|
-
},
|
|
85
|
-
findOne: async (options = {}) => {
|
|
86
|
-
const results = await mention.find(options);
|
|
87
|
-
if (results.code !== ResultCode.Ok || !results.data?.length) {
|
|
88
|
-
const result = createResult(results.code, results.message, null);
|
|
89
|
-
return {
|
|
90
|
-
...result,
|
|
91
|
-
count: 0
|
|
92
|
-
};
|
|
93
|
-
}
|
|
94
|
-
const data = results?.data[0];
|
|
95
|
-
const result = createResult(ResultCode.Ok, results.message, data);
|
|
96
|
-
return {
|
|
97
|
-
...result,
|
|
98
|
-
count: results.data?.length || 0
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
};
|
|
102
|
-
return [mention];
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
export { useMention };
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { DataEnums, EventKeys, Events } from '../../types';
|
|
2
|
-
import { Result } from '../../core/utils';
|
|
3
|
-
import { Format } from '../message/message-format.js';
|
|
4
|
-
/**
|
|
5
|
-
* 消息处理
|
|
6
|
-
* @param event
|
|
7
|
-
* @returns
|
|
8
|
-
*/
|
|
9
|
-
export declare const useMessage: <T extends EventKeys>(event: Events[T]) => readonly [{
|
|
10
|
-
send(params?: {
|
|
11
|
-
format: Format | DataEnums[];
|
|
12
|
-
} | DataEnums[]): Promise<Result[]>;
|
|
13
|
-
}];
|
|
14
|
-
/**
|
|
15
|
-
* 废弃,请使用 useMessage
|
|
16
|
-
* @deprecated
|
|
17
|
-
* @param event
|
|
18
|
-
* @returns
|
|
19
|
-
*/
|
|
20
|
-
export declare const useSend: <T extends EventKeys>(event: Events[T]) => (...val: DataEnums[]) => Promise<Result[]>;
|
|
21
|
-
/**
|
|
22
|
-
* 废弃,请使用 useMessage
|
|
23
|
-
* @deprecated
|
|
24
|
-
* @param event
|
|
25
|
-
* @returns
|
|
26
|
-
*/
|
|
27
|
-
export declare const useSends: <T extends EventKeys>(event: Events[T]) => readonly [(params?: DataEnums[] | {
|
|
28
|
-
format: Format | DataEnums[];
|
|
29
|
-
}) => Promise<Result[]>];
|
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
import { ResultCode } from '../../core/variable.js';
|
|
2
|
-
import { createResult } from '../../core/utils.js';
|
|
3
|
-
import { sendAction } from '../../cbp/processor/actions.js';
|
|
4
|
-
import { Format } from '../message/message-format.js';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* 消息处理
|
|
8
|
-
* @param event
|
|
9
|
-
* @returns
|
|
10
|
-
*/
|
|
11
|
-
const useMessage = (event) => {
|
|
12
|
-
if (!event || typeof event !== 'object') {
|
|
13
|
-
logger.error({
|
|
14
|
-
code: ResultCode.FailParams,
|
|
15
|
-
message: 'Invalid event: event must be an object',
|
|
16
|
-
data: null
|
|
17
|
-
});
|
|
18
|
-
throw new Error('Invalid event: event must be an object');
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* 将 format 参数解析为 DataEnums[]
|
|
22
|
-
*/
|
|
23
|
-
const resolveFormat = (params) => {
|
|
24
|
-
if (params.format instanceof Format) {
|
|
25
|
-
return params.format.value;
|
|
26
|
-
}
|
|
27
|
-
return params.format;
|
|
28
|
-
};
|
|
29
|
-
/**
|
|
30
|
-
* 发送消息(内部方法,兼容旧API)
|
|
31
|
-
* @param val
|
|
32
|
-
* @returns
|
|
33
|
-
*/
|
|
34
|
-
const sendRaw = async (val) => {
|
|
35
|
-
if (!val || val.length === 0) {
|
|
36
|
-
return [createResult(ResultCode.FailParams, 'Invalid val: val must be a non-empty array', null)];
|
|
37
|
-
}
|
|
38
|
-
const result = await sendAction({
|
|
39
|
-
action: 'message.send',
|
|
40
|
-
payload: {
|
|
41
|
-
event,
|
|
42
|
-
params: {
|
|
43
|
-
format: val
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
return Array.isArray(result) ? result : [result];
|
|
48
|
-
};
|
|
49
|
-
// 新的消息处理接口
|
|
50
|
-
const lightweight = {
|
|
51
|
-
send(params) {
|
|
52
|
-
// send 直接走快速路径,无需创建完整 controller
|
|
53
|
-
if (!params) {
|
|
54
|
-
return sendRaw([]);
|
|
55
|
-
}
|
|
56
|
-
if (Array.isArray(params)) {
|
|
57
|
-
return sendRaw(params.length > 0 ? params : []);
|
|
58
|
-
}
|
|
59
|
-
return sendRaw(resolveFormat(params));
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
return [lightweight];
|
|
63
|
-
};
|
|
64
|
-
/**
|
|
65
|
-
* 废弃,请使用 useMessage
|
|
66
|
-
* @deprecated
|
|
67
|
-
* @param event
|
|
68
|
-
* @returns
|
|
69
|
-
*/
|
|
70
|
-
const useSend = (event) => {
|
|
71
|
-
const [message] = useMessage(event);
|
|
72
|
-
const send = (...val) => {
|
|
73
|
-
return message.send(val);
|
|
74
|
-
};
|
|
75
|
-
return send;
|
|
76
|
-
};
|
|
77
|
-
/**
|
|
78
|
-
* 废弃,请使用 useMessage
|
|
79
|
-
* @deprecated
|
|
80
|
-
* @param event
|
|
81
|
-
* @returns
|
|
82
|
-
*/
|
|
83
|
-
const useSends = (event) => {
|
|
84
|
-
const [message] = useMessage(event);
|
|
85
|
-
return [message.send];
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
export { useMessage, useSend, useSends };
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 获取指定功能是启动还是关闭
|
|
3
|
-
* ***
|
|
4
|
-
* 当有其他地方调用时,
|
|
5
|
-
* 默认值以第一次调用为准
|
|
6
|
-
* ***
|
|
7
|
-
* 功能名相同时,
|
|
8
|
-
* 将会同时改变,因为状态是全局的
|
|
9
|
-
* @param name 功能名
|
|
10
|
-
* @param defaultValue 默认值,默认为 true
|
|
11
|
-
* @deprecated 废弃。指令管理可直接配置禁用正则
|
|
12
|
-
* @throws {Error} - 如果 name 不是字符串,或者 defaultValue 不是布尔值,抛出错误。
|
|
13
|
-
*/
|
|
14
|
-
export declare const useState: <T extends string>(name: T, defaultValue?: boolean) => [boolean, (value: boolean) => void];
|
|
15
|
-
/**
|
|
16
|
-
* 订阅状态变化
|
|
17
|
-
* @param name 功能名
|
|
18
|
-
* @param callback 回调函数
|
|
19
|
-
* @deprecated 废弃。指令管理可直接配置禁用正则
|
|
20
|
-
* @throws {Error} - 如果 callback 无效,抛出错误。
|
|
21
|
-
*/
|
|
22
|
-
export declare const onState: <T extends string>(name: T, callback: (value: boolean) => void) => void;
|
|
23
|
-
/**
|
|
24
|
-
* 取消订阅状态变化
|
|
25
|
-
* @param name 功能名
|
|
26
|
-
* @param callback 回调函数
|
|
27
|
-
* @deprecated 废弃。指令管理可直接配置禁用正则
|
|
28
|
-
* @throws {Error} - 如果 callback 无效,抛出错误。
|
|
29
|
-
*/
|
|
30
|
-
export declare const unState: <T extends string>(name: T, callback: (value: boolean) => void) => void;
|
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
import { getConfig } from '../../core/config.js';
|
|
2
|
-
import { ResultCode } from '../../core/variable.js';
|
|
3
|
-
import { State, StateSubscribe } from '../../store/store.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* 获取指定功能是启动还是关闭
|
|
7
|
-
* ***
|
|
8
|
-
* 当有其他地方调用时,
|
|
9
|
-
* 默认值以第一次调用为准
|
|
10
|
-
* ***
|
|
11
|
-
* 功能名相同时,
|
|
12
|
-
* 将会同时改变,因为状态是全局的
|
|
13
|
-
* @param name 功能名
|
|
14
|
-
* @param defaultValue 默认值,默认为 true
|
|
15
|
-
* @deprecated 废弃。指令管理可直接配置禁用正则
|
|
16
|
-
* @throws {Error} - 如果 name 不是字符串,或者 defaultValue 不是布尔值,抛出错误。
|
|
17
|
-
*/
|
|
18
|
-
const useState = (name, defaultValue = true) => {
|
|
19
|
-
// 检查参数
|
|
20
|
-
if (typeof name !== 'string') {
|
|
21
|
-
logger.error({
|
|
22
|
-
code: ResultCode.FailParams,
|
|
23
|
-
message: 'Invalid name: name must be a string',
|
|
24
|
-
data: null
|
|
25
|
-
});
|
|
26
|
-
throw new Error('Invalid name: name must be a string');
|
|
27
|
-
}
|
|
28
|
-
if (typeof defaultValue !== 'boolean') {
|
|
29
|
-
logger.error({
|
|
30
|
-
code: ResultCode.FailParams,
|
|
31
|
-
message: 'Invalid defaultValue: defaultValue must be a boolean',
|
|
32
|
-
data: null
|
|
33
|
-
});
|
|
34
|
-
throw new Error('Invalid defaultValue: defaultValue must be a boolean');
|
|
35
|
-
}
|
|
36
|
-
const state = new State(name, defaultValue);
|
|
37
|
-
// 设置值的函数
|
|
38
|
-
const setValue = (value) => {
|
|
39
|
-
if (state.value === value) {
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
state.value = value;
|
|
43
|
-
// 更新config
|
|
44
|
-
const cfg = getConfig();
|
|
45
|
-
cfg.value.core ??= {};
|
|
46
|
-
cfg.value.core.state ??= [];
|
|
47
|
-
const cfgState = cfg.value.core.state;
|
|
48
|
-
const exists = cfgState.includes(name);
|
|
49
|
-
if (value && exists) {
|
|
50
|
-
// 启用:从禁用列表中移除
|
|
51
|
-
cfg.value.core.state = cfg.value.core.state.filter((i) => i !== name);
|
|
52
|
-
}
|
|
53
|
-
else if (!value && !exists) {
|
|
54
|
-
// 禁用:添加到禁用列表
|
|
55
|
-
cfg.value.core.state.push(name);
|
|
56
|
-
}
|
|
57
|
-
cfg.saveValue(cfg.value);
|
|
58
|
-
};
|
|
59
|
-
return [state.value, setValue];
|
|
60
|
-
};
|
|
61
|
-
/**
|
|
62
|
-
* 订阅状态变化
|
|
63
|
-
* @param name 功能名
|
|
64
|
-
* @param callback 回调函数
|
|
65
|
-
* @deprecated 废弃。指令管理可直接配置禁用正则
|
|
66
|
-
* @throws {Error} - 如果 callback 无效,抛出错误。
|
|
67
|
-
*/
|
|
68
|
-
const onState = (name, callback) => {
|
|
69
|
-
if (typeof callback !== 'function') {
|
|
70
|
-
logger.error({
|
|
71
|
-
code: ResultCode.FailParams,
|
|
72
|
-
message: 'Callback must be a function',
|
|
73
|
-
data: null
|
|
74
|
-
});
|
|
75
|
-
throw new Error('Callback must be a function');
|
|
76
|
-
}
|
|
77
|
-
const sub = new StateSubscribe(name);
|
|
78
|
-
sub.on(callback);
|
|
79
|
-
};
|
|
80
|
-
/**
|
|
81
|
-
* 取消订阅状态变化
|
|
82
|
-
* @param name 功能名
|
|
83
|
-
* @param callback 回调函数
|
|
84
|
-
* @deprecated 废弃。指令管理可直接配置禁用正则
|
|
85
|
-
* @throws {Error} - 如果 callback 无效,抛出错误。
|
|
86
|
-
*/
|
|
87
|
-
const unState = (name, callback) => {
|
|
88
|
-
if (typeof callback !== 'function') {
|
|
89
|
-
logger.error({
|
|
90
|
-
code: ResultCode.FailParams,
|
|
91
|
-
message: 'Callback must be a function',
|
|
92
|
-
data: null
|
|
93
|
-
});
|
|
94
|
-
throw new Error('Callback must be a function');
|
|
95
|
-
}
|
|
96
|
-
// 取消订阅
|
|
97
|
-
const sub = new StateSubscribe(name);
|
|
98
|
-
sub.un(callback);
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
export { onState, unState, useState };
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { EventCycleEnum, Current, Events, EventKeys } from '../../types';
|
|
2
|
-
/**
|
|
3
|
-
* 订阅事件
|
|
4
|
-
* @param event
|
|
5
|
-
* @param select
|
|
6
|
-
* @returns
|
|
7
|
-
*/
|
|
8
|
-
export declare const useSubscribe: <T extends EventKeys>(event: Events[T], selects: T | T[]) => readonly [{
|
|
9
|
-
create: (callback: Current<T>, keys: (keyof Events[T])[]) => {
|
|
10
|
-
selects: T[];
|
|
11
|
-
choose: EventCycleEnum;
|
|
12
|
-
id: string;
|
|
13
|
-
};
|
|
14
|
-
mount: (callback: Current<T>, keys: (keyof Events[T])[]) => {
|
|
15
|
-
selects: T[];
|
|
16
|
-
choose: EventCycleEnum;
|
|
17
|
-
id: string;
|
|
18
|
-
};
|
|
19
|
-
unmount: (callback: Current<T>, keys: (keyof Events[T])[]) => {
|
|
20
|
-
selects: T[];
|
|
21
|
-
choose: EventCycleEnum;
|
|
22
|
-
id: string;
|
|
23
|
-
};
|
|
24
|
-
cancel: (value: {
|
|
25
|
-
id: string;
|
|
26
|
-
selects: T[];
|
|
27
|
-
choose: EventCycleEnum;
|
|
28
|
-
}) => void;
|
|
29
|
-
}];
|
|
30
|
-
/**
|
|
31
|
-
* 使用观察者模式订阅事件
|
|
32
|
-
* @param event
|
|
33
|
-
* @param selects
|
|
34
|
-
* @returns
|
|
35
|
-
* 废弃,请使用 useSubscribe
|
|
36
|
-
* @deprecated
|
|
37
|
-
*/
|
|
38
|
-
export declare const useObserver: <T extends EventKeys>(event: Events[T], selects: T | T[]) => readonly [(callback: Current<T>, keys: (keyof Events[T])[]) => {
|
|
39
|
-
selects: T[];
|
|
40
|
-
choose: EventCycleEnum;
|
|
41
|
-
id: string;
|
|
42
|
-
}, (value: {
|
|
43
|
-
id: string;
|
|
44
|
-
selects: T[];
|
|
45
|
-
choose: EventCycleEnum;
|
|
46
|
-
}) => void];
|
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
import { ResultCode } from '../../core/variable.js';
|
|
2
|
-
import { SubscribeList } from '../../store/store.js';
|
|
3
|
-
import { SubscribeStatus } from '../config.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* 订阅事件
|
|
7
|
-
* @param event
|
|
8
|
-
* @param select
|
|
9
|
-
* @returns
|
|
10
|
-
*/
|
|
11
|
-
const useSubscribe = (event, selects) => {
|
|
12
|
-
// 检查参数
|
|
13
|
-
if (typeof event !== 'object') {
|
|
14
|
-
logger.error({
|
|
15
|
-
code: ResultCode.FailParams,
|
|
16
|
-
message: 'event is not object',
|
|
17
|
-
data: null
|
|
18
|
-
});
|
|
19
|
-
throw new Error('event is not object');
|
|
20
|
-
}
|
|
21
|
-
if (typeof selects !== 'string' && !Array.isArray(selects)) {
|
|
22
|
-
logger.error({
|
|
23
|
-
code: ResultCode.FailParams,
|
|
24
|
-
message: 'select is not string or array',
|
|
25
|
-
data: null
|
|
26
|
-
});
|
|
27
|
-
throw new Error('select is not string or array');
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* 运行订阅
|
|
31
|
-
* @param callback
|
|
32
|
-
* @param keys
|
|
33
|
-
* @param choose
|
|
34
|
-
* @returns
|
|
35
|
-
*/
|
|
36
|
-
const register = (callback, keys, choose) => {
|
|
37
|
-
const curSelects = Array.isArray(selects) ? selects : [selects];
|
|
38
|
-
// 分配id
|
|
39
|
-
const ID = Date.now().toString(36) + Math.random().toString(36).substring(2, 15);
|
|
40
|
-
// 没有选择任何 key,无法绑定订阅
|
|
41
|
-
if (keys.length === 0) {
|
|
42
|
-
logger.warn({
|
|
43
|
-
code: ResultCode.FailParams,
|
|
44
|
-
message: 'subscribe keys is empty',
|
|
45
|
-
data: null
|
|
46
|
-
});
|
|
47
|
-
return { selects: curSelects, choose, id: ID };
|
|
48
|
-
}
|
|
49
|
-
// 创建 订阅 列表
|
|
50
|
-
for (const select of curSelects) {
|
|
51
|
-
const subList = new SubscribeList(choose, select);
|
|
52
|
-
// 只能选择基础数据类型的key
|
|
53
|
-
const values = {};
|
|
54
|
-
for (const key of keys) {
|
|
55
|
-
if (typeof key === 'string' && (typeof event[key] === 'string' || typeof event[key] === 'number' || typeof event[key] === 'boolean')) {
|
|
56
|
-
values[key] = event[key];
|
|
57
|
-
}
|
|
58
|
-
else {
|
|
59
|
-
logger.warn({
|
|
60
|
-
code: ResultCode.FailParams,
|
|
61
|
-
message: `Invalid key: ${key?.toString()} must be a string, number or boolean`,
|
|
62
|
-
data: null
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
subList.value.append({
|
|
67
|
-
choose,
|
|
68
|
-
selects: curSelects,
|
|
69
|
-
keys: values,
|
|
70
|
-
current: callback,
|
|
71
|
-
status: SubscribeStatus.active,
|
|
72
|
-
id: ID
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
return {
|
|
76
|
-
selects: curSelects,
|
|
77
|
-
choose,
|
|
78
|
-
id: ID
|
|
79
|
-
};
|
|
80
|
-
};
|
|
81
|
-
/**
|
|
82
|
-
* res 创建时 运行订阅
|
|
83
|
-
* @param callback
|
|
84
|
-
* @param keys
|
|
85
|
-
*/
|
|
86
|
-
const create = (callback, keys) => {
|
|
87
|
-
return register(callback, keys, 'create');
|
|
88
|
-
};
|
|
89
|
-
/**
|
|
90
|
-
* res 挂载时 运行订阅
|
|
91
|
-
* @param callback
|
|
92
|
-
* @param keys
|
|
93
|
-
*/
|
|
94
|
-
const mountBefore = (callback, keys) => {
|
|
95
|
-
return register(callback, keys, 'mount');
|
|
96
|
-
};
|
|
97
|
-
/**
|
|
98
|
-
* res 卸载时 运行订阅
|
|
99
|
-
* @param callback
|
|
100
|
-
* @param keys
|
|
101
|
-
*/
|
|
102
|
-
const unmount = (callback, keys) => {
|
|
103
|
-
return register(callback, keys, 'unmount');
|
|
104
|
-
};
|
|
105
|
-
/**
|
|
106
|
-
* 清除订阅
|
|
107
|
-
* @param id
|
|
108
|
-
*/
|
|
109
|
-
const cancel = (value) => {
|
|
110
|
-
const selects = value.selects;
|
|
111
|
-
const ID = value.id; // 订阅的 ID
|
|
112
|
-
for (const select of selects) {
|
|
113
|
-
const subList = new SubscribeList(value.choose, select);
|
|
114
|
-
subList.value.forEach(node => {
|
|
115
|
-
if (node.data.id === ID) {
|
|
116
|
-
node.data.status = SubscribeStatus.paused; // 标记为已暂停
|
|
117
|
-
return true; // break
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
const subscribe = {
|
|
123
|
-
create,
|
|
124
|
-
mount: mountBefore,
|
|
125
|
-
unmount,
|
|
126
|
-
cancel
|
|
127
|
-
};
|
|
128
|
-
return [subscribe];
|
|
129
|
-
};
|
|
130
|
-
/**
|
|
131
|
-
* 使用观察者模式订阅事件
|
|
132
|
-
* @param event
|
|
133
|
-
* @param selects
|
|
134
|
-
* @returns
|
|
135
|
-
* 废弃,请使用 useSubscribe
|
|
136
|
-
* @deprecated
|
|
137
|
-
*/
|
|
138
|
-
const useObserver = (event, selects) => {
|
|
139
|
-
const [subscribe] = useSubscribe(event, selects);
|
|
140
|
-
return [subscribe.mount, subscribe.cancel];
|
|
141
|
-
};
|
|
142
|
-
|
|
143
|
-
export { useObserver, useSubscribe };
|