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

fix context injection

parent e47ea73e
......@@ -549,6 +549,10 @@ export class AppModule {}
## 更新历史
### 1.5.3
* 修复了一个 BUG ,这个 BUG 曾经导致: 在提供者类注入的 Koishi 上下文不受提供者作用域装饰器的约束。
### 1.5
* 增加了指令拦截器。
......
import { Inject, Injectable, Type } from '@nestjs/common';
import { KOISHI_MODULE_OPTIONS } from '../utility/koishi.constants';
import {
KOISHI_MODULE_OPTIONS,
KoishiOnContextScope,
} from '../utility/koishi.constants';
import {
KoishiModuleOptions,
KoishiModuleSelection,
......@@ -7,11 +10,16 @@ import {
import { applySelector } from '../utility/koishi.utility';
import { Context } from 'koishi';
import { Module } from '@nestjs/core/injector/module';
import { KoishiMetadataFetcherService } from '../koishi-metadata-fetcher/koishi-metadata-fetcher.service';
import _ from 'lodash';
@Injectable()
export class KoishiContextService {
moduleSelections = new Map<Type<any>, KoishiModuleSelection>();
constructor(@Inject(KOISHI_MODULE_OPTIONS) options: KoishiModuleOptions) {
constructor(
@Inject(KOISHI_MODULE_OPTIONS) options: KoishiModuleOptions,
private readonly metaFetcher: KoishiMetadataFetcherService,
) {
if (options.moduleSelection) {
for (const selection of options.moduleSelection) {
this.moduleSelections.set(selection.module, selection);
......@@ -27,4 +35,16 @@ export class KoishiContextService {
return ctx;
}
}
getProviderCtx(ctx: Context, ...instances: any[]) {
const contextFilters = _.flatten(
instances.map((instance) =>
this.metaFetcher.getMetadataArray(KoishiOnContextScope, instance),
),
);
for (const filter of contextFilters) {
ctx = filter(ctx) || ctx;
}
return ctx;
}
}
......@@ -34,6 +34,7 @@ export class KoishiInjectionService {
ctx = this.ctxService.getModuleCtx(ctx, module);
}
}
ctx = this.ctxService.getProviderCtx(ctx, token);
return ctx;
}
}
......@@ -131,10 +131,9 @@ export class KoishiMetascanService {
return;
}
let baseContext = ctx;
const contextFilters = this.metaFetcher.getPropertyMetadataArray(
const contextFilters = this.metaFetcher.getMetadataArray(
KoishiOnContextScope,
instance,
methodKey,
instance[methodKey],
);
for (const filter of contextFilters) {
baseContext = filter(baseContext) || baseContext;
......@@ -293,11 +292,19 @@ export class KoishiMetascanService {
return allProviders.map((wrapper: InstanceWrapper) => {
const { instance } = wrapper;
const prototype = Object.getPrototypeOf(instance);
const providerCtx = this.ctxService.getProviderCtx(
moduleCtx,
instance.constructor,
);
return this.metadataScanner.scanFromPrototype(
instance,
prototype,
(methodKey: string) =>
this.handleInstanceRegistration(moduleCtx, instance, methodKey),
this.handleInstanceRegistration(
providerCtx,
instance,
methodKey,
),
);
});
}),
......
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