mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: use cordis to manage app lifecycle
This commit is contained in:
1 parent
d7ec26b772
commit
5b7067d77d
19 files changed
+307
-375
No files matched your search
@@ -5,10 +5,10 @@ import os from 'os';
|
||||
import path from 'path';
|
||||
import fs from 'fs-extra';
|
||||
import yaml from 'js-yaml';
|
||||
import { Context } from '../context';
|
||||
import i18n from '../lib/i18n';
|
||||
import { Logger } from '../logger';
|
||||
import * as bus from '../service/bus';
|
||||
import { Runtime } from '../service/module';
|
||||
|
||||
const logger = new Logger('common');
|
||||
|
||||
@@ -31,17 +31,14 @@ function locateFile(basePath: string, filenames: string[]) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const getLoader = (type: string, filename: string) => async function loader(pending: string[], fail: string[]) {
|
||||
const getLoader = (type: string, filename: string) => async function loader(pending: string[], fail: string[], ctx: Context) {
|
||||
for (const i of pending) {
|
||||
const p = locateFile(i, [`${filename}.ts`, `${filename}.js`]);
|
||||
if (p && !fail.includes(i)) {
|
||||
try {
|
||||
logger.info(`${type.replace(/^(.)/, (t) => t.toUpperCase())} init: %s`, i);
|
||||
const res = require(p);
|
||||
if (res.apply) {
|
||||
const runtime = new Runtime(p);
|
||||
runtime.load(res);
|
||||
}
|
||||
const m = require(p);
|
||||
if (m.apply) ctx.loader.reloadPlugin(ctx, p, {});
|
||||
else logger.info(`${type.replace(/^(.)/, (t) => t.toUpperCase())} init: %s`, i);
|
||||
} catch (e) {
|
||||
fail.push(i);
|
||||
logger.info(`${type.replace(/^(.)/, (t) => t.toUpperCase())} load fail: %s`, i);
|
||||
@@ -57,6 +54,7 @@ export const addon = getLoader('addon', 'index');
|
||||
export const model = getLoader('model', 'model');
|
||||
export const lib = getLoader('lib', 'lib');
|
||||
export const script = getLoader('script', 'script');
|
||||
export const service = getLoader('service', 'service');
|
||||
|
||||
export async function locale(pending: string[], fail: string[]) {
|
||||
await Promise.all(pending.map(async (i) => {
|
||||
@@ -142,25 +140,3 @@ export async function template(pending: string[], fail: string[]) {
|
||||
}
|
||||
await bus.serial('app/load/template');
|
||||
}
|
||||
|
||||
export async function service(pending: string[], fail: string[]) {
|
||||
for (const i of pending) {
|
||||
const p = locateFile(i, ['service.ts', 'service.js']);
|
||||
if (p && !fail.includes(i)) {
|
||||
try {
|
||||
logger.info('Service init: %s', i);
|
||||
require(p);
|
||||
} catch (e) {
|
||||
fail.push(i);
|
||||
logger.error('Service Load Fail: %s', i);
|
||||
logger.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const key in global.Hydro.service) {
|
||||
if (key === 'server') continue;
|
||||
const srv = global.Hydro.service[key];
|
||||
if (!srv.started && srv.start) await srv.start();
|
||||
}
|
||||
await bus.serial('app/load/service');
|
||||
}
|
||||
Reference in new issue
Block a user