Skip to content

Commit

Permalink
fix(core): freeze when load route function didn't bind. #3
Browse files Browse the repository at this point in the history
  • Loading branch information
MR-MKZ authored Nov 20, 2024
2 parents 91bd726 + c02999b commit b0a5a9a
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions src/core/AxonCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import addRoutePrefix from "./utils/routePrefixHandler";
import getRequestBody from "./utils/getRequestBody";

// Types
import { Request, Response} from "..";
import { Request, Response } from "..";
import { AxonPlugin } from "../types/PluginTypes";
import { AxonCoreConfig, AxonCorsConfig } from "../types/CoreTypes";
import { Controller, HttpMethods, JsonResponse, Middleware } from "../types/GlobalTypes"
Expand Down Expand Up @@ -43,6 +43,7 @@ export default class AxonCore {
private config: AxonCoreConfig;
private configsLoaded: boolean;
private passConfig: boolean;
private passRoutes: boolean;
private routesLoaded: boolean;

private pluginLoader: PLuginLoader = new PLuginLoader();
Expand All @@ -69,6 +70,7 @@ export default class AxonCore {

this.configsLoaded = false;
this.passConfig = true;
this.passRoutes = true;
this.routesLoaded = false;
}

Expand Down Expand Up @@ -115,31 +117,35 @@ export default class AxonCore {
* @param router instance of Router which routes setted with it.
*/
async loadRoute(router: Router, prefix?: string) {
this.passRoutes = false;

let routerRoutes: HttpMethods = router.exportRoutes();

(Object.keys(routerRoutes) as Array<keyof HttpMethods>).forEach((method) => {
Object.keys(routerRoutes[method]).forEach((route) => {
if (!this.routes[method][route]) {
const originalRoute = route
if (Object.keys(routerRoutes[method]).length > 0) {
Object.keys(routerRoutes[method]).forEach((route) => {
if (!this.routes[method][route]) {
const originalRoute = route

if (prefix) {
route = addRoutePrefix(route, prefix)
}
if (prefix) {
route = addRoutePrefix(route, prefix)
}

if (route[0] !== "/")
route = `/${route}`
if (route[0] !== "/")
route = `/${route}`

if (route[route.length - 1] === "/")
route = route.slice(0, -1)
if (route[route.length - 1] === "/")
route = route.slice(0, -1)

this.routes[method][route] = routerRoutes[method][originalRoute]
this.routes['OPTIONS'][route] = routerRoutes[method][originalRoute];
this.routes[method][route] = routerRoutes[method][originalRoute]
this.routes['OPTIONS'][route] = routerRoutes[method][originalRoute];

logger.debug(`loaded route ${method} ${route}`)
} else {
routeDuplicateException(method, route)
}
})
logger.debug(`loaded route ${method} ${route}`)
} else {
routeDuplicateException(method, route)
}
})
}
})

this.routesLoaded = true;
Expand Down Expand Up @@ -362,15 +368,18 @@ export default class AxonCore {
const corePreloader = async (): Promise<void> => {
return new Promise((resolve) => {
const interval = setInterval(() => {
if (this.passConfig) {
if (this.passConfig && !this.passRoutes) {
if (this.routesLoaded) {
logger.info("all routes loaded!");
logger.info("all routes loaded");
clearInterval(interval);
resolve();
}
} else {
if (this.routesLoaded && this.configsLoaded) {
logger.info("all configs and routes loaded!");
logger.info("all configs and routes loaded");
clearInterval(interval);
resolve();
} else if (this.passRoutes) {
clearInterval(interval);
resolve();
}
Expand Down

0 comments on commit b0a5a9a

Please sign in to comment.