Commit d3214ef9 authored by nanahira's avatar nanahira

add @Apply and resolve promise

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