You need to sign in or sign up before continuing.
Commit 178bbd57 authored by nanahira's avatar nanahira

add UseBeforeEvent

parent c198c1c8
import { import {
BeforeEventName,
CommandConfigExtended, CommandConfigExtended,
CommandDefinitionFun, CommandDefinitionFun,
CommandPutConfig, CommandPutConfig,
...@@ -39,6 +40,12 @@ export const UseMiddleware = (prepend?: boolean): MethodDecorator => ...@@ -39,6 +40,12 @@ export const UseMiddleware = (prepend?: boolean): MethodDecorator =>
export const UseEvent = (name: EventName, prepend?: boolean): MethodDecorator => export const UseEvent = (name: EventName, prepend?: boolean): MethodDecorator =>
DoRegister(GenerateMappingStruct('onevent', { name, prepend })); DoRegister(GenerateMappingStruct('onevent', { name, prepend }));
export const UseBeforeEvent = (
name: BeforeEventName,
prepend?: boolean,
): MethodDecorator =>
DoRegister(GenerateMappingStruct('beforeEvent', { name, prepend }));
export const UsePlugin = (): MethodDecorator => export const UsePlugin = (): MethodDecorator =>
DoRegister(GenerateMappingStruct('plugin')); DoRegister(GenerateMappingStruct('plugin'));
......
import { import {
App, App,
Argv, Argv,
BeforeEventMap,
Channel, Channel,
Command, Command,
Context, Context,
...@@ -76,17 +77,24 @@ export function PluginDef<T extends keyof Modules | Plugin>( ...@@ -76,17 +77,24 @@ export function PluginDef<T extends keyof Modules | Plugin>(
return { plugin, options, select }; return { plugin, options, select };
} }
export type EventName = keyof EventMap; export interface CommonEventNameAndPrepend<T extends keyof any> {
export interface EventNameAndPrepend { name: T;
name: EventName;
prepend?: boolean; prepend?: boolean;
} }
export type EventName = keyof EventMap;
export type EventNameAndPrepend = CommonEventNameAndPrepend<EventName>;
export type BeforeEventName = keyof BeforeEventMap;
export type BeforeEventNameAndPrepend =
CommonEventNameAndPrepend<BeforeEventName>;
export type ContextFunction<T> = (ctx: Context) => T; export type ContextFunction<T> = (ctx: Context) => T;
export type OnContextFunction = ContextFunction<Context>; export type OnContextFunction = ContextFunction<Context>;
export interface DoRegisterConfigDataMap { export interface DoRegisterConfigDataMap {
middleware: boolean; // prepend middleware: boolean; // prepend
onevent: EventNameAndPrepend; onevent: EventNameAndPrepend;
beforeEvent: BeforeEventNameAndPrepend;
plugin: never; plugin: never;
command: CommandRegisterConfig; command: CommandRegisterConfig;
route: KoishiRouteDef; route: KoishiRouteDef;
......
...@@ -247,14 +247,16 @@ export function KoishiPlugin<T = any>( ...@@ -247,14 +247,16 @@ export function KoishiPlugin<T = any>(
(...args: any[]) => this[methodKey](...args), (...args: any[]) => this[methodKey](...args),
eventData.prepend, eventData.prepend,
); );
// special events
/*if (eventName === 'service') {
const serviceName = eventName.slice(8);
if (baseContext[serviceName] != null) {
this[methodKey]();
}
}*/
break; break;
case 'beforeEvent':
const { data: beforeEventData } =
regData as DoRegisterConfig<'beforeEvent'>;
const beforeEventName = beforeEventData.name;
baseContext.before(
beforeEventName,
(...args: any[]) => this[methodKey](...args),
beforeEventData.prepend,
);
case 'plugin': case 'plugin':
this._applyInnerPlugin(baseContext, methodKey); this._applyInnerPlugin(baseContext, methodKey);
break; break;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment