mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: handle Handler registration on multi paths
This commit is contained in:
1 parent
84309e86fd
commit
1e62a9b027
2 files changed
+12
-6
No files matched your search
@@ -8,6 +8,7 @@ import superagent from 'superagent';
|
|||||||
import { Context } from './context';
|
import { Context } from './context';
|
||||||
import db from './service/db';
|
import db from './service/db';
|
||||||
export { ObjectID, ObjectId, FilterQuery } from 'mongodb';
|
export { ObjectID, ObjectId, FilterQuery } from 'mongodb';
|
||||||
|
export { WebSocket, WebSocketServer } from 'ws';
|
||||||
export * from './utils';
|
export * from './utils';
|
||||||
export * from './interface';
|
export * from './interface';
|
||||||
export * from './pipelineUtils';
|
export * from './pipelineUtils';
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import Compress from 'koa-compress';
|
|||||||
import proxy from 'koa-proxies';
|
import proxy from 'koa-proxies';
|
||||||
import cache from 'koa-static-cache';
|
import cache from 'koa-static-cache';
|
||||||
import WebSocket from 'ws';
|
import WebSocket from 'ws';
|
||||||
import { isClass, parseMemoryMB } from '@hydrooj/utils/lib/utils';
|
import { Counter, isClass, parseMemoryMB } from '@hydrooj/utils/lib/utils';
|
||||||
import { Context, Service } from '../context';
|
import { Context, Service } from '../context';
|
||||||
import {
|
import {
|
||||||
HydroError, InvalidOperationError, MethodNotAllowedError,
|
HydroError, InvalidOperationError, MethodNotAllowedError,
|
||||||
@@ -389,6 +389,7 @@ class NotFoundHandler extends Handler {
|
|||||||
export class RouteService extends Service {
|
export class RouteService extends Service {
|
||||||
static readonly methods = ['Route', 'Connection', 'withHandlerClass'];
|
static readonly methods = ['Route', 'Connection', 'withHandlerClass'];
|
||||||
private registry = {};
|
private registry = {};
|
||||||
|
private registrationCount = Counter();
|
||||||
|
|
||||||
constructor(ctx) {
|
constructor(ctx) {
|
||||||
super(ctx, 'server', true);
|
super(ctx, 'server', true);
|
||||||
@@ -396,15 +397,19 @@ export class RouteService extends Service {
|
|||||||
|
|
||||||
private register(func: typeof Route | typeof Connection, ...args: Parameters<typeof Route>) {
|
private register(func: typeof Route | typeof Connection, ...args: Parameters<typeof Route>) {
|
||||||
const HandlerClass = args[2];
|
const HandlerClass = args[2];
|
||||||
|
const name = HandlerClass.name;
|
||||||
if (!isClass(HandlerClass)) throw new Error('Invalid registration.');
|
if (!isClass(HandlerClass)) throw new Error('Invalid registration.');
|
||||||
if (this.registry[HandlerClass.name]) logger.warn('Route name with %s already exists.', HandlerClass.name);
|
if (this.registrationCount[name] && this.registry[name] !== HandlerClass) {
|
||||||
this.registry[HandlerClass.name] = HandlerClass;
|
logger.warn('Route with name %s already exists.', name);
|
||||||
const dispose = () => delete this.registry[HandlerClass.name];
|
}
|
||||||
|
this.registry[name] = HandlerClass;
|
||||||
|
this.registrationCount[name]++;
|
||||||
const res = func(...args);
|
const res = func(...args);
|
||||||
this.ctx.parallel(`handler/register/${HandlerClass.name}`, HandlerClass);
|
this.ctx.parallel(`handler/register/${name}`, HandlerClass);
|
||||||
this.caller?.on('dispose', () => {
|
this.caller?.on('dispose', () => {
|
||||||
|
this.registrationCount[name]--;
|
||||||
|
if (!this.registrationCount[name]) delete this.registry[name];
|
||||||
res();
|
res();
|
||||||
dispose();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user