diff --git a/packages/core/src/session.ts b/packages/core/src/session.ts index 0509f43da0..1a98260618 100644 --- a/packages/core/src/session.ts +++ b/packages/core/src/session.ts @@ -1,6 +1,6 @@ import { Channel, Tables, User } from './database' import { Command } from './command' -import { defineProperty, Logger, makeArray, observe, Promisify, Random, remove, segment } from '@koishijs/utils' +import { defineProperty, Logger, makeArray, observe, Promisify, Random, segment } from '@koishijs/utils' import { Argv } from './parser' import { Middleware, Next } from './context' import { App } from './app' @@ -94,10 +94,9 @@ export class Session parsed?: Parsed - private _delay?: number - private _queued: Promise - private _hooks: (() => void)[] private _promise: Promise + private _queued: NodeJS.Timeout + private _queuedMessages: [string, number][] constructor(bot: Bot, session: Partial) { Object.assign(this, session) @@ -107,8 +106,8 @@ export class Session this._next(), delay) + } + + private _next() { + const message = this._queuedMessages.shift() + if (typeof message === 'undefined') { + this._queued = undefined + return + } + this.send(message[0]) + this._queued = setTimeout(() => this._next(), message[1]) } async sendQueued(content: string, delay?: number) { @@ -179,19 +189,8 @@ export class Session new Promise((resolve) => { - const hook = () => { - resolve() - clearTimeout(timer) - remove(this._hooks, hook) - } - this._hooks.push(hook) - const timer = setTimeout(async () => { - await this.send(content) - this._delay = delay - hook() - }, this._delay || 0) - })) + this._queuedMessages.push([content, delay]) + if (typeof this._queued === 'undefined') this._next() } resolveValue(source: T | ((session: Session) => T)): T {