@halsp/core
Version:
面向云的现代渐进式轻量 Node.js 框架
87 lines • 2.81 kB
JavaScript
import { Context, Request } from "../context.mjs";
import { Middleware } from "../middlewares/index.mjs";
import { HookType, } from "./hook.item.mjs";
import { HookManager } from "./hook.manager.mjs";
export async function execContextHooks(startup, args) {
const hooks = HookManager.getGlobalHooks(startup, HookType.Context);
let result;
for (const hookItem of hooks) {
const hook = hookItem.hook;
result = (await hook(args));
if (result)
break;
}
if (!result) {
if (args[0] instanceof Context) {
result = args[0];
}
else if (args[0] instanceof Request) {
result = new Context(args[0]);
}
else {
result = new Context();
}
}
return result;
}
export async function execConstructorHooks(ctx, middleware) {
const hooks = HookManager.getHooks(ctx, HookType.Constructor);
let result;
for (const hookItem of hooks) {
if (!(middleware instanceof Middleware)) {
const hook = hookItem.hook;
result = await hook(ctx, middleware);
if (result)
break;
}
}
if (!result)
result = new middleware();
return result;
}
export async function execErrorHooks(ctx, middleware, error) {
const hooks = HookManager.getHooks(ctx, HookType.Error);
let result = false;
for (const hookItem of hooks) {
const hook = hookItem.hook;
result = await hook(ctx, middleware, error);
if (result)
break;
}
return result;
}
export async function execUnhandledHooks(ctx, middleware, error) {
const hooks = HookManager.getHooks(ctx, HookType.Unhandled);
for (const hookItem of hooks) {
const hook = hookItem.hook;
await hook(ctx, middleware, error);
}
}
export async function execBeginingHooks(ctx) {
const hooks = HookManager.getGlobalHooks(ctx.startup, HookType.Begining);
for (const hookItem of hooks) {
const hook = hookItem.hook;
const hookResult = await hook(ctx);
if (typeof hookResult == "boolean" && !hookResult) {
return false;
}
}
}
export async function execInitializationHooks(startup, args) {
const hooks = HookManager.getGlobalHooks(startup, HookType.Initialization);
for (const hookItem of hooks) {
const hook = hookItem.hook;
await hook(args);
}
}
export async function execHooks(ctx, type, middleware) {
const hooks = HookManager.getHooks(ctx, type);
for (const hookItem of hooks) {
const hook = hookItem.hook;
const hookResult = await hook(ctx, middleware);
if (typeof hookResult == "boolean" && !hookResult) {
return false;
}
}
}
//# sourceMappingURL=hook.exec.js.map