Commit 6c3b1727 authored by nanahira's avatar nanahira

ReplySession: add process timeout

parent 3e8a9082
This diff is collapsed.
...@@ -72,6 +72,6 @@ ...@@ -72,6 +72,6 @@
"testEnvironment": "node" "testEnvironment": "node"
}, },
"peerDependencies": { "peerDependencies": {
"koishi": "^4.11.5" "koishi": "^4.11.6"
} }
} }
...@@ -38,7 +38,7 @@ export class ReplySession< ...@@ -38,7 +38,7 @@ export class ReplySession<
this.midResolver = resolve; this.midResolver = resolve;
}); });
async process() { async process(timeout = 0) {
if (!this.app.lifecycle.isActive) return; if (!this.app.lifecycle.isActive) return;
const events: string[] = [this.type]; const events: string[] = [this.type];
if (this.subtype) { if (this.subtype) {
...@@ -47,17 +47,24 @@ export class ReplySession< ...@@ -47,17 +47,24 @@ export class ReplySession<
events.unshift(events[0] + '/' + this.subsubtype); events.unshift(events[0] + '/' + this.subsubtype);
} }
} }
this.inRequest = true;
this.emitPromise = Promise.all( this.emitPromise = Promise.all(
events.map((event) => this.app.root.parallel(this, event as any, this)), events.map((event) => this.app.root.parallel(this, event as any, this)),
); );
return this.waitForPattern(); return this.waitForPattern(timeout);
} }
async waitForPattern() { inRequest = false;
await Promise.race([
async waitForPattern(timeout = 0) {
const promises = [
this.emitPromise.then(() => this.midResolve(true)), this.emitPromise.then(() => this.midResolve(true)),
this.midPromise, this.midPromise,
]); ];
if (timeout) {
promises.push(new Promise((resolve) => setTimeout(resolve, timeout)));
}
await Promise.race(promises);
return this.gatherReplyMessages(); return this.gatherReplyMessages();
} }
...@@ -80,6 +87,9 @@ export class ReplySession< ...@@ -80,6 +87,9 @@ export class ReplySession<
} }
async send(content: Fragment, options: SendOptions = {}) { async send(content: Fragment, options: SendOptions = {}) {
if (!this.inRequest) {
return super.send(content, options);
}
if (!content) return; if (!content) return;
options.session = this; options.session = this;
const children = await this.transform(segment.normalize(content)); const children = await this.transform(segment.normalize(content));
...@@ -96,13 +106,17 @@ export class ReplySession< ...@@ -96,13 +106,17 @@ export class ReplySession<
return [messageId]; return [messageId];
} }
gatherReplyMessages() { private gatherReplyMessages() {
const result = this.replyMessages.filter((m) => !!m); const result = this.replyMessages.filter((m) => !!m);
this.replyMessages = []; this.replyMessages = [];
this.inRequest = false;
return result; return result;
} }
prompt(...args: any[]) { prompt(...args: any[]) {
if (!this.inRequest) {
return super.prompt(...args);
}
if (!this.app.__prompt_resolver__) { if (!this.app.__prompt_resolver__) {
this.app.root.plugin(PromptResolver); this.app.root.plugin(PromptResolver);
} }
...@@ -163,6 +177,7 @@ class PromptResolver extends StarterPlugin() { ...@@ -163,6 +177,7 @@ class PromptResolver extends StarterPlugin() {
async resolvePrompt(identifier: string, session: ReplySession) { async resolvePrompt(identifier: string, session: ReplySession) {
const prompt = this.prompts.get(identifier); const prompt = this.prompts.get(identifier);
if (prompt) { if (prompt) {
prompt.session.inRequest = true;
prompt.resolver(session ? await prompt.callback(session) : undefined); prompt.resolver(session ? await prompt.callback(session) : undefined);
clearTimeout(prompt.timeout); clearTimeout(prompt.timeout);
this.prompts.delete(identifier); this.prompts.delete(identifier);
......
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