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'
7
3
8
4
/**
9
5
* 注册的Egg插件key
@@ -26,7 +22,7 @@ export enum RegisterEggPluginName {
26
22
* @param app
27
23
* @param createInstance
28
24
*/
29
- export function registerPlugin ( name : RegisterEggPluginName , app : EggApp , createInstance : CreateInstance ) {
25
+ export function registerPlugin ( name : RegisterEggPluginName , app : EggApp , createInstance : CreatePluginInstance ) {
30
26
// 配置 agent 或者 app 才加载单例
31
27
if ( app . config [ name ] != null ) {
32
28
app . addSingleton ( name , createInstance )
@@ -57,5 +53,92 @@ export function defaultPluginConfig(pluginName: RegisterEggPluginName, userConfi
57
53
loaders : [ PluginLoader . APP ] ,
58
54
}
59
55
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
+ }
61
144
}
0 commit comments