Skip to content

Commit d55fe83

Browse files
committed
feat(@142vip/egg): 优化插件注册机制和类型支持
1 parent 94c1a4a commit d55fe83

File tree

2 files changed

+97
-19
lines changed

2 files changed

+97
-19
lines changed

packages/egg/src/plugin/logger.ts

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
interface EggCoreLogger {
2-
info: (msg: any, ...args: any[]) => void
3-
warn: (msg: any, ...args: any[]) => void
4-
error: (msg: any, ...args: any[]) => void
5-
debug: (msg: any, ...args: any[]) => void
6-
}
1+
import type { EggApp, EggCoreLogger, PluginConfig } from '../egg.interface'
72

83
/**
94
* egg插件日志
@@ -13,17 +8,17 @@ export class VipEggPluginLogger {
138
private readonly logger: EggCoreLogger
149
private readonly pluginName: string
1510

16-
constructor(pluginName: string, logger: EggCoreLogger) {
17-
this.logger = logger
18-
this.pluginName = pluginName
11+
constructor(pluginConfig: PluginConfig, app: EggApp) {
12+
this.logger = app.coreLogger
13+
this.pluginName = pluginConfig.pluginName
1914
}
2015

2116
/**
2217
* 单例对象
2318
*/
24-
public static getInstance(pluginName: string, logger: EggCoreLogger) {
19+
public static getInstance(pluginConfig: PluginConfig, app: EggApp): VipEggPluginLogger {
2520
if (this.instance == null) {
26-
this.instance = new VipEggPluginLogger(pluginName, logger)
21+
this.instance = new VipEggPluginLogger(pluginConfig, app)
2722
}
2823
return this.instance
2924
}

packages/egg/src/plugin/register.ts

Lines changed: 91 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
interface EggApp {
2-
config: EggConfig
3-
addSingleton: (pluginName: string, createInstance: CreateInstance) => void
4-
}
5-
type CreateInstance = (config: any, app: EggApp) => any
6-
type EggConfig = Record<string, any>
1+
import { VipLodash } from '@142vip/utils'
2+
import type { CreatePluginInstance, EggApp } from '../egg.interface'
73

84
/**
95
* 注册的Egg插件key
@@ -26,7 +22,7 @@ export enum RegisterEggPluginName {
2622
* @param app
2723
* @param createInstance
2824
*/
29-
export function registerPlugin(name: RegisterEggPluginName, app: EggApp, createInstance: CreateInstance) {
25+
export function registerPlugin(name: RegisterEggPluginName, app: EggApp, createInstance: CreatePluginInstance) {
3026
// 配置 agent 或者 app 才加载单例
3127
if (app.config[name] != null) {
3228
app.addSingleton(name, createInstance)
@@ -57,5 +53,92 @@ export function defaultPluginConfig(pluginName: RegisterEggPluginName, userConfi
5753
loaders: [PluginLoader.APP],
5854
}
5955

60-
return Object.assign({}, defaultConfig, userConfig)
56+
return VipLodash.merge(defaultConfig, userConfig)
57+
}
58+
59+
/**
60+
* egg启动的生命周期
61+
* - 参考:https://www.eggjs.org/zh-CN/basics/app-start
62+
*/
63+
export class EggPluginBoot {
64+
private readonly appOrAgent: EggApp
65+
private readonly pluginName: RegisterEggPluginName
66+
private readonly createEggPluginInstance: CreatePluginInstance
67+
constructor(eggPlugin: {
68+
pluginName: RegisterEggPluginName
69+
appOrAgent: EggApp
70+
createEggPluginInstance: CreatePluginInstance
71+
}) {
72+
this.pluginName = eggPlugin.pluginName
73+
this.appOrAgent = eggPlugin.appOrAgent
74+
this.createEggPluginInstance = eggPlugin.createEggPluginInstance
75+
}
76+
77+
/**
78+
* 此时 config 文件已经被读取并合并,但还并未生效
79+
* 这是应用层修改配置的最后机会
80+
* 注意:此函数只支持同步调用
81+
*/
82+
configWillLoad(): void {
83+
84+
}
85+
86+
/**
87+
* 所有配置已经加载完毕
88+
* 可以用来加载应用自定义的文件,启动自定义服务
89+
*/
90+
public async didLoad(): Promise<void> {
91+
// app.加载
92+
if (this.loaderPlugin(PluginLoader.APP)) {
93+
this.registerPlugin()
94+
}
95+
96+
// agent.js加载
97+
if (this.loaderPlugin(PluginLoader.AGENT)) {
98+
this.registerPlugin()
99+
}
100+
}
101+
102+
/**
103+
* 所有插件已启动完毕,但应用整体尚未 ready
104+
* 可进行数据初始化等操作,这些操作成功后才启动应用
105+
*/
106+
107+
public async willReady(): Promise<void> {
108+
109+
}
110+
111+
/**
112+
* 应用已启动完毕
113+
*/
114+
public async didReady(): Promise<void> {
115+
116+
}
117+
118+
/**
119+
* http/https 服务器已启动,开始接收外部请求
120+
* 此时可以从 app.server 获取 server 实例
121+
*/
122+
public async serverDidReady(): Promise<void> {
123+
124+
}
125+
126+
/**
127+
* 是否加载插件
128+
*/
129+
private loaderPlugin(loader: PluginLoader = PluginLoader.APP) {
130+
// 获取加载配置,默认app.js
131+
const { loaders = [] } = this.appOrAgent.config[this.pluginName]
132+
return loaders.includes(loader)
133+
}
134+
135+
/**
136+
* 注册插件
137+
*/
138+
private registerPlugin(): void {
139+
// 配置 agent 或者 app 才加载单例
140+
if (this.appOrAgent.config[this.pluginName] != null) {
141+
this.appOrAgent.addSingleton(this.pluginName, this.createEggPluginInstance)
142+
}
143+
}
61144
}

0 commit comments

Comments
 (0)