Commit f5d7c3c6 authored by nanahira's avatar nanahira

fix context injection

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