clean up container action runner
This commit is contained in:
@@ -1,5 +1,4 @@
|
|||||||
import type { Message, MessageOptions, TextBasedChannels } from "discord.js"
|
import type { Message, MessageOptions, TextBasedChannels } from "discord.js"
|
||||||
import { createDeferred } from "./helpers/deferred.js"
|
|
||||||
|
|
||||||
type Action =
|
type Action =
|
||||||
| { type: "updateMessage"; options: MessageOptions }
|
| { type: "updateMessage"; options: MessageOptions }
|
||||||
@@ -44,35 +43,36 @@ export class ReacordContainer {
|
|||||||
return this.runningPromise
|
return this.runningPromise
|
||||||
}
|
}
|
||||||
|
|
||||||
const promise = (this.runningPromise = createDeferred())
|
const runAction = async (action: Action) => {
|
||||||
|
if (action.type === "updateMessage") {
|
||||||
|
this.message = await (this.message
|
||||||
|
? this.message.edit(action.options)
|
||||||
|
: this.channel.send(action.options))
|
||||||
|
}
|
||||||
|
|
||||||
|
if (action.type === "deleteMessage") {
|
||||||
|
await this.message?.delete()
|
||||||
|
this.message = undefined
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.runningPromise = new Promise((resolve) => {
|
||||||
|
// using a microtask to allow multiple actions to be added synchronously
|
||||||
queueMicrotask(async () => {
|
queueMicrotask(async () => {
|
||||||
let action: Action | undefined
|
let action: Action | undefined
|
||||||
while ((action = this.actions.shift())) {
|
while ((action = this.actions.shift())) {
|
||||||
try {
|
try {
|
||||||
switch (action.type) {
|
await runAction(action)
|
||||||
case "updateMessage":
|
|
||||||
this.message = await (this.message
|
|
||||||
? this.message.edit(action.options)
|
|
||||||
: this.channel.send(action.options))
|
|
||||||
break
|
|
||||||
case "deleteMessage":
|
|
||||||
if (this.message) {
|
|
||||||
await this.message.delete()
|
|
||||||
this.message = undefined
|
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Failed to run action:`, action)
|
console.error(`Failed to run action:`, action)
|
||||||
console.error(error)
|
console.error(error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
resolve()
|
||||||
promise.resolve()
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
await promise
|
await this.runningPromise
|
||||||
this.runningPromise = undefined
|
this.runningPromise = undefined
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user