Skip to content

Commit

Permalink
fix(core): koishijs#630 simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
DrLee-lihr committed May 5, 2022
1 parent 2e6e955 commit 29cb7c6
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 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, 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,8 +94,8 @@ export class Session<U extends User.Field = never, G extends Channel.Field = nev
guild?: Channel.Observed<G>
parsed?: Parsed

private _hooks: NodeJS.Timeout[]
private _promise: Promise<string>
private _queued: NodeJS.Timeout
private _queuedMessages: [string, number][]

constructor(bot: Bot, session: Partial<Session.Payload>) {
Expand Down Expand Up @@ -168,21 +168,19 @@ export class Session<U extends User.Field = never, G extends Channel.Field = nev
}

cancelQueued() {
this._hooks.forEach(clearTimeout)
clearTimeout(this._queued)
this._queued = undefined
this._queuedMessages = []
}

private _next() {
const message = this._queuedMessages.shift()
if (typeof message === 'undefined') return
remove(this._queuedMessages, message)
this.send(message[0])
this._hooks.push(
setTimeout(() => {
this._hooks.shift()
this._next()
}, message[1]),
)
this._queued = setTimeout(() => {
this._queued = undefined
this._next()
}, message[1])
}

async sendQueued(content: string, delay?: number) {
Expand All @@ -192,7 +190,7 @@ export class Session<U extends User.Field = never, G extends Channel.Field = nev
delay = Math.max(message, character * content.length)
}
this._queuedMessages.push([content, delay])
if (this._hooks.length === 0) this._next()
if (typeof this._queued === 'undefined') this._next()
}

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

0 comments on commit 29cb7c6

Please sign in to comment.