@yumerijs/core 3.0.0-alpha.6 → 3.0.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/context.d.ts CHANGED
@@ -4,6 +4,7 @@ import { HookHandler } from './hook.js';
4
4
  import { Middleware } from './middleware.js';
5
5
  import { IRenderer } from '@yumerijs/types';
6
6
  import { SessionStorageProcessor, Storage, SessionStorageSnapshot } from './storage.js';
7
+ import { Service } from './service.js';
7
8
  export interface Components {
8
9
  [key: string]: any;
9
10
  }
@@ -21,6 +22,8 @@ export declare class Context {
21
22
  private childContexts;
22
23
  private childPlugins;
23
24
  private i18ns;
25
+ private affects;
26
+ private services;
24
27
  component: Components;
25
28
  renderer?: IRenderer;
26
29
  module: any;
@@ -39,6 +42,11 @@ export declare class Context {
39
42
  * @param value 依赖值
40
43
  */
41
44
  inject(name: string, value: any): void;
45
+ /**
46
+ * 注册 Context 销毁时执行的回调
47
+ * @param callback 销毁回调
48
+ */
49
+ affect(callback: () => void | Promise<void>): void;
42
50
  /**
43
51
  * 注册路由
44
52
  * @param path 路由路径
@@ -94,6 +102,12 @@ export declare class Context {
94
102
  * @param component 组件实例
95
103
  */
96
104
  registerComponent(name: string, component: any): void;
105
+ /**
106
+ * 注册服务
107
+ * @param name 服务名称
108
+ * @param service Service 派生类
109
+ */
110
+ registerService(name: string, service: new (context: Context) => Service): void;
97
111
  /**
98
112
  * 注册子 Context
99
113
  * @param name 子 Context 名称
package/dist/context.js CHANGED
@@ -14,6 +14,8 @@ export class Context {
14
14
  childContexts = [];
15
15
  childPlugins = new Map();
16
16
  i18ns = [];
17
+ affects = [];
18
+ services = [];
17
19
  component;
18
20
  renderer;
19
21
  module;
@@ -40,6 +42,15 @@ export class Context {
40
42
  inject(name, value) {
41
43
  this.component[name] = value;
42
44
  }
45
+ /**
46
+ * 注册 Context 销毁时执行的回调
47
+ * @param callback 销毁回调
48
+ */
49
+ affect(callback) {
50
+ if (!callback)
51
+ return;
52
+ this.affects.push(callback);
53
+ }
43
54
  /**
44
55
  * 注册路由
45
56
  * @param path 路由路径
@@ -139,6 +150,19 @@ export class Context {
139
150
  this.core.components[name] = component;
140
151
  this.components.push(name);
141
152
  }
153
+ /**
154
+ * 注册服务
155
+ * @param name 服务名称
156
+ * @param service Service 派生类
157
+ */
158
+ registerService(name, service) {
159
+ if (this.core.services[name]) {
160
+ this.core.logger.warn(`Plugin "${this.pluginname}" attempt to register service "${name}", but it has already been registered.`);
161
+ return;
162
+ }
163
+ this.core.services[name] = service;
164
+ this.services.push(name);
165
+ }
142
166
  /**
143
167
  * 注册子 Context
144
168
  * @param name 子 Context 名称
@@ -212,6 +236,8 @@ export class Context {
212
236
  async dispose() {
213
237
  // 删除组件
214
238
  this.components.forEach((name) => delete this.core.components[name]);
239
+ // 删除服务
240
+ this.services.forEach((name) => delete this.core.services[name]);
215
241
  // 删除路由
216
242
  this.routes.forEach((route) => delete this.core.routes[route]);
217
243
  // 删除中间件
@@ -244,6 +270,10 @@ export class Context {
244
270
  }
245
271
  this.i18ns.length = 0;
246
272
  }
273
+ // 执行 Context 销毁回调
274
+ for (const callback of this.affects) {
275
+ await callback();
276
+ }
247
277
  // 清空内部记录
248
278
  this.components = [];
249
279
  this.routes = [];
@@ -251,5 +281,7 @@ export class Context {
251
281
  this.eventlisteners = [];
252
282
  this.hooks = {};
253
283
  this.childContexts = [];
284
+ this.affects = [];
285
+ this.services = [];
254
286
  }
255
287
  }
package/dist/core.d.ts CHANGED
@@ -10,6 +10,7 @@ import { Server as CoreServer } from './server.js';
10
10
  import { I18n } from './i18n.js';
11
11
  import { IRenderer } from '@yumerijs/types';
12
12
  import { SessionStorageProcessor, Storage, SessionStorageSnapshot } from './storage.js';
13
+ import { Service } from './service.js';
13
14
  export interface Plugin {
14
15
  apply?: (ctx: Context, config: Config) => Promise<void> | void;
15
16
  disable?: (ctx: Context) => Promise<void> | void;
@@ -48,6 +49,9 @@ export declare class Core {
48
49
  components: {
49
50
  [name: string]: any;
50
51
  };
52
+ services: {
53
+ [name: string]: new (context: Context) => Service;
54
+ };
51
55
  routes: Record<string, Route>;
52
56
  logger: Logger;
53
57
  globalMiddlewares: Record<string, Middleware>;
@@ -71,6 +75,9 @@ export declare class Core {
71
75
  registerComponent(name: string, component: any): void;
72
76
  getComponent(name: string): any;
73
77
  unregisterComponent(name: string): void;
78
+ registerService(name: string, service: new (context: Context) => Service): void;
79
+ getService(name: string, context: Context): Service | undefined;
80
+ unregisterService(name: string): void;
74
81
  on(event: string, listener: (...args: any[]) => Promise<void>): void;
75
82
  emit(event: string, ...payload: any): void;
76
83
  off(event: string, listener: (...args: any[]) => Promise<void>): void;
package/dist/core.js CHANGED
@@ -61,6 +61,7 @@ export const coreConfigSchema = Schema.object({
61
61
  export class Core {
62
62
  emitter = new EventEmitter();
63
63
  components = {};
64
+ services = {};
64
65
  routes = {};
65
66
  logger = new Logger('core');
66
67
  globalMiddlewares = {};
@@ -150,6 +151,10 @@ export class Core {
150
151
  if (component) {
151
152
  context.inject(name, component);
152
153
  }
154
+ const service = this.getService(name, context);
155
+ if (service) {
156
+ context.inject(name, service);
157
+ }
153
158
  }
154
159
  this.logger.info(`apply plugin ${shortName}`);
155
160
  if (plugin.apply) {
@@ -165,6 +170,16 @@ export class Core {
165
170
  unregisterComponent(name) {
166
171
  delete this.components[name];
167
172
  }
173
+ registerService(name, service) {
174
+ this.services[name] = service;
175
+ }
176
+ getService(name, context) {
177
+ const service = this.services[name];
178
+ return service ? new service(context) : undefined;
179
+ }
180
+ unregisterService(name) {
181
+ delete this.services[name];
182
+ }
168
183
  on(event, listener) {
169
184
  this.emitter.on(event, (...args) => {
170
185
  // 包一层保证 async 可以被捕获
package/dist/index.d.ts CHANGED
@@ -13,3 +13,4 @@ export * from './hook.js';
13
13
  export * from './middleware.js';
14
14
  export * from './i18n.js';
15
15
  export * from './storage.js';
16
+ export * from './service.js';
package/dist/index.js CHANGED
@@ -13,3 +13,4 @@ export * from './hook.js';
13
13
  export * from './middleware.js';
14
14
  export * from './i18n.js';
15
15
  export * from './storage.js';
16
+ export * from './service.js';
@@ -0,0 +1,4 @@
1
+ import type { Context } from './context.js';
2
+ export declare class Service {
3
+ constructor(_context: Context);
4
+ }
@@ -0,0 +1,3 @@
1
+ export class Service {
2
+ constructor(_context) { }
3
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yumerijs/core",
3
- "version": "3.0.0-alpha.6",
3
+ "version": "3.0.1",
4
4
  "description": "Core module for yumeri",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",