@yumerijs/core 1.1.3 → 1.2.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.
package/dist/command.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @time: 2025/03/25 23:53
2
+ * @time: 2025/08/14 09:48
3
3
  * @author: FireGuo
4
4
  * WindyPear-Team All right reserved
5
5
  **/
@@ -52,6 +52,9 @@ export declare enum HttpMethod {
52
52
  /**
53
53
  * 命令类,用于注册和执行命令
54
54
  */
55
+ /**
56
+ * @deprecated Use Route instead.
57
+ */
55
58
  export declare class Command {
56
59
  /** 命令名称 */
57
60
  name: string;
package/dist/command.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  /**
3
- * @time: 2025/03/25 23:53
3
+ * @time: 2025/08/14 09:48
4
4
  * @author: FireGuo
5
5
  * WindyPear-Team All right reserved
6
6
  **/
@@ -45,6 +45,9 @@ var HttpMethod;
45
45
  /**
46
46
  * 命令类,用于注册和执行命令
47
47
  */
48
+ /**
49
+ * @deprecated Use Route instead.
50
+ */
48
51
  class Command {
49
52
  /** 命令名称 */
50
53
  name;
package/dist/config.d.ts CHANGED
File without changes
package/dist/config.js CHANGED
File without changes
package/dist/context.d.ts CHANGED
@@ -1,17 +1,97 @@
1
+ /**
2
+ * @time: 2025/08/14 19:10
3
+ * @author: FireGuo
4
+ * WindyPear-Team All right reserved
5
+ **/
1
6
  import { Core } from './core';
2
7
  import { Command } from './command';
8
+ import { Route } from './route';
9
+ import { HookHandler } from './hook';
10
+ /**
11
+ * 插件上下文对象
12
+ * 每个插件一个 Context,用于管理插件注册的命令、路由、事件、组件和中间件
13
+ */
3
14
  export declare class Context {
4
15
  private core;
16
+ private routes;
17
+ private eventlisteners;
18
+ private components;
19
+ private middlewares;
20
+ private hooks;
21
+ /**
22
+ * 插件名称
23
+ */
5
24
  pluginname: string;
25
+ /**
26
+ * 创建 Context 实例
27
+ * @param core Core 实例
28
+ * @param pluginname 插件名称
29
+ */
6
30
  constructor(core: Core, pluginname: string);
31
+ /**
32
+ * @deprecated Use route() instead.
33
+ * 尝试注册命令,如果命令已存在则警告
34
+ */
7
35
  command(name: string): Command;
36
+ /**
37
+ * 注册路由
38
+ * @param path 路由路径
39
+ * @returns Route 实例
40
+ */
41
+ route(path: string): Route;
42
+ /**
43
+ * 注册事件
44
+ * @param name 事件名称
45
+ * @param listener 事件监听器
46
+ */
8
47
  on(name: string, listener: (...args: any[]) => Promise<void>): void;
48
+ /**
49
+ * 注册全局中间件
50
+ * @param name 中间件名称
51
+ * @param callback 中间件回调函数
52
+ */
9
53
  use(name: string, callback: Function): void;
10
54
  /**
11
- * Unless nessesary, do not use core directly
12
- */
55
+ * 注册 Hook 钩子
56
+ * @param name Hook 点名称
57
+ * @param hookname 钩子名称
58
+ * @param callback 钩子回调函数
59
+ */
60
+ hook(name: string, hookname: string, callback: HookHandler): void;
61
+ /**
62
+ * 执行 Hook 钩子
63
+ * @param name Hook 点名称
64
+ * @param args Hook 参数
65
+ * @returns Promise<any[]>
66
+ */
67
+ executeHook(name: string, ...args: any[]): Promise<any[]>;
68
+ /**
69
+ * 获取 Core 实例
70
+ * @returns Core
71
+ */
13
72
  getCore(): Core;
73
+ /**
74
+ * 触发事件
75
+ * @param event 事件名称
76
+ * @param args 事件参数
77
+ * @returns Promise<void>
78
+ */
14
79
  emit(event: string, ...args: any[]): Promise<void>;
80
+ /**
81
+ * 获取组件实例
82
+ * @param name 组件名称
83
+ * @returns 组件实例
84
+ */
15
85
  getComponent(name: string): any;
86
+ /**
87
+ * 注册组件
88
+ * @param name 组件名称
89
+ * @param component 组件实例
90
+ */
16
91
  registerComponent(name: string, component: any): void;
92
+ /**
93
+ * 卸载插件时清理注册的所有资源
94
+ * 包括组件、路由、中间件和事件监听器
95
+ */
96
+ dispose(): void;
17
97
  }
package/dist/context.js CHANGED
@@ -2,54 +2,166 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Context = void 0;
4
4
  const command_1 = require("./command");
5
+ const route_1 = require("./route");
6
+ /**
7
+ * 插件上下文对象
8
+ * 每个插件一个 Context,用于管理插件注册的命令、路由、事件、组件和中间件
9
+ */
5
10
  class Context {
6
11
  core;
12
+ routes = [];
13
+ eventlisteners = [];
14
+ components = [];
15
+ middlewares = [];
16
+ hooks = {};
17
+ /**
18
+ * 插件名称
19
+ */
7
20
  pluginname;
21
+ /**
22
+ * 创建 Context 实例
23
+ * @param core Core 实例
24
+ * @param pluginname 插件名称
25
+ */
8
26
  constructor(core, pluginname) {
9
27
  this.core = core;
10
28
  this.pluginname = pluginname;
11
29
  }
30
+ /**
31
+ * @deprecated Use route() instead.
32
+ * 尝试注册命令,如果命令已存在则警告
33
+ */
12
34
  command(name) {
13
35
  if (this.core.commands[name]) {
14
- this.core.logger.warn(`Plugin "${this.pluginname}" attempt to register command "${name}", but it has been registered by plugin "${this.core.cmdtoplu[name]}."`);
36
+ this.core.logger.warn(`Plugin "${this.pluginname}" attempt to register command "${name}", but it has already been registered.`);
15
37
  return new command_1.Command(this.core, name);
16
38
  }
17
- else {
18
- this.core.cmdtoplu[name] = this.pluginname;
19
- return this.core.command(name);
39
+ return this.core.command(name);
40
+ }
41
+ /**
42
+ * 注册路由
43
+ * @param path 路由路径
44
+ * @returns Route 实例
45
+ */
46
+ route(path) {
47
+ if (this.core.routes[path]) {
48
+ this.core.logger.warn(`Plugin "${this.pluginname}" attempt to register route "${path}", but it has already been registered.`);
49
+ return new route_1.Route(path);
20
50
  }
51
+ this.routes.push(path);
52
+ return this.core.route(path);
21
53
  }
54
+ /**
55
+ * 注册事件
56
+ * @param name 事件名称
57
+ * @param listener 事件监听器
58
+ */
22
59
  on(name, listener) {
23
- // 存储该插件监听的事件,需要注意的是一个事件可由多个插件监听
24
- this.core.evttoplu[name][this.pluginname].push(listener);
60
+ // 记录插件自己的事件监听器
61
+ this.eventlisteners.push({ name, listener });
62
+ // 注册到 core 的全局事件监听器中
25
63
  this.core.on(name, listener);
26
64
  }
65
+ /**
66
+ * 注册全局中间件
67
+ * @param name 中间件名称
68
+ * @param callback 中间件回调函数
69
+ */
27
70
  use(name, callback) {
28
- this.core.mdwtoplu[name] = this.pluginname;
29
- this.core.use(name, async (...args) => {
30
- return callback(...args);
31
- });
71
+ this.middlewares.push(name);
72
+ this.core.use(name, async (...args) => callback(...args));
73
+ }
74
+ /**
75
+ * 注册 Hook 钩子
76
+ * @param name Hook 点名称
77
+ * @param hookname 钩子名称
78
+ * @param callback 钩子回调函数
79
+ */
80
+ hook(name, hookname, callback) {
81
+ this.core.hook(name, hookname, callback);
82
+ this.hooks[name].push(hookname);
83
+ }
84
+ /**
85
+ * 执行 Hook 钩子
86
+ * @param name Hook 点名称
87
+ * @param args Hook 参数
88
+ * @returns Promise<any[]>
89
+ */
90
+ async executeHook(name, ...args) {
91
+ return await this.core.hookExecute(name, ...args);
32
92
  }
33
93
  /**
34
- * Unless nessesary, do not use core directly
35
- */
94
+ * 获取 Core 实例
95
+ * @returns Core
96
+ */
36
97
  getCore() {
37
98
  return this.core;
38
99
  }
100
+ /**
101
+ * 触发事件
102
+ * @param event 事件名称
103
+ * @param args 事件参数
104
+ * @returns Promise<void>
105
+ */
39
106
  async emit(event, ...args) {
40
107
  return await this.core.emit(event, ...args);
41
108
  }
109
+ /**
110
+ * 获取组件实例
111
+ * @param name 组件名称
112
+ * @returns 组件实例
113
+ */
42
114
  getComponent(name) {
43
115
  return this.core.getComponent(name);
44
116
  }
117
+ /**
118
+ * 注册组件
119
+ * @param name 组件名称
120
+ * @param component 组件实例
121
+ */
45
122
  registerComponent(name, component) {
46
123
  if (this.core.components[name]) {
47
- this.core.logger.warn(`Plugin "${this.pluginname}" attempt to register component "${name}", but it has been registered by plugin "${this.core.comtoplu[name]}."`);
124
+ this.core.logger.warn(`Plugin "${this.pluginname}" attempt to register component "${name}", but it has already been registered.`);
48
125
  return;
49
126
  }
50
- this.core.comtoplu[name] = this.pluginname;
51
127
  this.core.components[name] = component;
52
- return;
128
+ this.components.push(name);
129
+ }
130
+ /**
131
+ * 卸载插件时清理注册的所有资源
132
+ * 包括组件、路由、中间件和事件监听器
133
+ */
134
+ dispose() {
135
+ // 删除组件
136
+ this.components.forEach((name) => {
137
+ delete this.core.components[name];
138
+ });
139
+ // 删除路由
140
+ this.routes.forEach((route) => {
141
+ delete this.core.routes[route];
142
+ });
143
+ // 删除中间件
144
+ this.middlewares.forEach((middleware) => {
145
+ delete this.core.globalMiddlewares[middleware];
146
+ });
147
+ // 删除事件监听器
148
+ this.eventlisteners.forEach(({ name, listener }) => {
149
+ if (this.core.eventListeners?.[name]) {
150
+ this.core.eventListeners[name] =
151
+ this.core.eventListeners[name].filter((l) => l !== listener);
152
+ }
153
+ });
154
+ // 删除钩子
155
+ for (const hook in this.hooks) {
156
+ this.hooks.hook.forEach((hookname) => {
157
+ this.core.unhook(hook, hookname);
158
+ });
159
+ }
160
+ // 清空 Context 内部记录,避免内存泄漏
161
+ this.components = [];
162
+ this.routes = [];
163
+ this.middlewares = [];
164
+ this.eventlisteners = [];
53
165
  }
54
166
  }
55
167
  exports.Context = Context;
package/dist/core.d.ts CHANGED
@@ -4,7 +4,9 @@ import { Command } from './command';
4
4
  import { Session } from './session';
5
5
  import { Platform } from './platform';
6
6
  import { Middleware } from './middleware';
7
+ import { Route } from './route';
7
8
  import { Context } from './context';
9
+ import { HookHandler, Hook } from './hook';
8
10
  interface Plugin {
9
11
  apply: (ctx: Context, config: Config) => Promise<void>;
10
12
  disable: (ctx: Context) => Promise<void>;
@@ -35,26 +37,38 @@ export declare class Core {
35
37
  };
36
38
  config: any;
37
39
  platforms: Platform[];
38
- private eventListeners;
40
+ eventListeners: {
41
+ [event: string]: ((...args: any[]) => Promise<void>)[];
42
+ };
39
43
  components: {
40
44
  [name: string]: any;
41
45
  };
42
46
  commands: Record<string, Command>;
47
+ routes: Record<string, Route>;
43
48
  pluginLoader: PluginLoader;
44
49
  logger: Logger;
45
50
  providedComponents: {
46
51
  [name: string]: string;
47
52
  };
53
+ globalMiddlewares: Record<string, Middleware>;
54
+ pluginStatus: Record<string, PluginStatus>;
55
+ hooks: Record<string, Hook>;
56
+ private pluginWatchers;
48
57
  private pluginModules;
49
58
  private configPath;
50
- private globalMiddlewares;
51
- cmdtoplu: Record<string, string>;
52
- comtoplu: Record<string, string>;
53
- evttoplu: Record<string, Record<string, ((...args: any[]) => Promise<void>)[]>>;
54
- mdwtoplu: Record<string, string>;
55
- plftoplu: Record<string, string>;
56
- pluginStatus: Record<string, PluginStatus>;
59
+ /**
60
+ * 插件名 -> Context 映射
61
+ */
62
+ private pluginContexts;
57
63
  constructor(pluginLoader: PluginLoader);
64
+ /**
65
+ * 获取或创建插件对应 Context
66
+ */
67
+ getContext(pluginName: string): Context;
68
+ /**
69
+ * 清理指定插件注册的所有内容
70
+ */
71
+ unregall(pluginName: string): void;
58
72
  /**
59
73
  * 加载配置文件
60
74
  * @param configPath 配置文件的路径
@@ -73,17 +87,19 @@ export declare class Core {
73
87
  getPluginConfig(pluginName: string): Promise<Config>;
74
88
  /**
75
89
  * 加载所有在配置文件中启用的插件
90
+ * @returns Promise<void>
76
91
  */
77
92
  loadPlugins(): Promise<void>;
78
93
  /**
79
- * 导出的函数:加载单个插件
94
+ * 加载单个插件
80
95
  * @param pluginName 要加载的插件名
81
96
  * @param triggerPendingCheck 是否在加载后触发对其他待定插件的检查,默认为 true
82
97
  * @returns Promise<boolean> 是否加载成功
83
98
  */
84
99
  loadSinglePlugin(pluginName: string, triggerPendingCheck?: boolean, onlypending?: boolean): Promise<boolean>;
85
100
  /**
86
- * 内部函数:尝试加载所有处于 PENDING 状态的插件
101
+ * 尝试加载所有处于 PENDING 状态的插件
102
+ * @returns Promise<void>
87
103
  */
88
104
  private _loadPendingPlugins;
89
105
  /**
@@ -92,8 +108,9 @@ export declare class Core {
92
108
  */
93
109
  unloadPlugin(pluginNameToUnload: string, ispending?: boolean): Promise<void>;
94
110
  /**
95
- * 内部函数:执行单个插件的卸载逻辑
111
+ * 执行单个插件的卸载逻辑
96
112
  * @param pluginName 插件名
113
+ * @param ispending 是否处于 PENDING 状态
97
114
  */
98
115
  private _unloadSinglePlugin;
99
116
  /**
@@ -102,11 +119,11 @@ export declare class Core {
102
119
  */
103
120
  reloadPlugin(pluginName: string): Promise<void>;
104
121
  /**
105
- * 监听插件文件变化,实现热更新
106
- * @param core Core 实例
107
- * @param pluginsDir 插件目录,默认为 'plugins'
122
+ * 监听插件文件变化
123
+ * @param pluginName 插件名称
124
+ * @param pluginPath 插件路径
108
125
  */
109
- watchPlugins(core: Core, pluginsDir?: string): void;
126
+ private watchPlugin;
110
127
  /**
111
128
  * 注册组件
112
129
  * @param name 组件名称
@@ -144,35 +161,63 @@ export declare class Core {
144
161
  */
145
162
  use(name: string, middleware: Middleware): Core;
146
163
  /**
147
- * 注册命令
148
- * @param name 命令名称
149
- * @returns Command 实例
164
+ * @deprecated 已更改为路由
150
165
  */
151
166
  command(name: string): Command;
167
+ /**
168
+ * 注册路由
169
+ * @param path 路由路径
170
+ * @returns Route 实例
171
+ */
172
+ route(path: string): Route;
173
+ /**
174
+ * 注册 Hook 钩子
175
+ * @param name Hook 点名称
176
+ * @param hookname 钩子名称
177
+ * @param callback 钩子函数
178
+ */
179
+ hook(name: string, hookname: string, callback: HookHandler): any;
180
+ /**
181
+ * 消除 Hook 钩子
182
+ */
183
+ unhook(name: string, hookname: string): any;
184
+ /**
185
+ * 执行 Hook 钩子
186
+ * @param name Hook 点名称
187
+ * @param args 参数
188
+ * @return Promise<any[]>
189
+ */
190
+ hookExecute(name: string, ...args: any[]): Promise<any[]>;
152
191
  /**
153
192
  * 执行命令
154
- * @param name 命令名称
155
- * @param session 会话对象
156
- * @param args 传递给命令处理函数的参数
157
- * @returns 会话对象或 null
193
+ * @deprecated
158
194
  */
159
195
  executeCommand(name: string, session: any, ...args: any[]): Promise<Session | null>;
196
+ /**
197
+ * 执行路由
198
+ * @param pathname 需要匹配的URL
199
+ * @param session 当前会话
200
+ * @param queryParams URL参数
201
+ * @returns 是否匹配成功
202
+ */
203
+ executeRoute(pathname: string, session: Session, queryParams: URLSearchParams): Promise<boolean>;
160
204
  /**
161
205
  * 注册平台
162
206
  * @param platform 平台实例
163
207
  * @returns 平台启动结果
164
208
  */
165
209
  registerPlatform(platform: Platform): any;
166
- /**
167
- * 清理指定插件注册的所有内容
168
- * @param pluginname 插件名称
169
- */
170
- unregall(pluginname: string): void;
171
210
  /**
172
211
  * 获取命令
173
212
  * @param name 命令名称
174
213
  * @returns Command 实例或 null
175
214
  */
176
215
  getCommand(name: string): Command | null;
216
+ /**
217
+ * 获取路由
218
+ * @param path 匹配路径
219
+ * @return Route 实例或 false
220
+ */
221
+ getRoute(path: string): Route | false;
177
222
  }
178
223
  export {};