@yumerijs/core 1.1.2 → 1.2.0

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
@@ -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
@@ -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,59 @@
1
1
  import { Core } from './core';
2
2
  import { Command } from './command';
3
+ import { Route } from './route';
3
4
  export declare class Context {
4
5
  private core;
5
6
  pluginname: string;
7
+ /**
8
+ * 创建 Context 实例
9
+ * @param core Core 实例
10
+ * @param pluginname 插件名称
11
+ */
6
12
  constructor(core: Core, pluginname: string);
13
+ /**
14
+ * @deprecated Use route() instead.
15
+ */
7
16
  command(name: string): Command;
17
+ /**
18
+ * 注册路由
19
+ * @param path 路由路径
20
+ * @returns Route
21
+ */
22
+ route(path: string): Route;
23
+ /**
24
+ * 注册事件
25
+ * @param name 事件名称
26
+ * @param listener 事件监听器
27
+ */
8
28
  on(name: string, listener: (...args: any[]) => Promise<void>): void;
29
+ /**
30
+ * 注册全局中间件
31
+ * @param name 中间件名称
32
+ * @param callback 中间件回调函数
33
+ */
9
34
  use(name: string, callback: Function): void;
10
35
  /**
11
36
  * Unless nessesary, do not use core directly
12
37
  */
13
38
  getCore(): Core;
39
+ /**
40
+ * 触发事件
41
+ * @param event 事件名称
42
+ * @param args 事件参数
43
+ * @returns Promise<void>
44
+ */
14
45
  emit(event: string, ...args: any[]): Promise<void>;
46
+ /**
47
+ * 获取组件实例
48
+ * @param name 组件名称
49
+ * @returns 组件实例
50
+ */
15
51
  getComponent(name: string): any;
52
+ /**
53
+ * 注册组件
54
+ * @param name 组件名称
55
+ * @param component 组件实例
56
+ * @returns void
57
+ */
16
58
  registerComponent(name: string, component: any): void;
17
59
  }
