@yumerijs/core 3.0.0 → 3.0.2

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,13 +42,26 @@ 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 路由路径
46
57
  * @returns Route 实例
47
58
  */
48
59
  route(routepath) {
49
- const realpath = path.join(this.childpath, routepath);
60
+ // `root` is a special fallback route name, not a filesystem path.
61
+ // URL paths must use POSIX separators even when Yumeri runs on Windows.
62
+ const realpath = routepath === 'root'
63
+ ? 'root'
64
+ : path.posix.join(this.childpath, routepath);
50
65
  if (this.core.routes[realpath]) {
51
66
  this.core.logger.warn(`Plugin "${this.pluginname}" attempt to register route "${path}", but it has already been registered.`);
52
67
  return new Route(realpath, this);
@@ -139,6 +154,19 @@ export class Context {
139
154
  this.core.components[name] = component;
140
155
  this.components.push(name);
141
156
  }
157
+ /**
158
+ * 注册服务
159
+ * @param name 服务名称
160
+ * @param service Service 派生类
161
+ */
162
+ registerService(name, service) {
163
+ if (this.core.services[name]) {
164
+ this.core.logger.warn(`Plugin "${this.pluginname}" attempt to register service "${name}", but it has already been registered.`);
165
+ return;
166
+ }
167
+ this.core.services[name] = service;
168
+ this.services.push(name);
169
+ }
142
170
  /**
143
171
  * 注册子 Context
144
172
  * @param name 子 Context 名称
@@ -212,6 +240,8 @@ export class Context {
212
240
  async dispose() {
213
241
  // 删除组件
214
242
  this.components.forEach((name) => delete this.core.components[name]);
243
+ // 删除服务
244
+ this.services.forEach((name) => delete this.core.services[name]);
215
245
  // 删除路由
216
246
  this.routes.forEach((route) => delete this.core.routes[route]);
217
247
  // 删除中间件
@@ -244,6 +274,10 @@ export class Context {
244
274
  }
245
275
  this.i18ns.length = 0;
246
276
  }
277
+ // 执行 Context 销毁回调
278
+ for (const callback of this.affects) {
279
+ await callback();
280
+ }
247
281
  // 清空内部记录
248
282
  this.components = [];
249
283
  this.routes = [];
@@ -251,5 +285,7 @@ export class Context {
251
285
  this.eventlisteners = [];
252
286
  this.hooks = {};
253
287
  this.childContexts = [];
288
+ this.affects = [];
289
+ this.services = [];
254
290
  }
255
291
  }
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,48 +1,48 @@
1
- {
2
- "name": "@yumerijs/core",
3
- "version": "3.0.0",
4
- "description": "Core module for yumeri",
5
- "main": "dist/index.js",
6
- "types": "dist/index.d.ts",
7
- "type": "module",
8
- "files": [
9
- "dist"
10
- ],
11
- "scripts": {
12
- "build": "tsc",
13
- "prepublishOnly": "yarn build"
14
- },
15
- "repository": {
16
- "type": "git",
17
- "url": "git+https://github.com/yumerijs/yumeri.git"
18
- },
19
- "keywords": [
20
- "yumeri",
21
- "API"
22
- ],
23
- "author": "WindyPear-Team,FireGuo",
24
- "license": "MIT",
25
- "bugs": {
26
- "url": "https://github.com/yumerijs/yumeri/issues"
27
- },
28
- "homepage": "https://github.com/yumerijs/yumeri#readme",
29
- "devDependencies": {
30
- "@types/formidable": "^3",
31
- "@types/js-yaml": "^4.0.9",
32
- "@types/mime-types": "^3",
33
- "@types/node": "^22.13.10",
34
- "@types/semver": "^7",
35
- "@types/ws": "^8",
36
- "typescript": "^6.0.3"
37
- },
38
- "dependencies": {
39
- "chokidar": "^4.0.3",
40
- "formidable": "^3.5.4",
41
- "js-yaml": "^4.1.0",
42
- "mime-types": "^3.0.1",
43
- "picocolors": "^1.1.1",
44
- "semver": "^7.8.4",
45
- "ws": "^8.18.3"
46
- },
47
- "packageManager": "yarn@4.9.1"
48
- }
1
+ {
2
+ "name": "@yumerijs/core",
3
+ "version": "3.0.2",
4
+ "description": "Core module for yumeri",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "type": "module",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "scripts": {
12
+ "build": "tsc",
13
+ "prepublishOnly": "yarn build"
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "git+https://github.com/yumerijs/yumeri.git"
18
+ },
19
+ "keywords": [
20
+ "yumeri",
21
+ "API"
22
+ ],
23
+ "author": "WindyPear-Team,FireGuo",
24
+ "license": "MIT",
25
+ "bugs": {
26
+ "url": "https://github.com/yumerijs/yumeri/issues"
27
+ },
28
+ "homepage": "https://github.com/yumerijs/yumeri#readme",
29
+ "devDependencies": {
30
+ "@types/formidable": "^3",
31
+ "@types/js-yaml": "^4.0.9",
32
+ "@types/mime-types": "^3",
33
+ "@types/node": "^22.13.10",
34
+ "@types/semver": "^7",
35
+ "@types/ws": "^8",
36
+ "typescript": "^6.0.3"
37
+ },
38
+ "dependencies": {
39
+ "chokidar": "^4.0.3",
40
+ "formidable": "^3.5.4",
41
+ "js-yaml": "^4.1.0",
42
+ "mime-types": "^3.0.1",
43
+ "picocolors": "^1.1.1",
44
+ "semver": "^7.8.4",
45
+ "ws": "^8.18.3"
46
+ },
47
+ "packageManager": "yarn@4.9.1"
48
+ }