Commit d3214ef9 authored by nanahira's avatar nanahira

add @Apply and resolve promise

parent 91a2bc57
......@@ -11,7 +11,7 @@
"dependencies": {
"@types/koa": "^2.13.4",
"@types/koa__router": "^8.0.11",
"koishi-decorators": "^2.1.0",
"koishi-decorators": "^2.1.2",
"lodash": "^4.17.21",
"minato-decorators": "^2.0.7",
"reflect-metadata": "^0.1.13",
......@@ -38,7 +38,7 @@
"ws": "^8.2.3"
},
"peerDependencies": {
"koishi": "^4.7.1",
"koishi": "^4.7.2",
"schemastery": "^3.4.3"
}
},
......@@ -1028,13 +1028,13 @@
}
},
"node_modules/@koishijs/core": {
"version": "4.7.1",
"resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.7.1.tgz",
"integrity": "sha512-vSd2BgyhDrTW/C7Ju1wDr5Z3vp7Qdyk7LgSpGI/b3QtDEfEkhdYKQ9J9LdHIVdUOFL0L0WeRgImW4ZMBtdVlfg==",
"version": "4.7.2",
"resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.7.2.tgz",
"integrity": "sha512-yO5Pyki1RV+MSqR1IzX02z6wLlJRej7RSH7zW4/+nd5r1fimtDaODiQz/F7lGfcuVthIPDg2aPxJXkl8eLCoSw==",
"peer": true,
"dependencies": {
"@koishijs/utils": "^5.4.5",
"cordis": "^1.1.1",
"cordis": "^1.2.2",
"fastest-levenshtein": "^1.0.12",
"minato": "^1.1.0"
},
......@@ -1055,24 +1055,6 @@
"koishi": "^4.7.1"
}
},
"node_modules/@koishijs/plugin-help": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/@koishijs/plugin-help/-/plugin-help-1.0.0.tgz",
"integrity": "sha512-SvOZiSmtysQxnhOUG0He+/Ftxr+IuIbTApsK6KypRpQ4a1q7OuJWmKb2e+f62zS3GhMYBJ8qT0wRMU+TFPP3AQ==",
"peer": true,
"peerDependencies": {
"koishi": "^4.7.1"
}
},
"node_modules/@koishijs/plugin-suggest": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/@koishijs/plugin-suggest/-/plugin-suggest-1.0.0.tgz",
"integrity": "sha512-6jmhymcygID59Z3UblAARGXflJoH0+Tj/qi20GNQO0bxrKkoW4qjxJhlmZe2uv+D1hvTO0q2Yv98iEMDhKyXxw==",
"peer": true,
"peerDependencies": {
"koishi": "^4.7.1"
}
},
"node_modules/@koishijs/segment": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/@koishijs/segment/-/segment-1.1.1.tgz",
......@@ -2316,9 +2298,9 @@
"peer": true
},
"node_modules/cordis": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/cordis/-/cordis-1.1.1.tgz",
"integrity": "sha512-l1tYxBqy+oEb3EKEvQAlH1g5BICDdnTdcIre1MuCY/qWO4bsrh97qd7jQe2Rq/+tg5HRqs7K6RgP5N/85c57xw==",
"version": "1.2.2",
"resolved": "https://registry.npmjs.org/cordis/-/cordis-1.2.2.tgz",
"integrity": "sha512-R6RhHuBu6be5DGXBkIGyw3W/yPBPqr69KBVEGVgBY38DzMBIXilmsfAdVdIIEe3G0vlnn5rtAgsMg2yGwRpkiQ==",
"peer": true,
"dependencies": {
"cosmokit": "^1.1.2"
......@@ -4784,15 +4766,13 @@
}
},
"node_modules/koishi": {
"version": "4.7.1",
"resolved": "https://registry.npmjs.org/koishi/-/koishi-4.7.1.tgz",
"integrity": "sha512-WtY2ZujFMP4/ySKCc08oXLe2dYlZsdZP7f2eUck1w3cdvOh3eAgndckBP7xpkICAL0l6BYxrbzurIYh32Qck6g==",
"version": "4.7.2",
"resolved": "https://registry.npmjs.org/koishi/-/koishi-4.7.2.tgz",
"integrity": "sha512-oH1yIGX8skfPO4WcLPpnNqt+qzETVux7OIAK9jkMQxHLJv1fq1/OqsLS0wTvpTJ8qFZLt49lZ3gbLujga43RPA==",
"peer": true,
"dependencies": {
"@koa/router": "^10.1.1",
"@koishijs/core": "^4.7.1",
"@koishijs/plugin-help": "^1.0.0",
"@koishijs/plugin-suggest": "^1.0.0",
"@koishijs/core": "^4.7.2",
"@koishijs/utils": "^5.4.5",
"@types/koa": "*",
"@types/koa__router": "*",
......@@ -4803,7 +4783,7 @@
"koa-bodyparser": "^4.3.0",
"ns-require": "^1.1.2",
"parseurl": "^1.3.3",
"path-to-regexp": "^6.2.0",
"path-to-regexp": "^6.2.1",
"portfinder": "^1.0.28",
"proxy-agent": "^5.0.0",
"ws": "^8.6.0"
......@@ -4813,9 +4793,9 @@
}
},
"node_modules/koishi-decorators": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/koishi-decorators/-/koishi-decorators-2.1.0.tgz",
"integrity": "sha512-b0opV0YfWXH7/sJ7ecBjPWhz9PsL4esQ1J8EUfymTcZE5jhiTjKhvp+TEdlEC+bDTM49LwMSnohGMU8nZxaD+g==",
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/koishi-decorators/-/koishi-decorators-2.1.2.tgz",
"integrity": "sha512-32pFPob5FBhAcQ6qz02hGFs6MLj3k+8mpki8HjG4PD8RF8WeuKhrWA7SlVg6Clw3NvfU/lEyqGcnIavwiUF2Tg==",
"dependencies": {
"@types/koa": "^2.13.4",
"@types/koa__router": "^8.0.11",
......@@ -4825,7 +4805,7 @@
"typed-reflector": "^1.0.10"
},
"peerDependencies": {
"koishi": "^4.7.1"
"koishi": "^4.7.2"
}
},
"node_modules/leven": {
......@@ -5361,9 +5341,9 @@
"dev": true
},
"node_modules/path-to-regexp": {
"version": "6.2.0",
"resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-6.2.0.tgz",
"integrity": "sha512-f66KywYG6+43afgE/8j/GoiNyygk/bnoCbps++3ErRKsIYkGGupyv07R2Ok5m9i67Iqc+T2g1eAUGUPzWhYTyg==",
"version": "6.2.1",
"resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-6.2.1.tgz",
"integrity": "sha512-JLyh7xT1kizaEvcaXOQwOc2/Yhw6KZOvPf1S8401UyLk86CU79LN3vl7ztXGm/pZ+YjoyAJ4rxmHwbkBXJX+yw==",
"peer": true
},
"node_modules/path-type": {
......@@ -7710,13 +7690,13 @@
}
},
"@koishijs/core": {
"version": "4.7.1",
"resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.7.1.tgz",
"integrity": "sha512-vSd2BgyhDrTW/C7Ju1wDr5Z3vp7Qdyk7LgSpGI/b3QtDEfEkhdYKQ9J9LdHIVdUOFL0L0WeRgImW4ZMBtdVlfg==",
"version": "4.7.2",
"resolved": "https://registry.npmjs.org/@koishijs/core/-/core-4.7.2.tgz",
"integrity": "sha512-yO5Pyki1RV+MSqR1IzX02z6wLlJRej7RSH7zW4/+nd5r1fimtDaODiQz/F7lGfcuVthIPDg2aPxJXkl8eLCoSw==",
"peer": true,
"requires": {
"@koishijs/utils": "^5.4.5",
"cordis": "^1.1.1",
"cordis": "^1.2.2",
"fastest-levenshtein": "^1.0.12",
"minato": "^1.1.0"
}
......@@ -7731,20 +7711,6 @@
"ws": "^8.6.0"
}
},
"@koishijs/plugin-help": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/@koishijs/plugin-help/-/plugin-help-1.0.0.tgz",
"integrity": "sha512-SvOZiSmtysQxnhOUG0He+/Ftxr+IuIbTApsK6KypRpQ4a1q7OuJWmKb2e+f62zS3GhMYBJ8qT0wRMU+TFPP3AQ==",
"peer": true,
"requires": {}
},
"@koishijs/plugin-suggest": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/@koishijs/plugin-suggest/-/plugin-suggest-1.0.0.tgz",
"integrity": "sha512-6jmhymcygID59Z3UblAARGXflJoH0+Tj/qi20GNQO0bxrKkoW4qjxJhlmZe2uv+D1hvTO0q2Yv98iEMDhKyXxw==",
"peer": true,
"requires": {}
},
"@koishijs/segment": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/@koishijs/segment/-/segment-1.1.1.tgz",
......@@ -8749,9 +8715,9 @@
"peer": true
},
"cordis": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/cordis/-/cordis-1.1.1.tgz",
"integrity": "sha512-l1tYxBqy+oEb3EKEvQAlH1g5BICDdnTdcIre1MuCY/qWO4bsrh97qd7jQe2Rq/+tg5HRqs7K6RgP5N/85c57xw==",
"version": "1.2.2",
"resolved": "https://registry.npmjs.org/cordis/-/cordis-1.2.2.tgz",
"integrity": "sha512-R6RhHuBu6be5DGXBkIGyw3W/yPBPqr69KBVEGVgBY38DzMBIXilmsfAdVdIIEe3G0vlnn5rtAgsMg2yGwRpkiQ==",
"peer": true,
"requires": {
"cosmokit": "^1.1.2"
......@@ -10608,15 +10574,13 @@
}
},
"koishi": {
"version": "4.7.1",
"resolved": "https://registry.npmjs.org/koishi/-/koishi-4.7.1.tgz",
"integrity": "sha512-WtY2ZujFMP4/ySKCc08oXLe2dYlZsdZP7f2eUck1w3cdvOh3eAgndckBP7xpkICAL0l6BYxrbzurIYh32Qck6g==",
"version": "4.7.2",
"resolved": "https://registry.npmjs.org/koishi/-/koishi-4.7.2.tgz",
"integrity": "sha512-oH1yIGX8skfPO4WcLPpnNqt+qzETVux7OIAK9jkMQxHLJv1fq1/OqsLS0wTvpTJ8qFZLt49lZ3gbLujga43RPA==",
"peer": true,
"requires": {
"@koa/router": "^10.1.1",
"@koishijs/core": "^4.7.1",
"@koishijs/plugin-help": "^1.0.0",
"@koishijs/plugin-suggest": "^1.0.0",
"@koishijs/core": "^4.7.2",
"@koishijs/utils": "^5.4.5",
"@types/koa": "*",
"@types/koa__router": "*",
......@@ -10627,16 +10591,16 @@
"koa-bodyparser": "^4.3.0",
"ns-require": "^1.1.2",
"parseurl": "^1.3.3",
"path-to-regexp": "^6.2.0",
"path-to-regexp": "^6.2.1",
"portfinder": "^1.0.28",
"proxy-agent": "^5.0.0",
"ws": "^8.6.0"
}
},
"koishi-decorators": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/koishi-decorators/-/koishi-decorators-2.1.0.tgz",
"integrity": "sha512-b0opV0YfWXH7/sJ7ecBjPWhz9PsL4esQ1J8EUfymTcZE5jhiTjKhvp+TEdlEC+bDTM49LwMSnohGMU8nZxaD+g==",
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/koishi-decorators/-/koishi-decorators-2.1.2.tgz",
"integrity": "sha512-32pFPob5FBhAcQ6qz02hGFs6MLj3k+8mpki8HjG4PD8RF8WeuKhrWA7SlVg6Clw3NvfU/lEyqGcnIavwiUF2Tg==",
"requires": {
"@types/koa": "^2.13.4",
"@types/koa__router": "^8.0.11",
......@@ -11058,9 +11022,9 @@
"dev": true
},
"path-to-regexp": {
"version": "6.2.0",
"resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-6.2.0.tgz",
"integrity": "sha512-f66KywYG6+43afgE/8j/GoiNyygk/bnoCbps++3ErRKsIYkGGupyv07R2Ok5m9i67Iqc+T2g1eAUGUPzWhYTyg==",
"version": "6.2.1",
"resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-6.2.1.tgz",
"integrity": "sha512-JLyh7xT1kizaEvcaXOQwOc2/Yhw6KZOvPf1S8401UyLk86CU79LN3vl7ztXGm/pZ+YjoyAJ4rxmHwbkBXJX+yw==",
"peer": true
},
"path-type": {
......
......@@ -50,7 +50,7 @@
"dependencies": {
"@types/koa": "^2.13.4",
"@types/koa__router": "^8.0.11",
"koishi-decorators": "^2.1.0",
"koishi-decorators": "^2.1.2",
"lodash": "^4.17.21",
"minato-decorators": "^2.0.7",
"reflect-metadata": "^0.1.13",
......@@ -75,7 +75,7 @@
"testEnvironment": "node"
},
"peerDependencies": {
"koishi": "^4.7.1",
"koishi": "^4.7.2",
"schemastery": "^3.4.3"
}
}
......@@ -3,7 +3,7 @@ import { PluginClass } from '../../def';
import { BasePlugin, PartialDeep } from '../../base-plugin';
import { LifecycleEvents } from '../../register';
import { ClonePlugin } from '../../utility/clone-plugin';
import { UseEvent } from 'koishi-decorators';
import { Apply, UseEvent } from 'koishi-decorators';
export class MappingPluginBase<
M extends Dict<PluginClass>,
......@@ -26,7 +26,8 @@ export class MappingPluginBase<
return {};
}
onApply() {
@Apply()
_registerInstances() {
const dict = this._getDict();
for (const [key, plugin] of Object.entries(dict)) {
const config = this._getPluginConfig(key);
......@@ -42,6 +43,8 @@ export class MappingPluginBase<
}
}
onApply() {}
@UseEvent('dispose')
_onThingsDispose() {
delete this._instanceMap;
......
......@@ -12,7 +12,7 @@ import { ClassType } from 'schemastery-gen';
import { ToInstancesConfig } from '../utility/to-instance-config';
import Schema from 'schemastery';
import { UsingService } from '../decorators';
import { UseEvent } from 'koishi-decorators';
import { Apply, UseEvent } from 'koishi-decorators';
import { CreatePluginFactory } from '../plugin-factory';
import { LifecycleEvents } from '../register';
......@@ -29,6 +29,7 @@ export class MultiInstancePluginFramework<InnerPlugin extends PluginClass>
throw new Error(`Not implemented`);
}
@Apply()
_registerInstances() {
const innerPlugin = this._getInnerPlugin();
for (let i = 0; i < this.config.instances.length; i++) {
......@@ -46,9 +47,7 @@ export class MultiInstancePluginFramework<InnerPlugin extends PluginClass>
delete this.instances;
}
onApply() {
this._registerInstances();
}
onApply() {}
}
export function MultiInstancePlugin<
......
......@@ -30,6 +30,7 @@ export interface PluginMeta<T = any> {
__config: T;
__registrar: Registrar;
__pluginOptions: KoishiPluginRegistrationOptions<T>;
__forkInstances: any[];
}
export interface OnApply {
......@@ -93,7 +94,8 @@ export function DefinePlugin<T>(
__config: T;
__pluginOptions: KoishiPluginRegistrationOptions<T>;
__registrar: Registrar;
__pluginsToWaitFor: Promise<void>[];
__promisesToWaitFor: Promise<void>[];
__forkInstances: any[];
_handleSystemInjections() {
const injectKeys = reflector.getArray(KoishiSystemInjectSymKeys, this);
......@@ -138,13 +140,14 @@ export function DefinePlugin<T>(
view: Record<string, any> = {},
) {
const result = this.__registrar.register(ctx, methodKey, false, view);
if (result?.type === 'ws') {
const type = result?.type;
if (type === 'ws') {
const layer = result.result as WebSocketLayer;
ctx.on('dispose', () => layer.close());
} else if (result?.type === 'plugin') {
} else if (type === 'apply' || type === 'plugin') {
const mayBePromise = result.result;
if (mayBePromise instanceof Promise) {
this.__pluginsToWaitFor.push(mayBePromise);
this.__promisesToWaitFor.push(mayBePromise);
}
}
}
......@@ -255,9 +258,9 @@ export function DefinePlugin<T>(
_registerAfterInit() {
this.__ctx.on('ready', async () => {
if (this.__pluginsToWaitFor.length) {
await Promise.all(this.__pluginsToWaitFor);
this.__pluginsToWaitFor = [];
if (this.__promisesToWaitFor.length) {
await Promise.all(this.__promisesToWaitFor);
this.__promisesToWaitFor = [];
}
if (typeof this.onConnect === 'function') {
await this.onConnect();
......@@ -295,7 +298,7 @@ export function DefinePlugin<T>(
this.__config = config;
this.__pluginOptions = options;
this.__registrar = new Registrar(this, originalClass, config);
this.__pluginsToWaitFor = [];
this.__promisesToWaitFor = [];
this._initializePluginClass();
}
};
......
......@@ -11,6 +11,7 @@ import {
InjectLogger,
Provide,
} from '../src/decorators';
import { BasePlugin } from '../src/base-plugin';
declare module 'koishi' {
// eslint-disable-next-line @typescript-eslint/no-namespace
......@@ -33,10 +34,10 @@ class NonImmediateDependency {}
@Provide('myPlugin', { immediate: true })
@DefinePlugin()
class TestingBase implements OnConnect, OnDisconnect, OnApply {
@InjectContext()
ctx: Context;
class TestingBase
extends BasePlugin<any>
implements OnConnect, OnDisconnect, OnApply
{
@InjectLogger()
logger: Logger;
......@@ -69,13 +70,14 @@ class MyPlugin3 extends TestingBase {
nonImmediateDependency: NonImmediateDependency;
}
async function RunApplyTest(app: App) {
async function RunApplyTest(app: App, plugin: any) {
app.plugin(plugin);
await app.start();
const myPlugin = app.myPlugin;
expect(myPlugin.applied).toBe(true);
expect(myPlugin.connected).toBe(true);
expect(myPlugin.disconnected).toBe(false);
await myPlugin.ctx.dispose();
app.dispose(plugin);
expect(myPlugin.disconnected).toBe(true);
expect(app.immediateDependency).toBeDefined();
expect(app.nonImmediateDependency).toBeDefined();
......@@ -91,20 +93,13 @@ describe('Apply and Connect in koishi-thirdeye', () => {
});
it('should be applied and connected', async () => {
app.plugin(MyPlugin);
const myPlugin = app.myPlugin;
expect(myPlugin.applied).toBe(true);
expect(myPlugin.connected).toBe(false);
expect(myPlugin.disconnected).toBe(false);
await RunApplyTest(app);
await RunApplyTest(app, MyPlugin);
});
it('should be applied and connected with immediate dependency', async () => {
app.plugin(MyPlugin2);
await RunApplyTest(app);
await RunApplyTest(app, MyPlugin2);
});
it('should be applied and connected with non-immediate dependency', async () => {
app.plugin(MyPlugin3);
await RunApplyTest(app);
await RunApplyTest(app, MyPlugin3);
});
it('should name logger correctly', () => {
......
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