Skip to content

Commit

Permalink
fix(core): fix incorrect behavior of session.cancelQueued(), fix #630
Browse files Browse the repository at this point in the history
… (#665)

Co-authored-by: Shigma <1700011071@pku.edu.cn>
  • Loading branch information
DrLee-lihr and shigma authored May 9, 2022
1 parent 34a79e1 commit 722366d
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions packages/core/src/session.ts
Original file line number Diff line number Diff line change
@@ -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, isNullable, Logger, makeArray, observe, Promisify, Random, segment } from '@koishijs/utils'
import { Argv } from './parser'
import { Middleware, Next } from './context'
import { App } from './app'
Expand Down Expand Up @@ -94,10 +94,9 @@ export class Session<U extends User.Field = never, G extends Channel.Field = nev
guild?: Channel.Observed<G>
parsed?: Parsed

private _delay?: number
private _queued: Promise<void>
private _hooks: (() => void)[]
private _promise: Promise<string>
private _queuedTimeout: NodeJS.Timeout
private _queuedMessages: [string, number][]

constructor(bot: Bot, session: Partial<Session.Payload>) {
Object.assign(this, session)
Expand All @@ -107,8 +106,8 @@ export class Session<U extends User.Field = never, G extends Channel.Field = nev
defineProperty(this, 'user', null)
defineProperty(this, 'channel', null)
defineProperty(this, 'id', Random.id())
defineProperty(this, '_queued', Promise.resolve())
defineProperty(this, '_hooks', [])
defineProperty(this, '_queuedMessages', [])
defineProperty(this, '_queued', undefined)
}

get uid() {
Expand Down Expand Up @@ -169,29 +168,29 @@ export class Session<U extends User.Field = never, G extends Channel.Field = nev
}

cancelQueued(delay = this.app.options.delay.cancel) {
this._hooks.forEach(Reflect.apply)
this._delay = delay
clearTimeout(this._queuedTimeout)
this._queuedMessages = []
this._queuedTimeout = setTimeout(() => this._next(), delay)
}

private _next() {
const message = this._queuedMessages.shift()
if (!message) {
this._queuedTimeout = undefined
return
}
this.send(message[0])
this._queuedTimeout = setTimeout(() => this._next(), message[1])
}

async sendQueued(content: string, delay?: number) {
if (!content) return
if (typeof delay === 'undefined') {
if (isNullable(delay)) {
const { message, character } = this.app.options.delay
delay = Math.max(message, character * content.length)
}
return this._queued = this._queued.then(() => new Promise<void>((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 (!this._queuedTimeout) this._next()
}

resolveValue<T>(source: T | ((session: Session) => T)): T {
Expand Down

0 comments on commit 722366d

Please sign in to comment.