alemonjs 2.1.0-alpha.50 → 2.1.0-alpha.52
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/README.md +0 -88
- package/lib/{datastructure → app}/SinglyLinkedList.d.ts +2 -3
- package/lib/{datastructure → app}/SinglyLinkedList.js +14 -18
- package/lib/app/define-chidren.d.ts +2 -11
- package/lib/app/define-chidren.js +0 -6
- package/lib/app/define-middleware.d.ts +2 -5
- package/lib/app/define-response.d.ts +3 -11
- package/lib/app/define-response.js +0 -5
- package/lib/app/event-group.d.ts +1 -9
- package/lib/app/event-group.js +0 -7
- package/lib/app/event-middleware.d.ts +3 -27
- package/lib/app/event-middleware.js +0 -16
- package/lib/app/event-processor-callHandler.d.ts +1 -0
- package/lib/app/event-processor-callHandler.js +0 -3
- package/lib/app/event-processor-cycle.d.ts +2 -18
- package/lib/app/event-processor-cycle.js +0 -15
- package/lib/app/event-processor-cycleFiles.d.ts +2 -0
- package/lib/app/event-processor-cycleFiles.js +91 -0
- package/lib/app/event-processor-cycleRoute.d.ts +2 -0
- package/lib/app/{event-processor-event-route.js → event-processor-cycleRoute.js} +5 -34
- package/lib/app/event-processor-event.d.ts +2 -19
- package/lib/app/event-processor-event.js +9 -92
- package/lib/app/event-processor-middleware.d.ts +2 -19
- package/lib/app/event-processor-middleware.js +9 -89
- package/lib/app/event-processor-subscribe.d.ts +5 -40
- package/lib/app/event-processor-subscribe.js +4 -44
- package/lib/app/event-processor.d.ts +3 -20
- package/lib/app/event-processor.js +0 -46
- package/lib/app/event-response.d.ts +3 -19
- package/lib/app/event-response.js +0 -13
- package/lib/app/hook-use-api.d.ts +13 -73
- package/lib/app/hook-use-api.js +5 -168
- package/lib/app/hook-use-state.d.ts +3 -32
- package/lib/app/hook-use-state.js +0 -31
- package/lib/app/hook-use-subscribe.d.ts +3 -22
- package/lib/app/hook-use-subscribe.js +3 -48
- package/lib/app/index.d.ts +18 -0
- package/lib/app/index.js +19 -0
- package/lib/app/load_modules/index.d.ts +2 -0
- package/lib/app/load_modules/index.js +2 -0
- package/lib/app/load_modules/load.d.ts +2 -0
- package/lib/app/load_modules/load.js +34 -0
- package/lib/app/load_modules/loadChild.d.ts +2 -0
- package/lib/app/{load.js → load_modules/loadChild.js} +21 -38
- package/lib/app/message-api.d.ts +7 -43
- package/lib/app/message-api.js +1 -32
- package/lib/app/message-format.d.ts +10 -84
- package/lib/app/message-format.js +0 -149
- package/lib/app/store.d.ts +36 -63
- package/lib/app/store.js +45 -73
- package/lib/cbp/connects/client.d.ts +2 -0
- package/lib/cbp/{client.js → connects/client.js} +25 -33
- package/lib/cbp/connects/connect.d.ts +9 -0
- package/lib/cbp/{connect.js → connects/connect.js} +10 -9
- package/lib/cbp/connects/platform.d.ts +9 -0
- package/lib/cbp/{platform.js → connects/platform.js} +15 -43
- package/lib/cbp/index.d.ts +3 -0
- package/lib/cbp/index.js +3 -434
- package/lib/cbp/processor/actions.d.ts +3 -0
- package/lib/cbp/processor/actions.js +28 -0
- package/lib/cbp/processor/api.d.ts +3 -0
- package/lib/cbp/processor/api.js +28 -0
- package/lib/cbp/processor/config.d.ts +26 -0
- package/lib/cbp/{config.js → processor/config.js} +3 -19
- package/lib/cbp/processor/hello.html.d.ts +1 -0
- package/lib/cbp/routers/middleware.d.ts +2 -0
- package/lib/cbp/{router-middleware.js → routers/middleware.js} +2 -13
- package/lib/cbp/routers/router.d.ts +3 -0
- package/lib/cbp/{router.js → routers/router.js} +9 -21
- package/lib/cbp/routers/utils.d.ts +2 -0
- package/lib/cbp/{router-utils.js → routers/utils.js} +0 -1
- package/lib/cbp/server/main.d.ts +1 -0
- package/lib/cbp/server/main.js +418 -0
- package/lib/cbp/server/testone.d.ts +8 -0
- package/lib/cbp/{testone.js → server/testone.js} +22 -87
- package/lib/cbp/typings.d.ts +17 -9
- package/lib/cbp/typings.js +1 -0
- package/lib/client.d.ts +2 -0
- package/lib/client.js +77 -0
- package/lib/core/config.d.ts +4 -37
- package/lib/core/config.js +0 -40
- package/lib/core/index.d.ts +3 -0
- package/lib/core/index.js +3 -0
- package/lib/core/utils.d.ts +10 -62
- package/lib/core/utils.js +0 -50
- package/lib/core/variable.d.ts +22 -9
- package/lib/core/variable.js +7 -24
- package/lib/global.d.ts +1 -85
- package/lib/global.js +0 -7
- package/lib/index.d.ts +6 -53
- package/lib/index.js +10 -21
- package/lib/main.d.ts +2 -42
- package/lib/main.js +51 -121
- package/lib/process/index.d.ts +2 -0
- package/lib/process/index.js +2 -0
- package/lib/process/module.d.ts +1 -0
- package/lib/process/module.js +76 -0
- package/lib/process/platform.d.ts +1 -0
- package/lib/{adapter.js → process/platform.js} +7 -13
- package/lib/types/actions.d.ts +12 -14
- package/lib/types/actions.js +1 -0
- package/lib/types/apis.d.ts +1 -9
- package/lib/types/apis.js +1 -0
- package/lib/types/client/index.d.ts +6 -47
- package/lib/types/client/index.js +1 -0
- package/lib/types/cycle/index.d.ts +8 -35
- package/lib/types/cycle/index.js +1 -0
- package/lib/types/event/actions.d.ts +1 -3
- package/lib/types/event/actions.js +0 -34
- package/lib/types/event/base/expansion.d.ts +1 -3
- package/lib/types/event/base/expansion.js +1 -0
- package/lib/types/event/base/guild.d.ts +2 -13
- package/lib/types/event/base/guild.js +1 -0
- package/lib/types/event/base/message.d.ts +3 -20
- package/lib/types/event/base/message.js +1 -0
- package/lib/types/event/base/platform.d.ts +1 -12
- package/lib/types/event/base/platform.js +1 -0
- package/lib/types/event/base/user.d.ts +1 -27
- package/lib/types/event/base/user.js +1 -0
- package/lib/types/event/channal/index.d.ts +6 -9
- package/lib/types/event/channal/index.js +1 -0
- package/lib/types/event/guild/index.d.ts +6 -9
- package/lib/types/event/guild/index.js +1 -0
- package/lib/types/event/index.d.ts +30 -74
- package/lib/types/event/index.js +1 -1
- package/lib/types/event/interaction/index.d.ts +7 -10
- package/lib/types/event/interaction/index.js +1 -0
- package/lib/types/event/map.d.ts +13 -16
- package/lib/types/event/map.js +1 -0
- package/lib/types/event/member/index.d.ts +7 -10
- package/lib/types/event/member/index.js +1 -0
- package/lib/types/event/message/message.d.ts +10 -13
- package/lib/types/event/message/message.js +1 -0
- package/lib/types/event/message/private.message.d.ts +7 -10
- package/lib/types/event/message/private.message.js +1 -0
- package/lib/types/event/request/index.d.ts +6 -9
- package/lib/types/event/request/index.js +1 -0
- package/lib/types/index.d.ts +26 -0
- package/lib/types/index.js +1 -0
- package/lib/types/logger/index.d.ts +1 -41
- package/lib/types/logger/index.js +1 -0
- package/lib/types/message/ark.d.ts +6 -8
- package/lib/types/message/ark.js +1 -0
- package/lib/types/message/button.d.ts +4 -6
- package/lib/types/message/button.js +1 -0
- package/lib/types/message/image.d.ts +3 -14
- package/lib/types/message/image.js +1 -0
- package/lib/types/message/index.d.ts +16 -16
- package/lib/types/message/index.js +1 -0
- package/lib/types/message/link.d.ts +1 -6
- package/lib/types/message/link.js +1 -0
- package/lib/types/message/markdown.d.ts +18 -22
- package/lib/types/message/markdown.js +1 -0
- package/lib/types/message/mention.d.ts +3 -9
- package/lib/types/message/mention.js +1 -0
- package/lib/types/message/text.d.ts +1 -6
- package/lib/types/message/text.js +1 -0
- package/lib/types/package/index.d.ts +1 -3
- package/lib/types/package/index.js +1 -0
- package/lib/types/run.d.ts +13 -0
- package/lib/types/run.js +1 -0
- package/lib/types/state/index.d.ts +1 -3
- package/lib/types/state/index.js +1 -0
- package/lib/types/store/res.d.ts +11 -47
- package/lib/types/store/res.js +1 -0
- package/lib/types/subscribe/index.d.ts +7 -16
- package/lib/types/subscribe/index.js +1 -0
- package/lib/utils.d.ts +6 -56
- package/lib/utils.js +0 -49
- package/package.json +3 -3
- package/lib/app/event-processor-event-callHandler.js +0 -75
- package/lib/app/event-processor-middleware-callHandler.js +0 -69
- package/lib/app/event-processor-middleware-route.js +0 -103
- package/lib/app/load.d.ts +0 -14
- package/lib/cbp/actions.js +0 -48
- package/lib/cbp/api.js +0 -48
- package/lib/cbp/client.d.ts +0 -10
- package/lib/cbp/connect.d.ts +0 -27
- package/lib/cbp/platform.d.ts +0 -18
- package/lib/cbp/server.d.ts +0 -8
- package/lib/cbp/server.js +0 -449
- package/lib/core/code.d.ts +0 -16
- package/lib/core/code.js +0 -17
- package/lib/typing/actions.d.ts +0 -93
- package/lib/typing/apis.d.ts +0 -18
- package/lib/typing/client/index.d.ts +0 -67
- package/lib/typing/cycle/index.d.ts +0 -42
- package/lib/typing/event/actions.d.ts +0 -37
- package/lib/typing/event/actions.js +0 -72
- package/lib/typing/event/base/expansion.d.ts +0 -5
- package/lib/typing/event/base/guild.d.ts +0 -18
- package/lib/typing/event/base/message.d.ts +0 -28
- package/lib/typing/event/base/platform.d.ts +0 -16
- package/lib/typing/event/base/user.d.ts +0 -34
- package/lib/typing/event/channal/index.d.ts +0 -13
- package/lib/typing/event/guild/index.d.ts +0 -13
- package/lib/typing/event/index.d.ts +0 -94
- package/lib/typing/event/interaction/index.d.ts +0 -14
- package/lib/typing/event/map.d.ts +0 -36
- package/lib/typing/event/map.js +0 -20
- package/lib/typing/event/member/index.d.ts +0 -14
- package/lib/typing/event/message/message.d.ts +0 -23
- package/lib/typing/event/message/private.message.d.ts +0 -16
- package/lib/typing/event/request/index.d.ts +0 -13
- package/lib/typing/logger/index.d.ts +0 -49
- package/lib/typing/message/ark.d.ts +0 -46
- package/lib/typing/message/button.d.ts +0 -32
- package/lib/typing/message/image.d.ts +0 -23
- package/lib/typing/message/index.d.ts +0 -16
- package/lib/typing/message/link.d.ts +0 -12
- package/lib/typing/message/markdown.d.ts +0 -91
- package/lib/typing/message/mention.d.ts +0 -16
- package/lib/typing/message/text.d.ts +0 -12
- package/lib/typing/package/index.d.ts +0 -12
- package/lib/typing/state/index.d.ts +0 -5
- package/lib/typing/store/res.d.ts +0 -75
- package/lib/typing/subscribe/index.d.ts +0 -30
- /package/lib/cbp/{hello.html.js → processor/hello.html.js} +0 -0
package/README.md
CHANGED
|
@@ -3,91 +3,3 @@
|
|
|
3
3
|
[alemonjs](https://alemonjs.com) 是一个专门用于开发聊天机器人的 Node.js 框架,它提供了完整的事件驱动架构来处理各种聊天平台的消息和交互。
|
|
4
4
|
|
|
5
5
|
https://alemonjs.com
|
|
6
|
-
|
|
7
|
-
## 🏗️ 架构设计特点
|
|
8
|
-
|
|
9
|
-
1. 事件驱动架构
|
|
10
|
-
|
|
11
|
-
基于事件选择器(onSelects)和响应处理器(onResponse)的模式
|
|
12
|
-
|
|
13
|
-
支持多种事件类型(如 message.create、private.message.create 等)
|
|
14
|
-
|
|
15
|
-
事件处理包含完整的生命周期:创建、挂载、卸载
|
|
16
|
-
|
|
17
|
-
2. CBP 通信协议
|
|
18
|
-
|
|
19
|
-
实现了自定义的 WebSocket 通信协议
|
|
20
|
-
|
|
21
|
-
支持客户端-服务器架构,可以作为服务器运行也可以连接到远程服务器
|
|
22
|
-
|
|
23
|
-
包含心跳机制、重连机制、消息去重等可靠性保障
|
|
24
|
-
|
|
25
|
-
3. 模块化插件系统
|
|
26
|
-
|
|
27
|
-
支持动态加载应用模块(apps)和响应模块(response)
|
|
28
|
-
|
|
29
|
-
提供中间件机制用于消息预处理
|
|
30
|
-
|
|
31
|
-
状态管理系统,支持模块的启用/禁用
|
|
32
|
-
|
|
33
|
-
4. 多平台适配器
|
|
34
|
-
|
|
35
|
-
通过环境变量动态加载不同的聊天平台适配器
|
|
36
|
-
|
|
37
|
-
支持统一的消息格式处理(文本、图片、按钮、卡片等)
|
|
38
|
-
|
|
39
|
-
## 🔧 核心组件分析
|
|
40
|
-
|
|
41
|
-
5. 事件处理器
|
|
42
|
-
|
|
43
|
-
实现消息去重机制(防止重复处理同一消息)
|
|
44
|
-
|
|
45
|
-
用户操作频率限制(防止恶意刷屏)
|
|
46
|
-
|
|
47
|
-
自动清理过期的消息记录
|
|
48
|
-
|
|
49
|
-
6. Hook 系统
|
|
50
|
-
|
|
51
|
-
类似 React Hooks 的设计模式
|
|
52
|
-
|
|
53
|
-
提供 useMessage、useMention、useState 等常用 Hook
|
|
54
|
-
|
|
55
|
-
简化了消息处理的复杂性
|
|
56
|
-
|
|
57
|
-
7. 消息格式化系统
|
|
58
|
-
|
|
59
|
-
统一的消息格式定义
|
|
60
|
-
|
|
61
|
-
支持文本、图片、按钮、Markdown、ARK 卡片等多种消息类型
|
|
62
|
-
|
|
63
|
-
通过 format() 函数进行消息格式化
|
|
64
|
-
|
|
65
|
-
8. 配置管理系统
|
|
66
|
-
|
|
67
|
-
支持命令行参数、配置文件、环境变量多层配置
|
|
68
|
-
|
|
69
|
-
动态配置加载和热更新
|
|
70
|
-
|
|
71
|
-
平台特定的配置管理
|
|
72
|
-
|
|
73
|
-
## 💡 设计优势
|
|
74
|
-
|
|
75
|
-
高度模块化:每个功能都是独立的模块,便于维护和扩展
|
|
76
|
-
|
|
77
|
-
TypeScript 全支持:完整的类型定义,提供良好的开发体验
|
|
78
|
-
|
|
79
|
-
事件驱动:响应式编程模型,代码逻辑清晰
|
|
80
|
-
|
|
81
|
-
可扩展性强:支持自定义中间件、Hook 和平台适配器
|
|
82
|
-
|
|
83
|
-
性能优化:包含消息去重、频率限制、内存清理等优化机制
|
|
84
|
-
|
|
85
|
-
## 🔄 工作流程
|
|
86
|
-
|
|
87
|
-
启动阶段:加载配置 → 启动 CBP 服务器 → 连接平台适配器
|
|
88
|
-
|
|
89
|
-
消息接收:平台消息 → CBP 协议转换 → 事件分发
|
|
90
|
-
|
|
91
|
-
消息处理:事件选择 → 中间件处理 → 响应函数执行
|
|
92
|
-
|
|
93
|
-
消息发送:格式化消息 → CBP 协议传输 → 平台发送
|
|
@@ -3,7 +3,7 @@ declare class ListNode<T> {
|
|
|
3
3
|
next: ListNode<T> | null;
|
|
4
4
|
constructor(data: T);
|
|
5
5
|
}
|
|
6
|
-
declare class SinglyLinkedList<T> {
|
|
6
|
+
export declare class SinglyLinkedList<T> {
|
|
7
7
|
private head;
|
|
8
8
|
private size;
|
|
9
9
|
private current;
|
|
@@ -13,5 +13,4 @@ declare class SinglyLinkedList<T> {
|
|
|
13
13
|
removeCurrent(): void;
|
|
14
14
|
getSize(): number;
|
|
15
15
|
}
|
|
16
|
-
|
|
17
|
-
export { SinglyLinkedList };
|
|
16
|
+
export {};
|
|
@@ -1,24 +1,23 @@
|
|
|
1
1
|
class ListNode {
|
|
2
|
-
data;
|
|
3
|
-
next;
|
|
2
|
+
data;
|
|
3
|
+
next;
|
|
4
4
|
constructor(data) {
|
|
5
5
|
this.data = data;
|
|
6
6
|
this.next = null;
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
9
|
class SinglyLinkedList {
|
|
10
|
-
head;
|
|
11
|
-
size;
|
|
12
|
-
current;
|
|
10
|
+
head;
|
|
11
|
+
size;
|
|
12
|
+
current;
|
|
13
13
|
constructor(initialValues) {
|
|
14
14
|
this.head = null;
|
|
15
15
|
this.size = 0;
|
|
16
|
-
this.current = null;
|
|
16
|
+
this.current = null;
|
|
17
17
|
if (initialValues) {
|
|
18
|
-
initialValues.forEach(value => this.append(value));
|
|
18
|
+
initialValues.forEach(value => this.append(value));
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
|
-
// 在链表末尾添加新节点
|
|
22
21
|
append(data) {
|
|
23
22
|
const newNode = new ListNode(data);
|
|
24
23
|
if (!this.head) {
|
|
@@ -33,24 +32,22 @@ class SinglyLinkedList {
|
|
|
33
32
|
}
|
|
34
33
|
this.size++;
|
|
35
34
|
}
|
|
36
|
-
// 获取下一个节点并移动指针
|
|
37
35
|
popNext() {
|
|
38
36
|
if (!this.current) {
|
|
39
|
-
this.current = this.head;
|
|
37
|
+
this.current = this.head;
|
|
40
38
|
}
|
|
41
39
|
else {
|
|
42
|
-
this.current = this.current.next;
|
|
40
|
+
this.current = this.current.next;
|
|
43
41
|
}
|
|
44
|
-
return this.current;
|
|
42
|
+
return this.current;
|
|
45
43
|
}
|
|
46
|
-
// 删除当前节点
|
|
47
44
|
removeCurrent() {
|
|
48
45
|
if (!this.head) {
|
|
49
46
|
return;
|
|
50
47
|
}
|
|
51
48
|
if (this.current === this.head) {
|
|
52
|
-
this.head = this.head.next;
|
|
53
|
-
this.current = null;
|
|
49
|
+
this.head = this.head.next;
|
|
50
|
+
this.current = null;
|
|
54
51
|
this.size--;
|
|
55
52
|
return;
|
|
56
53
|
}
|
|
@@ -59,12 +56,11 @@ class SinglyLinkedList {
|
|
|
59
56
|
previous = previous.next;
|
|
60
57
|
}
|
|
61
58
|
if (previous && this.current) {
|
|
62
|
-
previous.next = this.current.next;
|
|
63
|
-
this.current = null;
|
|
59
|
+
previous.next = this.current.next;
|
|
60
|
+
this.current = null;
|
|
64
61
|
this.size--;
|
|
65
62
|
}
|
|
66
63
|
}
|
|
67
|
-
// 获取链表的大小
|
|
68
64
|
getSize() {
|
|
69
65
|
return this.size;
|
|
70
66
|
}
|
|
@@ -1,11 +1,2 @@
|
|
|
1
|
-
import { DefineChildrenFunc } from '../types
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 定义子事件
|
|
5
|
-
* @param callback
|
|
6
|
-
* @throws {Error} - 如果 callback 无效,抛出错误。
|
|
7
|
-
* @returns
|
|
8
|
-
*/
|
|
9
|
-
declare const defineChildren: DefineChildrenFunc;
|
|
10
|
-
|
|
11
|
-
export { defineChildren };
|
|
1
|
+
import { DefineChildrenFunc } from '../types';
|
|
2
|
+
export declare const defineChildren: DefineChildrenFunc;
|
|
@@ -1,11 +1,5 @@
|
|
|
1
1
|
import { ResultCode } from '../core/variable.js';
|
|
2
2
|
|
|
3
|
-
/**
|
|
4
|
-
* 定义子事件
|
|
5
|
-
* @param callback
|
|
6
|
-
* @throws {Error} - 如果 callback 无效,抛出错误。
|
|
7
|
-
* @returns
|
|
8
|
-
*/
|
|
9
3
|
const defineChildren = callback => {
|
|
10
4
|
if (typeof callback === 'function' || typeof callback === 'object') {
|
|
11
5
|
return {
|
|
@@ -1,5 +1,2 @@
|
|
|
1
|
-
import { defineMiddlewareFunc } from '../types
|
|
2
|
-
|
|
3
|
-
declare const defineMiddleware: defineMiddlewareFunc;
|
|
4
|
-
|
|
5
|
-
export { defineMiddleware };
|
|
1
|
+
import { defineMiddlewareFunc } from '../types';
|
|
2
|
+
export declare const defineMiddleware: defineMiddlewareFunc;
|
|
@@ -1,11 +1,3 @@
|
|
|
1
|
-
import { DefineResponseFunc } from '../types
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* Lazy load a response handler
|
|
5
|
-
* @param fnc
|
|
6
|
-
* @returns
|
|
7
|
-
*/
|
|
8
|
-
declare const lazy: (fnc: () => Promise<any>) => () => Promise<any>;
|
|
9
|
-
declare const defineResponse: DefineResponseFunc;
|
|
10
|
-
|
|
11
|
-
export { defineResponse, lazy };
|
|
1
|
+
import { DefineResponseFunc } from '../types';
|
|
2
|
+
export declare const lazy: (fnc: () => Promise<any>) => () => Promise<any>;
|
|
3
|
+
export declare const defineResponse: DefineResponseFunc;
|
package/lib/app/event-group.d.ts
CHANGED
package/lib/app/event-group.js
CHANGED
|
@@ -1,16 +1,9 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 定义一组标准执行导出
|
|
3
|
-
* @param calls
|
|
4
|
-
* @deprecated 废弃
|
|
5
|
-
* @returns
|
|
6
|
-
*/
|
|
7
1
|
const onGroup = (...calls) => {
|
|
8
2
|
if (calls.length === 0) {
|
|
9
3
|
throw new Error('onGroup: 至少需要一个响应或中间件');
|
|
10
4
|
}
|
|
11
5
|
const firstItem = calls[0];
|
|
12
6
|
const baseSelects = firstItem.select;
|
|
13
|
-
// 把如果item是数组的,豆扁平起来。
|
|
14
7
|
const currents = calls.reduce((acc, item) => {
|
|
15
8
|
if (Array.isArray(item.current)) {
|
|
16
9
|
return acc.concat(item.current);
|
|
@@ -1,27 +1,3 @@
|
|
|
1
|
-
import { OnMiddlewareReversalFunc, OnMiddlewareReversalFuncBack } from '../types
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
* @fileoverview 中间件
|
|
5
|
-
* @module middleware
|
|
6
|
-
* @author ningmengchongshui
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* 中间件
|
|
11
|
-
* @param select 事件选择
|
|
12
|
-
* @param callback 回调函数,处理事件和 API
|
|
13
|
-
* @throws {Error} - 如果 select 无效,抛出错误。
|
|
14
|
-
* @throws {Error} - 如果 callback 无效,抛出错误。
|
|
15
|
-
* @returns
|
|
16
|
-
*/
|
|
17
|
-
declare const onMiddleware: OnMiddlewareReversalFunc;
|
|
18
|
-
/**
|
|
19
|
-
* 废弃,请使用 onMiddleware
|
|
20
|
-
* @deprecated
|
|
21
|
-
* @param select
|
|
22
|
-
* @param callback
|
|
23
|
-
* @returns
|
|
24
|
-
*/
|
|
25
|
-
declare const OnMiddleware: OnMiddlewareReversalFuncBack;
|
|
26
|
-
|
|
27
|
-
export { OnMiddleware, onMiddleware };
|
|
1
|
+
import { OnMiddlewareReversalFunc, OnMiddlewareReversalFuncBack } from '../types';
|
|
2
|
+
export declare const onMiddleware: OnMiddlewareReversalFunc;
|
|
3
|
+
export declare const OnMiddleware: OnMiddlewareReversalFuncBack;
|
|
@@ -1,15 +1,6 @@
|
|
|
1
1
|
import { ResultCode } from '../core/variable.js';
|
|
2
2
|
|
|
3
|
-
/**
|
|
4
|
-
* 中间件
|
|
5
|
-
* @param select 事件选择
|
|
6
|
-
* @param callback 回调函数,处理事件和 API
|
|
7
|
-
* @throws {Error} - 如果 select 无效,抛出错误。
|
|
8
|
-
* @throws {Error} - 如果 callback 无效,抛出错误。
|
|
9
|
-
* @returns
|
|
10
|
-
*/
|
|
11
3
|
const onMiddleware = (select, callback) => {
|
|
12
|
-
// 参数检查
|
|
13
4
|
if (typeof callback !== 'function') {
|
|
14
5
|
logger.error({
|
|
15
6
|
code: ResultCode.FailParams,
|
|
@@ -29,13 +20,6 @@ const onMiddleware = (select, callback) => {
|
|
|
29
20
|
throw new Error('Invalid select: select must be a string or object');
|
|
30
21
|
};
|
|
31
22
|
global.onMiddleware = onMiddleware;
|
|
32
|
-
/**
|
|
33
|
-
* 废弃,请使用 onMiddleware
|
|
34
|
-
* @deprecated
|
|
35
|
-
* @param select
|
|
36
|
-
* @param callback
|
|
37
|
-
* @returns
|
|
38
|
-
*/
|
|
39
23
|
const OnMiddleware = (callback, select) => {
|
|
40
24
|
return onMiddleware(select, callback);
|
|
41
25
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const createCallHandler: (valueEvent: any) => (currents: any, nextEvent: any) => void;
|
|
@@ -7,7 +7,6 @@ import { useMessage } from './hook-use-api.js';
|
|
|
7
7
|
|
|
8
8
|
const createCallHandler = valueEvent => {
|
|
9
9
|
const [message] = useMessage(valueEvent);
|
|
10
|
-
// 开始处理 heandler
|
|
11
10
|
const callHandler = (currents, nextEvent) => {
|
|
12
11
|
let index = 0;
|
|
13
12
|
let isClose = false;
|
|
@@ -19,14 +18,12 @@ const createCallHandler = valueEvent => {
|
|
|
19
18
|
}
|
|
20
19
|
if (Array.isArray(res)) {
|
|
21
20
|
if (res.length > 0) {
|
|
22
|
-
// 发送数据
|
|
23
21
|
void message.send(res);
|
|
24
22
|
}
|
|
25
23
|
isClose = true;
|
|
26
24
|
}
|
|
27
25
|
else if (typeof res === 'object') {
|
|
28
26
|
if (Array.isArray(res.data)) {
|
|
29
|
-
// 发送数据
|
|
30
27
|
void message.send(res.data);
|
|
31
28
|
}
|
|
32
29
|
if (!res.allowGrouping) {
|
|
@@ -1,18 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @fileoverview 消息处理快
|
|
5
|
-
* 登录模块向核心模块发送数据
|
|
6
|
-
* 核心模块调用模块索引
|
|
7
|
-
* @module processor
|
|
8
|
-
* @author ningmengchongshui
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* 消息体处理机制
|
|
13
|
-
* @param event
|
|
14
|
-
* @param key
|
|
15
|
-
*/
|
|
16
|
-
declare const expendCycle: <T extends EventKeys>(valueEvent: Events[T], select: T) => void;
|
|
17
|
-
|
|
18
|
-
export { expendCycle };
|
|
1
|
+
import { Events, EventKeys } from '../types';
|
|
2
|
+
export declare const expendCycle: <T extends EventKeys>(valueEvent: Events[T], select: T) => void;
|
|
@@ -4,11 +4,6 @@ import { expendMiddleware } from './event-processor-middleware.js';
|
|
|
4
4
|
import { expendSubscribeCreate, expendSubscribeMount, expendSubscribeUnmount } from './event-processor-subscribe.js';
|
|
5
5
|
import { getConfigValue } from '../core/config.js';
|
|
6
6
|
|
|
7
|
-
/**
|
|
8
|
-
* 打印日志
|
|
9
|
-
* @param event
|
|
10
|
-
* @param select
|
|
11
|
-
*/
|
|
12
7
|
const showLog = (event, select) => {
|
|
13
8
|
if (process.env.NODE_ENV === 'development') {
|
|
14
9
|
const log = {
|
|
@@ -51,14 +46,8 @@ const showLog = (event, select) => {
|
|
|
51
46
|
logger.info(baseLog);
|
|
52
47
|
}
|
|
53
48
|
};
|
|
54
|
-
/**
|
|
55
|
-
* 消息体处理机制
|
|
56
|
-
* @param event
|
|
57
|
-
* @param key
|
|
58
|
-
*/
|
|
59
49
|
const expendCycle = (valueEvent, select) => {
|
|
60
50
|
const nextEnd = () => { };
|
|
61
|
-
// unmount
|
|
62
51
|
const nextUnMount = (cn, ...cns) => {
|
|
63
52
|
if (cn) {
|
|
64
53
|
nextEnd(...cns);
|
|
@@ -66,7 +55,6 @@ const expendCycle = (valueEvent, select) => {
|
|
|
66
55
|
}
|
|
67
56
|
void expendSubscribeUnmount(valueEvent, select, nextEnd);
|
|
68
57
|
};
|
|
69
|
-
// event
|
|
70
58
|
const nextEvent = (cn, ...cns) => {
|
|
71
59
|
if (cn) {
|
|
72
60
|
nextUnMount(...cns);
|
|
@@ -74,7 +62,6 @@ const expendCycle = (valueEvent, select) => {
|
|
|
74
62
|
}
|
|
75
63
|
void expendEvent(valueEvent, select, nextUnMount);
|
|
76
64
|
};
|
|
77
|
-
// mount
|
|
78
65
|
const nextMount = (cn, ...cns) => {
|
|
79
66
|
if (cn) {
|
|
80
67
|
nextEvent(...cns);
|
|
@@ -82,7 +69,6 @@ const expendCycle = (valueEvent, select) => {
|
|
|
82
69
|
}
|
|
83
70
|
void expendSubscribeMount(valueEvent, select, nextEvent);
|
|
84
71
|
};
|
|
85
|
-
// middleware
|
|
86
72
|
const nextCreate = (cn, ...cns) => {
|
|
87
73
|
if (cn) {
|
|
88
74
|
nextMount(...cns);
|
|
@@ -100,7 +86,6 @@ const expendCycle = (valueEvent, select) => {
|
|
|
100
86
|
else {
|
|
101
87
|
showLog(valueEvent, select);
|
|
102
88
|
}
|
|
103
|
-
// create
|
|
104
89
|
void expendSubscribeCreate(valueEvent, select, nextCreate);
|
|
105
90
|
};
|
|
106
91
|
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { useState } from './hook-use-state.js';
|
|
2
|
+
import { showErrorModule } from '../core/utils.js';
|
|
3
|
+
import { EventMessageText } from '../core/variable.js';
|
|
4
|
+
import { ResponseMiddleware } from './store.js';
|
|
5
|
+
|
|
6
|
+
const callHandlerFile = async (valueEvent, select, file, nextStep, callback) => {
|
|
7
|
+
try {
|
|
8
|
+
const app = await import(`file://${file.path}`);
|
|
9
|
+
if (!app?.default?.current || !app?.default?.select) {
|
|
10
|
+
nextStep();
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
if (file?.stateKey) {
|
|
14
|
+
const [state] = useState(file?.stateKey);
|
|
15
|
+
if (state === false) {
|
|
16
|
+
nextStep();
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
if (EventMessageText.includes(select)) {
|
|
21
|
+
if (app?.regular) {
|
|
22
|
+
const reg = new RegExp(app.regular);
|
|
23
|
+
if (!reg.test(valueEvent['MessageText'])) {
|
|
24
|
+
nextStep();
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const selects = Array.isArray(app.default.select) ? app.default.select : [app.default.select];
|
|
30
|
+
if (!selects.includes(select)) {
|
|
31
|
+
nextStep();
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
callback(app);
|
|
35
|
+
}
|
|
36
|
+
catch (err) {
|
|
37
|
+
showErrorModule(err);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
const createNextStep = (valueEvent, select, next, files, callHandler) => {
|
|
41
|
+
let valueI = 0;
|
|
42
|
+
const recordCloseMw = new Set();
|
|
43
|
+
const nextStep = (cn, ...cns) => {
|
|
44
|
+
if (cn) {
|
|
45
|
+
next(...cns);
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
if (valueI >= files.length) {
|
|
49
|
+
next();
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
void calli();
|
|
53
|
+
};
|
|
54
|
+
const calli = async () => {
|
|
55
|
+
if (valueI >= files.length) {
|
|
56
|
+
nextStep();
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
valueI++;
|
|
60
|
+
const file = files[valueI - 1];
|
|
61
|
+
if (!file?.path) {
|
|
62
|
+
nextStep();
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const responseMiddleware = new ResponseMiddleware();
|
|
66
|
+
const currentsAndMiddleware = responseMiddleware.find(file.appName, file.stateKey);
|
|
67
|
+
const currents = [];
|
|
68
|
+
for (const cm of [...currentsAndMiddleware, file]) {
|
|
69
|
+
let isBreak = false;
|
|
70
|
+
if (recordCloseMw.has(cm.stateKey)) {
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const close = () => {
|
|
74
|
+
isBreak = true;
|
|
75
|
+
recordCloseMw.add(cm.stateKey);
|
|
76
|
+
};
|
|
77
|
+
await callHandlerFile(valueEvent, select, cm, close, app => {
|
|
78
|
+
const currentsItem = Array.isArray(app.default.current) ? app.default.current : [app.default.current];
|
|
79
|
+
currents.push(...currentsItem);
|
|
80
|
+
});
|
|
81
|
+
if (isBreak) {
|
|
82
|
+
nextStep();
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
callHandler(currents, nextStep);
|
|
87
|
+
};
|
|
88
|
+
return nextStep;
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export { createNextStep };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { Next, Events, EventKeys, ResponseRoute } from '../types';
|
|
2
|
+
export declare const createRouteProcessChildren: <T extends EventKeys>(valueEvent: Events[T], select: T, nextCycle: Next, callHandler: (currents: any, nextEvent: any) => void) => (nodes: ResponseRoute[], middleware: any, next: () => Promise<void> | void) => void;
|
|
@@ -1,35 +1,21 @@
|
|
|
1
1
|
import { isAsyncFunction } from 'util/types';
|
|
2
|
-
import { ResponseRouter } from './store.js';
|
|
3
2
|
import { EventMessageText } from '../core/variable.js';
|
|
4
3
|
import 'fs';
|
|
5
4
|
import 'path';
|
|
6
5
|
import 'yaml';
|
|
7
6
|
import { showErrorModule } from '../core/utils.js';
|
|
8
|
-
import { createCallHandler } from './event-processor-callHandler.js';
|
|
9
7
|
|
|
10
|
-
|
|
11
|
-
* @fileoverview 消息处理快
|
|
12
|
-
* 登录模块向核心模块发送数据
|
|
13
|
-
* 核心模块调用模块索引
|
|
14
|
-
* @module processor
|
|
15
|
-
* @author ningmengchongshui
|
|
16
|
-
*/
|
|
17
|
-
const expendEventRoute = (valueEvent, select, nextCycle) => {
|
|
18
|
-
const resRoute = new ResponseRouter();
|
|
19
|
-
const routes = resRoute.value;
|
|
20
|
-
// 开始处理 heandler
|
|
21
|
-
const callHandler = createCallHandler(valueEvent);
|
|
22
|
-
// 开始处理 handler
|
|
8
|
+
const createRouteProcessChildren = (valueEvent, select, nextCycle, callHandler) => {
|
|
23
9
|
const processChildren = (nodes, middleware, next) => {
|
|
24
10
|
if (!nodes || nodes.length === 0) {
|
|
25
|
-
next();
|
|
11
|
+
void next();
|
|
26
12
|
return;
|
|
27
13
|
}
|
|
28
14
|
let idx = 0;
|
|
29
15
|
const nextNode = async () => {
|
|
30
16
|
idx++;
|
|
31
17
|
if (idx > nodes.length) {
|
|
32
|
-
next();
|
|
18
|
+
void next();
|
|
33
19
|
return;
|
|
34
20
|
}
|
|
35
21
|
const node = nodes[idx - 1];
|
|
@@ -40,7 +26,6 @@ const expendEventRoute = (valueEvent, select, nextCycle) => {
|
|
|
40
26
|
return;
|
|
41
27
|
}
|
|
42
28
|
}
|
|
43
|
-
// 正则匹配
|
|
44
29
|
if (EventMessageText.includes(select) && node.regular) {
|
|
45
30
|
const reg = new RegExp(node.regular);
|
|
46
31
|
if (!reg.test(valueEvent['MessageText'])) {
|
|
@@ -52,39 +37,25 @@ const expendEventRoute = (valueEvent, select, nextCycle) => {
|
|
|
52
37
|
void nextNode();
|
|
53
38
|
return;
|
|
54
39
|
}
|
|
55
|
-
// 递归:如果有children,继续递归下去
|
|
56
40
|
if (node.children && node.children.length > 0) {
|
|
57
|
-
// middleware 追加自身 handler
|
|
58
41
|
processChildren(node.children, [...middleware, node.handler], nextNode);
|
|
59
42
|
return;
|
|
60
43
|
}
|
|
61
|
-
// 没有children,直接处理handler
|
|
62
44
|
const currentsAndMiddleware = [...middleware, node.handler];
|
|
63
|
-
// node.handler 是一个异步函数。函数执行的结果是 { current: Current[] | Current, select: xxx }
|
|
64
45
|
try {
|
|
65
46
|
const currents = [];
|
|
66
47
|
for (const item of currentsAndMiddleware) {
|
|
67
48
|
const app = isAsyncFunction(item) ? await item() : item();
|
|
68
|
-
// 没有 default。因为是 import x from './';
|
|
69
|
-
// 中间件也有 selects。
|
|
70
|
-
// 如果 发现 和当前要处理的 selects 不匹配。
|
|
71
|
-
// 只要是一个不匹配。则说明处理还不是最终想要执行结果。
|
|
72
49
|
const selects = Array.isArray(app.select) ? app.select : [app.select];
|
|
73
|
-
// 没有匹配到
|
|
74
50
|
if (!selects.includes(select)) {
|
|
75
|
-
// 需要继续。
|
|
76
51
|
void nextNode();
|
|
77
52
|
return;
|
|
78
53
|
}
|
|
79
|
-
// 可能是数组。也可能不是数组
|
|
80
54
|
const currentsItem = Array.isArray(app.current) ? app.current : [app.current];
|
|
81
55
|
currents.push(...currentsItem);
|
|
82
56
|
}
|
|
83
|
-
// 要把二维数组拍平
|
|
84
57
|
callHandler(currents, (cn, ...cns) => {
|
|
85
58
|
if (cn) {
|
|
86
|
-
// 这里的 next 要加 true。
|
|
87
|
-
// 因为下一层是旧版本逻辑。不加一层。会出现处理了没有完全结束周期
|
|
88
59
|
nextCycle(true, ...cns);
|
|
89
60
|
return;
|
|
90
61
|
}
|
|
@@ -97,7 +68,7 @@ const expendEventRoute = (valueEvent, select, nextCycle) => {
|
|
|
97
68
|
};
|
|
98
69
|
void nextNode();
|
|
99
70
|
};
|
|
100
|
-
|
|
71
|
+
return processChildren;
|
|
101
72
|
};
|
|
102
73
|
|
|
103
|
-
export {
|
|
74
|
+
export { createRouteProcessChildren };
|
|
@@ -1,19 +1,2 @@
|
|
|
1
|
-
import { Next } from '../types
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @fileoverview 消息处理快
|
|
6
|
-
* 登录模块向核心模块发送数据
|
|
7
|
-
* 核心模块调用模块索引
|
|
8
|
-
* @module processor
|
|
9
|
-
* @author ningmengchongshui
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* 消息体处理机制
|
|
14
|
-
* @param event
|
|
15
|
-
* @param key
|
|
16
|
-
*/
|
|
17
|
-
declare const expendEvent: <T extends EventKeys>(valueEvent: Events[T], select: T, next: Next) => void;
|
|
18
|
-
|
|
19
|
-
export { expendEvent };
|
|
1
|
+
import { Next, Events, EventKeys } from '../types';
|
|
2
|
+
export declare const expendEvent: <T extends EventKeys>(valueEvent: Events[T], select: T, next: Next) => void;
|