Commit a2127ef4 authored by nanahira's avatar nanahira

wait for inner plugin load before onConnect

parent 66a035be
......@@ -11,14 +11,14 @@
"dependencies": {
"@types/koa": "^2.13.4",
"@types/koa__router": "^8.0.11",
"koishi-decorators": "^1.3.3",
"koishi-decorators": "^1.3.4",
"lodash": "^4.17.21",
"reflect-metadata": "^0.1.13",
"schemastery-gen": "^3.1.2",
"typed-reflector": "^1.0.10"
},
"devDependencies": {
"@koishijs/plugin-adapter-onebot": "^4.0.0-rc.0",
"@koishijs/plugin-adapter-onebot": "^4.1.5",
"@types/jest": "^27.0.3",
"@types/lodash": "^4.14.177",
"@types/node": "^16.11.9",
......@@ -1053,16 +1053,16 @@
}
},
"node_modules/@koishijs/plugin-adapter-onebot": {
"version": "4.0.0-rc.0",
"resolved": "https://registry.npmjs.org/@koishijs/plugin-adapter-onebot/-/plugin-adapter-onebot-4.0.0-rc.0.tgz",
"integrity": "sha512-IU7EQGXt35V73qGRHPn1WBGmdgaMkVky/OestbLAtZq07hQmBCbnZ8HTSakOMx34day+TS5Qn04QSUm/kfVIGA==",
"version": "4.1.5",
"resolved": "https://registry.npmjs.org/@koishijs/plugin-adapter-onebot/-/plugin-adapter-onebot-4.1.5.tgz",
"integrity": "sha512-6Lb2ifqbY/hTxnbaSLopDHyVbPUBBfnxdDS7FWDapJ7Z0UeXHCNeIyLSSreuEG93UCn3GDzl43ZCVvOhGQUCHg==",
"dev": true,
"dependencies": {
"qface": "^1.2.0",
"ws": "^8.2.1"
"ws": "^8.5.0"
},
"peerDependencies": {
"koishi": "^4.0.0-rc.0"
"koishi": "^4.4.2"
}
},
"node_modules/@koishijs/segment": {
......@@ -4775,9 +4775,9 @@
}
},
"node_modules/koishi-decorators": {
"version": "1.3.3",
"resolved": "https://registry.npmjs.org/koishi-decorators/-/koishi-decorators-1.3.3.tgz",
"integrity": "sha512-Q1iOHqG8p+qhRw2uzyZau8hb5+UGd8QaCdjpPaKc883nXhSAKksmsn0Nt/IrQDqEL4TKbsXKcTO1p59ukRO9Pg==",
"version": "1.3.4",
"resolved": "https://registry.npmjs.org/koishi-decorators/-/koishi-decorators-1.3.4.tgz",
"integrity": "sha512-AokyzfDz3x4eZzd8AZrX+mVAGR7BBQdoyq1xAdol+48K2Ca7Xz8USu4Whvksi/tW8VrSN+Zr3L6YbCo3wS3fmg==",
"dependencies": {
"@types/koa": "^2.13.4",
"@types/koa__router": "^8.0.11",
......@@ -7593,13 +7593,13 @@
}
},
"@koishijs/plugin-adapter-onebot": {
"version": "4.0.0-rc.0",
"resolved": "https://registry.npmjs.org/@koishijs/plugin-adapter-onebot/-/plugin-adapter-onebot-4.0.0-rc.0.tgz",
"integrity": "sha512-IU7EQGXt35V73qGRHPn1WBGmdgaMkVky/OestbLAtZq07hQmBCbnZ8HTSakOMx34day+TS5Qn04QSUm/kfVIGA==",
"version": "4.1.5",
"resolved": "https://registry.npmjs.org/@koishijs/plugin-adapter-onebot/-/plugin-adapter-onebot-4.1.5.tgz",
"integrity": "sha512-6Lb2ifqbY/hTxnbaSLopDHyVbPUBBfnxdDS7FWDapJ7Z0UeXHCNeIyLSSreuEG93UCn3GDzl43ZCVvOhGQUCHg==",
"dev": true,
"requires": {
"qface": "^1.2.0",
"ws": "^8.2.1"
"ws": "^8.5.0"
}
},
"@koishijs/segment": {
......@@ -10461,9 +10461,9 @@
}
},
"koishi-decorators": {
"version": "1.3.3",
"resolved": "https://registry.npmjs.org/koishi-decorators/-/koishi-decorators-1.3.3.tgz",
"integrity": "sha512-Q1iOHqG8p+qhRw2uzyZau8hb5+UGd8QaCdjpPaKc883nXhSAKksmsn0Nt/IrQDqEL4TKbsXKcTO1p59ukRO9Pg==",
"version": "1.3.4",
"resolved": "https://registry.npmjs.org/koishi-decorators/-/koishi-decorators-1.3.4.tgz",
"integrity": "sha512-AokyzfDz3x4eZzd8AZrX+mVAGR7BBQdoyq1xAdol+48K2Ca7Xz8USu4Whvksi/tW8VrSN+Zr3L6YbCo3wS3fmg==",
"requires": {
"@types/koa": "^2.13.4",
"@types/koa__router": "^8.0.11",
......
......@@ -29,7 +29,7 @@
"author": "Nanahira <nanahira@momobako.com>",
"license": "MIT",
"devDependencies": {
"@koishijs/plugin-adapter-onebot": "^4.0.0-rc.0",
"@koishijs/plugin-adapter-onebot": "^4.1.5",
"@types/jest": "^27.0.3",
"@types/lodash": "^4.14.177",
"@types/node": "^16.11.9",
......@@ -50,7 +50,7 @@
"dependencies": {
"@types/koa": "^2.13.4",
"@types/koa__router": "^8.0.11",
"koishi-decorators": "^1.3.3",
"koishi-decorators": "^1.3.4",
"lodash": "^4.17.21",
"reflect-metadata": "^0.1.13",
"schemastery-gen": "^3.1.2",
......
......@@ -69,6 +69,7 @@ export function DefinePlugin<T = any>(
__config: T;
__pluginOptions: KoishiPluginRegistrationOptions<T>;
__registrar: Registrar;
__pluginsToWaitFor: Promise<void>[];
_handleSystemInjections() {
const injectKeys = reflector.getArray(KoishiSystemInjectSymKeys, this);
......@@ -104,6 +105,11 @@ export function DefinePlugin<T = any>(
if (result?.type === 'ws') {
const layer = result.result as WebSocketLayer;
ctx.on('dispose', () => layer.close());
} else if (result?.type === 'plugin') {
const mayBePromise = result.result;
if (mayBePromise instanceof Promise) {
this.__pluginsToWaitFor.push(mayBePromise);
}
}
}
......@@ -174,6 +180,10 @@ export function DefinePlugin<T = any>(
_registerAfterInit() {
this.__ctx.on('ready', async () => {
if (this.__pluginsToWaitFor.length) {
await Promise.all(this.__pluginsToWaitFor);
this.__pluginsToWaitFor = [];
}
if (typeof this.onConnect === 'function') {
await this.onConnect();
}
......@@ -210,6 +220,7 @@ export function DefinePlugin<T = any>(
this.__config = config;
this.__pluginOptions = options;
this.__registrar = new Registrar(this, originalClass);
this.__pluginsToWaitFor = [];
this._initializePluginClass();
}
};
......
......@@ -15,17 +15,36 @@ class MyPlugin extends BasePlugin<any> {
}
}
@DefinePlugin()
class MyLazyPlugin extends BasePlugin<any> {
@UsePlugin()
async loadOnebot() {
await new Promise((resolve) => setTimeout(resolve, 1000));
this.ctx.app['_flag'] = 2;
return PluginDef((ctx) => (ctx.app['_flag1'] = 1));
}
}
describe('Inner plugin', () => {
let app: App;
beforeEach(async () => {
app = new App({ port: 11111 });
await app.start();
// app.plugin(PluginOnebot, { bots: [] });
});
it('should load inner plugin properly', () => {
it('should load inner plugin properly', async () => {
app = new App();
await app.start();
app.plugin(MyPlugin);
expect(app.bots.length).toBe(1);
//expect(app.cache).toBeDefined();
});
it('should wait for plugin to load', async () => {
app = new App();
app['_flag'] = 1;
app['_flag1'] = 0;
app.plugin(MyLazyPlugin);
expect(app['_flag']).toBe(1);
expect(app['_flag1']).toBe(0);
await app.start();
expect(app['_flag']).toBe(2);
expect(app['_flag1']).toBe(1);
});
});
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