Commit 50a1d676 authored by nanahira's avatar nanahira

fix reusable things

parent 11d99588
......@@ -167,3 +167,5 @@ export const MixinModel = <K extends Keys<Tables>>(
export const Fork = (forkPlugin: PluginClass) =>
Metadata.set('KoishiFork', forkPlugin);
export const Reusable = (value = true) => Metadata.set('KoishiReusable', value);
......@@ -32,6 +32,7 @@ export interface MetadataMap {
KoishiPredefineSchema: Schema | ClassType<any>;
KoishiPredefineName: string;
KoishiFork: PluginClass;
KoishiReusable: boolean;
}
export const ThirdEyeSym = Symbol('ThirdEyeSym');
......@@ -23,7 +23,7 @@ export interface KoishiPluginRegistrationOptions<T = any> {
schema?: Schema<T> | Type<T>;
Config?: Schema<T> | Type<T>;
using?: ServiceName[];
// reusable?: boolean;
reusable?: boolean;
}
export interface PluginMeta<T = any> {
......@@ -102,7 +102,7 @@ export function DefinePlugin<T>(
}
static get reusable() {
return !!getFork(newClass);
return reflector.get('KoishiReusable', newClass);
}
__ctx: Context;
......
import { RegisterSchema, SchemaProperty } from 'schemastery-gen';
import { DefinePlugin } from '../src/register';
import { ParentPluginMap, StarterPlugin } from '../src/base-plugin';
import { Fork, InjectParent, Provide } from '../src/decorators';
import { UseCommand } from 'koishi-decorators';
import { Fork, InjectParent, Provide, Reusable } from '../src/decorators';
import { Apply, UseCommand } from 'koishi-decorators';
import { App } from 'koishi';
import { Prop } from '../src/def';
......@@ -39,12 +39,33 @@ class ChildPlugin extends StarterPlugin(Config) {
async onSelfCommand() {
return this.config.getName();
}
@Apply()
increase() {
this.parent.loadCount++;
// console.log('fork loaded: ', this.parent.loadCount);
}
}
@Provide('forkTest', { immediate: true })
@DefinePlugin()
class MyPlugin extends ParentPluginMap(ChildPlugin, (p) => p.config.getName()) {
loadCount = 0;
isParent = true;
@Apply()
onLoad() {
// console.log('load', this.config);
}
}
@Reusable()
@DefinePlugin()
class MyReusablePlugin extends StarterPlugin(Config) {
@Apply()
onLoad() {
this.ctx.app['count']++;
}
}
describe('Fork', () => {
......@@ -52,18 +73,39 @@ describe('Fork', () => {
beforeEach(async () => {
app = new App();
await app.start();
app['count'] = 0;
});
it('should fork a plugin', async () => {
// console.log('before 1');
app.plugin(MyPlugin, { name: 'a' });
// console.log('after 1: ' + app.forkTest.loadCount);
const myPlugin = app.forkTest;
expect(app.forkTest.config.getName()).toEqual('a');
expect(app.forkTest.instances.get('a').config.getName()).toEqual('a');
expect(app.forkTest.instances.get('a').parent).toEqual(myPlugin);
// console.log(myPlugin.instances.get('a').parent);
// console.log(myPlugin);
expect(myPlugin.instances.get('a').parent === myPlugin).toBe(true);
expect(app.forkTest.instances.get('b')).toBeUndefined();
expect(app.forkTest.loadCount).toBe(1);
// console.log('before 2: ' + app.forkTest.loadCount);
app.plugin(MyPlugin, { name: 'b' });
// console.log('after 2: ' + app.forkTest.loadCount);
expect(app.forkTest.instances.get('b').config.getName()).toEqual('b');
expect(myPlugin.instances.get('b').parent).toEqual(app.forkTest);
// console.log(myPlugin.instances.get('b').parent);
// console.log(myPlugin);
expect(myPlugin.instances.get('b').parent === myPlugin).toBe(true);
expect(app.forkTest.loadCount).toBe(2);
// console.log('before 3: ' + app.forkTest.loadCount);
app.plugin(MyPlugin, { name: 'c' });
// console.log('after 3: ' + app.forkTest.loadCount);
expect(app.forkTest.instances.get('c').config.getName()).toEqual('c');
// console.log(myPlugin.instances.get('c').parent);
// console.log(myPlugin);
expect(myPlugin.instances.get('c').parent === myPlugin).toBe(true);
expect(app.forkTest.loadCount).toBe(3);
const commandChildA = app.command('childa');
const commandChildB = app.command('childb');
......@@ -73,6 +115,14 @@ describe('Fork', () => {
expect(await commandChildA.execute({})).toEqual('a');
expect(await commandChildB.execute({})).toEqual('b');
expect(await commandParentA.execute({})).toEqual('a');
// expect(await commandParentB.execute({})).toEqual('a');
expect(await commandParentB.execute({})).toEqual('a');
});
it('it should work on reusable', async () => {
expect(app['count']).toBe(0);
app.plugin(MyReusablePlugin, { name: 'a' });
expect(app['count']).toBe(1);
app.plugin(MyReusablePlugin, { name: 'b' });
expect(app['count']).toBe(2);
});
});
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