package/dist/context.js CHANGED
@@ -5,10 +5,18 @@ const command_1 = require("./command");
5
5
  class Context {
6
6
  core;
7
7
  pluginname;
8
+ /**
9
+ * 创建 Context 实例
10
+ * @param core Core 实例
11
+ * @param pluginname 插件名称
12
+ */
8
13
  constructor(core, pluginname) {
9
14
  this.core = core;
10
15
  this.pluginname = pluginname;
11
16
  }
17
+ /**
18
+ * @deprecated Use route() instead.
19
+ */
12
20
  command(name) {
13
21
  if (this.core.commands[name]) {
14
22
  this.core.logger.warn(`Plugin "${this.pluginname}" attempt to register command "${name}", but it has been registered by plugin "${this.core.cmdtoplu[name]}."`);
@@ -19,11 +27,42 @@ class Context {
19
27
  return this.core.command(name);
20
28
  }
21
29
  }
30
+ /**
31
+ * 注册路由
32
+ * @param path 路由路径
33
+ * @returns Route
34
+ */
35
+ route(path) {
36
+ if (this.core.routes[path]) {
37
+ this.core.logger.warn(`Plugin "${this.pluginname}" attempt to register route "${path}", but it has been registered by plugin "${this.core.routetoplu[path]}."`);
38
+ return this.core.routes[path];
39
+ }
40
+ else {
41
+ this.core.routetoplu[path] = this.pluginname;
42
+ return this.core.route(path);
43
+ }
44
+ }
45
+ /**
46
+ * 注册事件
47
+ * @param name 事件名称
48
+ * @param listener 事件监听器
49
+ */
22
50
  on(name, listener) {
23
51
  // 存储该插件监听的事件,需要注意的是一个事件可由多个插件监听
52
+ if (!this.core.evttoplu[name]) {
53
+ this.core.evttoplu[name] = {};
54
+ }
55
+ if (!this.core.evttoplu[name][this.pluginname]) {
56
+ this.core.evttoplu[name][this.pluginname] = [];
57
+ }
24
58
  this.core.evttoplu[name][this.pluginname].push(listener);
25
59
  this.core.on(name, listener);
26
60
  }
61
+ /**
62
+ * 注册全局中间件
63
+ * @param name 中间件名称
64
+ * @param callback 中间件回调函数
65
+ */
27
66
  use(name, callback) {
28
67
  this.core.mdwtoplu[name] = this.pluginname;
29
68
  this.core.use(name, async (...args) => {
@@ -36,12 +75,29 @@ class Context {
36
75
  getCore() {
37
76
  return this.core;
38
77
  }
78
+ /**
79
+ * 触发事件
80
+ * @param event 事件名称
81
+ * @param args 事件参数
82
+ * @returns Promise<void>
83
+ */
39
84
  async emit(event, ...args) {
40
85
  return await this.core.emit(event, ...args);
41
86
  }
87
+ /**
88
+ * 获取组件实例
89
+ * @param name 组件名称
90
+ * @returns 组件实例
91
+ */
42
92
  getComponent(name) {
43
93
  return this.core.getComponent(name);
44
94
  }
95
+ /**
96
+ * 注册组件
97
+ * @param name 组件名称
98
+ * @param component 组件实例
99
+ * @returns void
100
+ */
45
101
  registerComponent(name, component) {
46
102
  if (this.core.components[name]) {
47
103
  this.core.logger.warn(`Plugin "${this.pluginname}" attempt to register component "${name}", but it has been registered by plugin "${this.core.comtoplu[name]}."`);
package/dist/core.d.ts CHANGED
@@ -4,6 +4,7 @@ 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';
8
9
  interface Plugin {
9
10
  apply: (ctx: Context, config: Config) => Promise<void>;
@@ -40,6 +41,7 @@ export declare class Core {
40
41
  [name: string]: any;
41
42
  };
42
43
  commands: Record<string, Command>;
44
+ routes: Record<string, Route>;
43
45
  pluginLoader: PluginLoader;
44
46
  logger: Logger;
45
47
  providedComponents: {
@@ -48,12 +50,39 @@ export declare class Core {
48
50
  private pluginModules;
49
51
  private configPath;
50
52
  private globalMiddlewares;
53
+ /**
54
+ * @deprecated 命令与插件对应关系
55
+ */
51
56
  cmdtoplu: Record<string, string>;
57
+ /**
58
+ * 路由与插件对应关系
59
+ */
60
+ routetoplu: Record<string, string>;
61
+ /**
62
+ * 组件与插件对应关系
63
+ */
52
64
  comtoplu: Record<string, string>;
65
+ /**
66
+ * 事件监听器与插件对应关系
67
+ */
53
68
  evttoplu: Record<string, Record<string, ((...args: any[]) => Promise<void>)[]>>;
69
+ /**
70
+ * 中间件与插件对应关系
71
+ */
54
72
  mdwtoplu: Record<string, string>;
55
73
  plftoplu: Record<string, string>;
74
+ /**
75
+ * 插件状态
76
+ */
56
77
  pluginStatus: Record<string, PluginStatus>;
78
+ /**
79
+ * 插件监听器
80
+ */
81
+ private pluginWatchers;
82
+ /**
83
+ * 初始化核心
84
+ * @param pluginLoader 插件加载器
85
+ */
57
86
  constructor(pluginLoader: PluginLoader);
58
87
  /**
59
88
  * 加载配置文件
@@ -73,27 +102,30 @@ export declare class Core {
73
102
  getPluginConfig(pluginName: string): Promise<Config>;
74
103
  /**
75
104
  * 加载所有在配置文件中启用的插件
105
+ * @returns Promise<void>
76
106
  */
77
107
  loadPlugins(): Promise<void>;
78
108
  /**
79
- * 导出的函数:加载单个插件
109
+ * 加载单个插件
80
110
  * @param pluginName 要加载的插件名
81
111
  * @param triggerPendingCheck 是否在加载后触发对其他待定插件的检查,默认为 true
82
112
  * @returns Promise<boolean> 是否加载成功
83
113
  */
84
- loadSinglePlugin(pluginName: string, triggerPendingCheck?: boolean): Promise<boolean>;
114
+ loadSinglePlugin(pluginName: string, triggerPendingCheck?: boolean, onlypending?: boolean): Promise<boolean>;
85
115
  /**
86
- * 内部函数:尝试加载所有处于 PENDING 状态的插件
116
+ * 尝试加载所有处于 PENDING 状态的插件
117
+ * @returns Promise<void>
87
118
  */
88
119
  private _loadPendingPlugins;
89
120
  /**
90
121
  * 卸载插件,并递归卸载依赖于它的其他插件
91
122
  * @param pluginNameToUnload 要卸载的插件名
92
123
  */
93
- unloadPlugin(pluginNameToUnload: string): Promise<void>;
124
+ unloadPlugin(pluginNameToUnload: string, ispending?: boolean): Promise<void>;
94
125
  /**
95
- * 内部函数:执行单个插件的卸载逻辑
126
+ * 执行单个插件的卸载逻辑
96
127
  * @param pluginName 插件名
128
+ * @param ispending 是否处于 PENDING 状态
97
129
  */
98
130
  private _unloadSinglePlugin;
99
131
  /**
@@ -102,11 +134,11 @@ export declare class Core {
102
134
  */
103
135
  reloadPlugin(pluginName: string): Promise<void>;
104
136
  /**
105
- * 监听插件文件变化,实现热更新
106
- * @param core Core 实例
107
- * @param pluginsDir 插件目录,默认为 'plugins'
137
+ * 监听插件文件变化
138
+ * @param pluginName 插件名称
139
+ * @param pluginPath 插件路径
108
140
  */
109
- watchPlugins(core: Core, pluginsDir?: string): void;
141
+ private watchPlugin;
110
142
  /**
111
143
  * 注册组件
112
144
  * @param name 组件名称
@@ -144,19 +176,28 @@ export declare class Core {
144
176
  */
145
177
  use(name: string, middleware: Middleware): Core;
146
178
  /**
147
- * 注册命令
148
- * @param name 命令名称
149
- * @returns Command 实例
179
+ * @deprecated 已更改为路由
150
180
  */
151
181
  command(name: string): Command;
182
+ /**
183
+ * 注册路由
184
+ * @param path 路由路径
185
+ * @returns Route 实例
186
+ */
187
+ route(path: string): Route;
152
188
  /**
153
189
  * 执行命令
154
- * @param name 命令名称
155
- * @param session 会话对象
156
- * @param args 传递给命令处理函数的参数
157
- * @returns 会话对象或 null
190
+ * @deprecated
158
191
  */
159
192
  executeCommand(name: string, session: any, ...args: any[]): Promise<Session | null>;
193
+ /**
194
+ * 执行路由
195
+ * @param pathname 需要匹配的URL
196
+ * @param session 当前会话
197
+ * @param queryParams URL参数
198
+ * @returns 是否匹配成功
199
+ */
200
+ executeRoute(pathname: string, session: Session, queryParams: URLSearchParams): Promise<boolean>;
160
201
  /**
161
202
  * 注册平台
162
203
  * @param platform 平台实例
@@ -174,5 +215,11 @@ export declare class Core {
174
215
  * @returns Command 实例或 null
175
216
  */
176
217
  getCommand(name: string): Command | null;
218
+ /**
219
+ * 获取路由
220
+ * @param path 匹配路径
221
+ * @return Route 实例或 false
222
+ */
223
+ getRoute(path: string): Route | false;
177
224
  }
178
225
  export {};