Commit 08760c62 authored by nanahira's avatar nanahira

pre inject everything

parent 127ae6ea
...@@ -42,6 +42,14 @@ export interface OnDisconnect { ...@@ -42,6 +42,14 @@ export interface OnDisconnect {
onDisconnect(): void | Promise<void>; onDisconnect(): void | Promise<void>;
} }
function getContextFromFilters(ctx: Context, filters: OnContextFunction[]) {
let targetCtx = ctx;
for (const fun of filters) {
targetCtx = fun(targetCtx) || targetCtx;
}
return targetCtx;
}
export function KoishiPlugin<T = any>( export function KoishiPlugin<T = any>(
options: KoishiPluginRegistrationOptions<T> = {}, options: KoishiPluginRegistrationOptions<T> = {},
) { ) {
...@@ -51,14 +59,6 @@ export function KoishiPlugin<T = any>( ...@@ -51,14 +59,6 @@ export function KoishiPlugin<T = any>(
__config: T; __config: T;
__rawConfig: T; __rawConfig: T;
_getContextFromFilters(ctx: Context, filters: OnContextFunction[]) {
let targetCtx = ctx;
for (const fun of filters) {
targetCtx = fun(targetCtx) || targetCtx;
}
return targetCtx;
}
_handleSystemInjections() { _handleSystemInjections() {
// console.log('Handling system injection'); // console.log('Handling system injection');
const injectKeys = getMetadataArray(KoishiSystemInjectSymKeys, this); const injectKeys = getMetadataArray(KoishiSystemInjectSymKeys, this);
...@@ -176,7 +176,7 @@ export function KoishiPlugin<T = any>( ...@@ -176,7 +176,7 @@ export function KoishiPlugin<T = any>(
return; return;
} }
// console.log(`Type: ${regData.type}`); // console.log(`Type: ${regData.type}`);
const baseContext = this._getContextFromFilters( const baseContext = getContextFromFilters(
this.__ctx, this.__ctx,
getMetadataArray(KoishiOnContextScope, this, methodKey), getMetadataArray(KoishiOnContextScope, this, methodKey),
); );
...@@ -294,18 +294,7 @@ export function KoishiPlugin<T = any>( ...@@ -294,18 +294,7 @@ export function KoishiPlugin<T = any>(
}); });
} }
async _initializePluginClass(ctx: Context) { async _initializePluginClass() {
const contextFilters = getMetadataArray(
KoishiOnContextScope,
originalClass,
);
this.__ctx = this._getContextFromFilters(ctx, contextFilters);
this.__config =
typeof options.schema === 'function'
? schemaTransform(options.schema, this.__rawConfig)
: options.schema
? Schema.validate(this.__rawConfig, options.schema)
: this.__rawConfig;
this._handleSystemInjections(); this._handleSystemInjections();
this._handleServiceInjections(); this._handleServiceInjections();
this._registerAfterInit(); this._registerAfterInit();
...@@ -316,10 +305,24 @@ export function KoishiPlugin<T = any>( ...@@ -316,10 +305,24 @@ export function KoishiPlugin<T = any>(
} }
constructor(...args: any[]) { constructor(...args: any[]) {
super(...args); const originalCtx: Context = args[0];
const ctx: Context = args[0]; const rawConfig = args[1];
this.__rawConfig = args[1]; const contextFilters = getMetadataArray(
this._initializePluginClass(ctx).then(); KoishiOnContextScope,
originalClass,
);
const ctx = getContextFromFilters(originalCtx, contextFilters);
const config =
typeof options.schema === 'function'
? schemaTransform(options.schema, rawConfig)
: options.schema
? Schema.validate(rawConfig, options.schema)
: rawConfig;
super(ctx, config, ...args.slice(2));
this.__ctx = ctx;
this.__rawConfig = rawConfig;
this.__config = config;
this._initializePluginClass().then();
} }
}; };
if (options.name) { if (options.name) {
......
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