Commit a1ad3565 authored by nanahira's avatar nanahira

update partial register details

parent f337c600
...@@ -9,11 +9,11 @@ import { ClonePlugin } from '../utility/clone-plugin'; ...@@ -9,11 +9,11 @@ import { ClonePlugin } from '../utility/clone-plugin';
import { UseEvent } from 'koishi-decorators'; import { UseEvent } from 'koishi-decorators';
type MapPluginToConfig<M extends Dict<PluginClass>> = { type MapPluginToConfig<M extends Dict<PluginClass>> = {
[K in keyof M]?: ClassPluginConfig<M[K]> & Selection; [K in keyof M]: ClassPluginConfig<M[K]> & Selection;
}; };
export class MapPluginBase<M extends Dict<PluginClass>> export class MapPluginBase<M extends Dict<PluginClass>>
extends BasePlugin<MapPluginToConfig<M>, MapPluginToConfig<M>> extends BasePlugin<MapPluginToConfig<M>, Partial<MapPluginToConfig<M>>>
implements LifecycleEvents implements LifecycleEvents
{ {
_getDict(): M { _getDict(): M {
...@@ -28,7 +28,7 @@ export class MapPluginBase<M extends Dict<PluginClass>> ...@@ -28,7 +28,7 @@ export class MapPluginBase<M extends Dict<PluginClass>>
onApply() { onApply() {
const dict = this._getDict(); const dict = this._getDict();
for (const [key, plugin] of Object.entries(dict)) { for (const [key, plugin] of Object.entries(dict)) {
if (this.config[key]?.[NoRegisterSym]) continue; if (this.config[key] == null) continue;
const ctx = const ctx =
typeof this.config[key] === 'object' typeof this.config[key] === 'object'
? this.ctx.select(this.config[key]) ? this.ctx.select(this.config[key])
...@@ -47,15 +47,12 @@ export class MapPluginBase<M extends Dict<PluginClass>> ...@@ -47,15 +47,12 @@ export class MapPluginBase<M extends Dict<PluginClass>>
delete this._instanceMap; delete this._instanceMap;
} }
} }
const NoRegisterSym = '__no_register' as const;
function MappedConfig<M extends Dict<PluginClass>>( function MappedConfig<M extends Dict<PluginClass>>(
dict: M, dict: M,
): ClassType<MapPluginToConfig<M>> { ): ClassType<MapPluginToConfig<M>> {
const PropertySchema: ClassType< const PropertySchema = class SpecificPropertySchema {} as ClassType<
MapPluginToConfig<M> MapPluginToConfig<M>
> = class SpecificPropertySchema {}; >;
for (const [key, plugin] of Object.entries(dict)) { for (const [key, plugin] of Object.entries(dict)) {
const propertySchemaClass = const propertySchemaClass =
plugin['Config'] || plugin['Config'] ||
...@@ -63,7 +60,6 @@ function MappedConfig<M extends Dict<PluginClass>>( ...@@ -63,7 +60,6 @@ function MappedConfig<M extends Dict<PluginClass>>(
reflector.get('KoishiPredefineSchema', plugin); reflector.get('KoishiPredefineSchema', plugin);
SchemaProperty({ SchemaProperty({
type: propertySchemaClass, type: propertySchemaClass,
default: { [NoRegisterSym]: true },
})(PropertySchema.prototype, key); })(PropertySchema.prototype, key);
} }
return PropertySchema; return PropertySchema;
......
import { RegisterSchema, SchemaProperty } from '..'; import { RegisterSchema, SchemaProperty } from '..';
import { Assets, Bot, Cache, Context } from 'koishi'; import { Assets, Bot, Cache, Context } from 'koishi';
import { PluginName, UsingService, Inject, PluginSchema } from '../src/decorators'; import {
Inject,
PluginName,
PluginSchema,
UsingService,
} from '../src/decorators';
import { DefinePlugin } from '../src/register'; import { DefinePlugin } from '../src/register';
@RegisterSchema() @RegisterSchema()
......
...@@ -2,7 +2,7 @@ import { SchemaProperty } from 'schemastery-gen'; ...@@ -2,7 +2,7 @@ import { SchemaProperty } from 'schemastery-gen';
import { StarterPlugin } from '../src/base-plugin'; import { StarterPlugin } from '../src/base-plugin';
import { DefinePlugin } from '../src/register'; import { DefinePlugin } from '../src/register';
import { UseCommand } from 'koishi-decorators'; import { UseCommand } from 'koishi-decorators';
import { MapPlugin } from '../src/plugin-operators/map-plugin'; import { MapPlugin } from '../src/plugin-operators';
import { App } from 'koishi'; import { App } from 'koishi';
class DressConfig { class DressConfig {
...@@ -69,6 +69,6 @@ describe('register map plugin instance', () => { ...@@ -69,6 +69,6 @@ describe('register map plugin instance', () => {
await app.start(); await app.start();
expect(await app.command('dressColor').execute({})).toBe('red'); expect(await app.command('dressColor').execute({})).toBe('red');
expect(await app.command('wearingStrip').execute({})).toBe('pink'); expect(await app.command('wearingStrip').execute({})).toBe('pink');
expect(await app.command('skirtSize').execute({})).toBeFalsy(); expect(await app.command('skirtSize').execute({})).toBe('S');
}); });
}); });
...@@ -8,8 +8,8 @@ import { ...@@ -8,8 +8,8 @@ import {
import { import {
Inject, Inject,
InjectContext, InjectContext,
Provide,
InjectLogger, InjectLogger,
Provide,
} from '../src/decorators'; } from '../src/decorators';
declare module 'koishi' { declare module 'koishi' {
......
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