Commit adb12069 authored by nanahira's avatar nanahira

put on apply as sync

parent 4f1205fe
import { Argv, Command, Context, Schema, User } from 'koishi'; import { Argv, Command, Context, Logger, Schema, User } from 'koishi';
import { import {
CommandPutConfig, CommandPutConfig,
DoRegisterConfig, DoRegisterConfig,
...@@ -34,7 +34,7 @@ export interface PluginClass<T = any> { ...@@ -34,7 +34,7 @@ export interface PluginClass<T = any> {
} }
export interface OnApply { export interface OnApply {
onApply(): void | Promise<void>; onApply(): void;
} }
export interface OnConnect { export interface OnConnect {
...@@ -180,7 +180,19 @@ export function KoishiPlugin<T = any>( ...@@ -180,7 +180,19 @@ export function KoishiPlugin<T = any>(
} }
} }
async _registerDeclarationsFor(methodKey: keyof C & string) { async _applyInnerPlugin(
baseContext: Context,
methodKey: keyof C & string,
) {
const pluginDesc: KoishiModulePlugin<any> = await this[methodKey]();
if (!pluginDesc || !pluginDesc.plugin) {
throw new Error(`Invalid plugin from method ${methodKey}.`);
}
const pluginCtx = applySelector(baseContext, pluginDesc);
pluginCtx.plugin(pluginDesc.plugin, pluginDesc.options);
}
_registerDeclarationsFor(methodKey: keyof C & string) {
// console.log(`Handling declaration for ${methodKey}`); // console.log(`Handling declaration for ${methodKey}`);
const regData = reflector.get(KoishiDoRegister, this, methodKey); const regData = reflector.get(KoishiDoRegister, this, methodKey);
if (!regData) { if (!regData) {
...@@ -215,12 +227,13 @@ export function KoishiPlugin<T = any>( ...@@ -215,12 +227,13 @@ export function KoishiPlugin<T = any>(
}*/ }*/
break; break;
case 'plugin': case 'plugin':
const pluginDesc: KoishiModulePlugin<any> = await this[methodKey](); this._applyInnerPlugin(baseContext, methodKey)
if (!pluginDesc || !pluginDesc.plugin) { .then()
throw new Error(`Invalid plugin from method ${methodKey}.`); .catch((e) =>
} new Logger(originalClass.name).error(
const pluginCtx = applySelector(baseContext, pluginDesc); `Failed to load plugin from method ${methodKey}: ${e.toString()}`,
pluginCtx.plugin(pluginDesc.plugin, pluginDesc.options); ),
);
break; break;
case 'command': case 'command':
const { data: commandData } = const { data: commandData } =
...@@ -259,16 +272,14 @@ export function KoishiPlugin<T = any>( ...@@ -259,16 +272,14 @@ export function KoishiPlugin<T = any>(
} }
} }
async _registerDeclarations() { _registerDeclarations() {
const methodKeys = reflector.getArray( const methodKeys = reflector.getArray(
KoishiDoRegisterKeys, KoishiDoRegisterKeys,
this, this,
) as (keyof C & string)[]; ) as (keyof C & string)[];
// console.log(methodKeys); // console.log(methodKeys);
await Promise.all( methodKeys.forEach((methodKey) =>
methodKeys.map((methodKey) => this._registerDeclarationsFor(methodKey),
this._registerDeclarationsFor(methodKey),
),
); );
} }
...@@ -304,9 +315,9 @@ export function KoishiPlugin<T = any>( ...@@ -304,9 +315,9 @@ export function KoishiPlugin<T = any>(
this._handleServiceProvide(true); this._handleServiceProvide(true);
this._handleSystemInjections(); this._handleSystemInjections();
this._handleServiceInjections(); this._handleServiceInjections();
await this._registerDeclarations(); this._registerDeclarations();
if (typeof this.onApply === 'function') { if (typeof this.onApply === 'function') {
await this.onApply(); this.onApply();
} }
this._registerAfterInit(); this._registerAfterInit();
} }
......
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