@busy-hour/blaze
Version:
<h1 align='center'>🔥 Blaze</h1> <div align='center'> An event driven framework for 🔥 Hono.js </div>
133 lines (132 loc) • 3.61 kB
JavaScript
// src/loader/service.ts
import path from "node:path";
import { getRestPath, getServiceName } from "../extractor/service/index.js";
import { BlazeEvent } from "../internal/index.js";
import { BlazeRouter } from "../router/BlazeRouter.js";
import { loadService } from "../utils/helper/service.js";
import { BlazeServiceAction } from "./action.js";
import { BlazeServiceEvent } from "./event.js";
import { BlazeServiceRest } from "./rest.js";
var BlazeService = class _BlazeService {
servicePath;
serviceName;
restPath;
mainRouter;
actions;
events;
rests;
handlers;
middlewares;
router;
ctx;
service;
isStarted;
constructor(options) {
const { service, ctx, servicePath, app, middlewares } = options;
this.service = service;
this.servicePath = servicePath;
this.ctx = ctx;
this.serviceName = getServiceName(service);
this.restPath = getRestPath(service);
this.mainRouter = app;
this.isStarted = false;
this.actions = [];
this.events = [];
this.rests = [];
this.handlers = [];
this.middlewares = service.middlewares ?? [];
if (middlewares) {
this.middlewares.unshift(...middlewares);
}
this.router = null;
this.loadServiceActions();
this.loadServiceEvents();
}
loadRest(action) {
if (!this.router) {
this.router = new BlazeRouter({
router: this.service.router
});
}
const restInstance = new BlazeServiceRest({
action,
router: this.router,
service: this.service,
middlewares: this.middlewares
});
this.rests.push(restInstance);
}
loadServiceActions() {
if (!this.service.actions)
return this.actions;
for (const actionAlias in this.service.actions) {
const action = this.service.actions[actionAlias];
if (action.rest)
this.loadRest(action);
const instance = new BlazeServiceAction({
action,
actionAlias,
serviceName: this.serviceName
});
this.actions.push(instance);
}
return this.actions;
}
loadServiceEvents() {
if (!this.service.events)
return this.events;
for (const eventAlias in this.service.events) {
const event = this.service.events[eventAlias];
const instance = new BlazeServiceEvent({
event,
eventAlias,
serviceName: this.serviceName
});
this.events.push(instance);
}
return this.events;
}
assignRestRoute() {
if (!this.router)
return;
this.mainRouter.route(`/${this.restPath}`, this.router);
}
onStopped() {
this.service.onStopped?.(this.handlers);
this.actions.forEach((action) => {
BlazeEvent.off(action.actionName, action.actionHandler);
});
}
onRestarted() {
this.rests.forEach((rest) => {
const method = rest.method ?? "ALL";
this.router?.on?.(method, rest.path, rest.restHandler);
});
this.actions.forEach((action) => {
BlazeEvent.offAll(action.actionName);
BlazeEvent.on(action.actionName, action.actionHandler);
});
}
onStarted() {
if (this.isStarted)
return;
this.assignRestRoute();
this.service.onStarted?.(this.ctx);
this.isStarted = true;
}
static async create(options) {
const servicePath = path.resolve(options.sourcePath, options.servicePath);
const serviceFile = await loadService(servicePath);
const service = new _BlazeService({
app: options.app,
ctx: options.ctx,
servicePath,
service: serviceFile,
middlewares: options.middlewares
});
return service;
}
};
export {
BlazeService
};