@midwayjs/web
Version:
Midway Web Framework for Egg.js
144 lines • 6.51 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MidwayWebLifeCycleService = void 0;
const core_1 = require("@midwayjs/core");
const util_1 = require("util");
const debug = (0, util_1.debuglog)('midway:debug');
let MidwayWebLifeCycleService = class MidwayWebLifeCycleService {
constructor(applicationContext) {
this.applicationContext = applicationContext;
this.lifecycleInstanceList = [];
}
async init() {
// run lifecycle
const cycles = (0, core_1.listModule)(core_1.CONFIGURATION_KEY);
debug(`[core]: Found Configuration length = ${cycles.length}`);
for (const cycle of cycles) {
if (cycle.target instanceof core_1.FunctionalConfiguration) {
// 函数式写法
cycle.instance = cycle.target;
}
else {
// 普通类写法
debug(`[core]: Lifecycle run ${cycle.target.name} init`);
cycle.instance = await this.applicationContext.getAsync(cycle.target);
}
if (cycle.instance) {
this.lifecycleInstanceList.push(cycle);
}
}
// bind object lifecycle
await Promise.all([
this.runObjectLifeCycle(this.lifecycleInstanceList, 'onBeforeObjectCreated'),
this.runObjectLifeCycle(this.lifecycleInstanceList, 'onObjectCreated'),
this.runObjectLifeCycle(this.lifecycleInstanceList, 'onObjectInit'),
this.runObjectLifeCycle(this.lifecycleInstanceList, 'onBeforeObjectDestroy'),
]);
// bind framework lifecycle
// onAppError
// exec onConfigLoad()
await this.runContainerLifeCycle(this.lifecycleInstanceList, 'onConfigLoad', configData => {
if (configData) {
this.configService.addObject(configData);
}
});
// cluster 下,onReady 放到 egg willReady 中执行
}
async runReady() {
// exec onReady()
await this.runContainerLifeCycle(this.lifecycleInstanceList, 'onReady');
}
async afterInit() {
// exec framework.run()
await this.frameworkService.runFramework();
// exec onServerReady()
await this.runContainerLifeCycle(this.lifecycleInstanceList, 'onServerReady');
// clear config merge cache
if (!this.configService.getConfiguration('debug.recordConfigMergeOrder')) {
this.configService.clearConfigMergeOrder();
}
// some preload module init
const modules = (0, core_1.listPreloadModule)();
for (const module of modules) {
// preload init context
await this.applicationContext.getAsync(module);
}
}
async stop() {
// stop lifecycle
const cycles = (0, core_1.listModule)(core_1.CONFIGURATION_KEY);
for (const cycle of cycles) {
let inst;
if (cycle.target instanceof core_1.FunctionalConfiguration) {
// 函数式写法
inst = cycle.target;
}
else {
inst = await this.applicationContext.getAsync(cycle.target);
}
await this.runContainerLifeCycle(inst, 'onStop');
}
// stop framework
await this.frameworkService.stopFramework();
}
async runContainerLifeCycle(lifecycleInstanceOrList, lifecycle, resultHandler) {
if (Array.isArray(lifecycleInstanceOrList)) {
for (const cycle of lifecycleInstanceOrList) {
if (typeof cycle.instance[lifecycle] === 'function') {
debug(`[core]: Lifecycle run ${cycle.instance.constructor.name} ${lifecycle}`);
const result = await cycle.instance[lifecycle](this.applicationContext, this.frameworkService.getMainApp());
if (resultHandler) {
resultHandler(result);
}
}
}
}
else {
if (typeof lifecycleInstanceOrList[lifecycle] === 'function') {
debug(`[core]: Lifecycle run ${lifecycleInstanceOrList.constructor.name} ${lifecycle}`);
const result = await lifecycleInstanceOrList[lifecycle](this.applicationContext, this.frameworkService.getMainApp());
if (resultHandler) {
resultHandler(result);
}
}
}
}
async runObjectLifeCycle(lifecycleInstanceList, lifecycle) {
for (const cycle of lifecycleInstanceList) {
if (typeof cycle.instance[lifecycle] === 'function') {
debug(`[core]: Lifecycle run ${cycle.instance.constructor.name} ${lifecycle}`);
return this.applicationContext[lifecycle](cycle.instance[lifecycle].bind(cycle.instance));
}
}
}
};
__decorate([
(0, core_1.Inject)(),
__metadata("design:type", core_1.MidwayFrameworkService)
], MidwayWebLifeCycleService.prototype, "frameworkService", void 0);
__decorate([
(0, core_1.Inject)(),
__metadata("design:type", core_1.MidwayConfigService)
], MidwayWebLifeCycleService.prototype, "configService", void 0);
__decorate([
(0, core_1.Init)(),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], MidwayWebLifeCycleService.prototype, "init", null);
MidwayWebLifeCycleService = __decorate([
(0, core_1.Provide)(),
(0, core_1.Scope)(core_1.ScopeEnum.Singleton),
__metadata("design:paramtypes", [Object])
], MidwayWebLifeCycleService);
exports.MidwayWebLifeCycleService = MidwayWebLifeCycleService;
//# sourceMappingURL=lifecycle.js.map