framework: mixin: support callback style

This commit is contained in:
undefined committed 2025-03-12 15:49:04 +08:00
1 parent 51c3c4f30f
commit af1b12deed
3 files changed
+9 -8

No files matched your search

+5 -4
View File
@@ -752,8 +752,9 @@ ${c.response.status} ${endTime - startTime}ms ${c.response.length}`);
this.captureAllRoutes[prefix] = cb; this.captureAllRoutes[prefix] = cb;
} }
private _applyMixin(Target: any, MixinClass: Partial<any>) { private _applyMixin(Target: any, MixinClass: Partial<any> | ((t: Partial<any>) => Partial<any>)) {
if (!('prototype' in Target)) throw new Error('Target must be a class.'); if (!('prototype' in Target)) throw new Error('Target must be a class.');
if (typeof MixinClass === 'function') MixinClass = MixinClass(Target.prototype);
this.ctx.effect(() => { this.ctx.effect(() => {
let oldValue = null; let oldValue = null;
for (const val of Object.getOwnPropertyNames(MixinClass)) { for (const val of Object.getOwnPropertyNames(MixinClass)) {
@@ -782,15 +783,15 @@ ${c.response.status} ${endTime - startTime}ms ${c.response.length}`);
}); });
} }
public handlerMixin(MixinClass: Partial<HandlerCommon<C>>) { public handlerMixin(MixinClass: Partial<HandlerCommon<C>> | ((s: HandlerCommon<C>) => Partial<HandlerCommon<C>>)) {
return this._applyMixin(HandlerCommon, MixinClass); return this._applyMixin(HandlerCommon, MixinClass);
} }
public httpHandlerMixin(MixinClass: Partial<Handler<C>>) { public httpHandlerMixin(MixinClass: Partial<Handler<C>> | ((s: Handler<C>) => Partial<Handler<C>>)) {
return this._applyMixin(Handler, MixinClass); return this._applyMixin(Handler, MixinClass);
} }
public wsHandlerMixin(MixinClass: Partial<ConnectionHandler<C>>) { public wsHandlerMixin(MixinClass: Partial<ConnectionHandler<C>> | ((s: ConnectionHandler<C>) => Partial<ConnectionHandler<C>>)) {
return this._applyMixin(ConnectionHandler, MixinClass); return this._applyMixin(ConnectionHandler, MixinClass);
} }
+3 -3
View File
@@ -222,15 +222,15 @@ export async function apply(ctx: Context) {
} }
}, },
}); });
server.wsHandlerMixin({ server.wsHandlerMixin((s) => ({
async onerror(err: HydroError) { async onerror(err: HydroError) {
if (![NotFoundError, PrivilegeError, PermissionError].some((i) => err instanceof i) || this.user?._id !== 0) { if (![NotFoundError, PrivilegeError, PermissionError].some((i) => err instanceof i) || this.user?._id !== 0) {
logger.error(`Path:${this.request.path}, User:${this.user?._id}(${this.user?.uname})`); logger.error(`Path:${this.request.path}, User:${this.user?._id}(${this.user?.uname})`);
logger.error(err); logger.error(err);
} }
await super.onerror(err); await s.onerror(err);
}, },
}); }));
on('handler/create', async (h) => { on('handler/create', async (h) => {
h.user = h.context.HydroContext.user as any; h.user = h.context.HydroContext.user as any;
+1 -1
View File
@@ -74,7 +74,7 @@ export class ConfigService extends Service {
await this.saveConfig(newConfig); await this.saveConfig(newConfig);
} }
requestConfig<T, S>(s: Schema<T, S>, dynamic = true): ReturnType<Schema<T, S>> { requestConfig<T, S>(s: Schema<T, S>, dynamic = true): S {
if (dynamic) { if (dynamic) {
this.ctx.effect(() => { this.ctx.effect(() => {
this.settings.push(s); this.settings.push(s);