@halsp/core
Version:
面向云的现代渐进式轻量 Node.js 框架
97 lines • 3.61 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.execHooks = exports.execInitializationHooks = exports.execBeginingHooks = exports.execUnhandledHooks = exports.execErrorHooks = exports.execConstructorHooks = exports.execContextHooks = void 0;
const context_1 = require("../context.cjs");
const middlewares_1 = require("../middlewares/index.cjs");
const hook_item_1 = require("./hook.item.cjs");
const hook_manager_1 = require("./hook.manager.cjs");
async function execContextHooks(startup, args) {
const hooks = hook_manager_1.HookManager.getGlobalHooks(startup, hook_item_1.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_1.Context) {
result = args[0];
}
else if (args[0] instanceof context_1.Request) {
result = new context_1.Context(args[0]);
}
else {
result = new context_1.Context();
}
}
return result;
}
exports.execContextHooks = execContextHooks;
async function execConstructorHooks(ctx, middleware) {
const hooks = hook_manager_1.HookManager.getHooks(ctx, hook_item_1.HookType.Constructor);
let result;
for (const hookItem of hooks) {
if (!(middleware instanceof middlewares_1.Middleware)) {
const hook = hookItem.hook;
result = await hook(ctx, middleware);
if (result)
break;
}
}
if (!result)
result = new middleware();
return result;
}
exports.execConstructorHooks = execConstructorHooks;
async function execErrorHooks(ctx, middleware, error) {
const hooks = hook_manager_1.HookManager.getHooks(ctx, hook_item_1.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;
}
exports.execErrorHooks = execErrorHooks;
async function execUnhandledHooks(ctx, middleware, error) {
const hooks = hook_manager_1.HookManager.getHooks(ctx, hook_item_1.HookType.Unhandled);
for (const hookItem of hooks) {
const hook = hookItem.hook;
await hook(ctx, middleware, error);
}
}
exports.execUnhandledHooks = execUnhandledHooks;
async function execBeginingHooks(ctx) {
const hooks = hook_manager_1.HookManager.getGlobalHooks(ctx.startup, hook_item_1.HookType.Begining);
for (const hookItem of hooks) {
const hook = hookItem.hook;
const hookResult = await hook(ctx);
if (typeof hookResult == "boolean" && !hookResult) {
return false;
}
}
}
exports.execBeginingHooks = execBeginingHooks;
async function execInitializationHooks(startup, args) {
const hooks = hook_manager_1.HookManager.getGlobalHooks(startup, hook_item_1.HookType.Initialization);
for (const hookItem of hooks) {
const hook = hookItem.hook;
await hook(args);
}
}
exports.execInitializationHooks = execInitializationHooks;
async function execHooks(ctx, type, middleware) {
const hooks = hook_manager_1.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;
}
}
}
exports.execHooks = execHooks;
//# sourceMappingURL=hook.exec.js.map
;