handle async updates via queue
This commit is contained in:
@@ -1,40 +1,72 @@
|
|||||||
import type { Message, MessageOptions, TextBasedChannels } from "discord.js"
|
import type { Message, MessageOptions, TextBasedChannels } from "discord.js"
|
||||||
|
|
||||||
|
type Action =
|
||||||
|
| { type: "updateMessage"; options: MessageOptions }
|
||||||
|
| { type: "deleteMessage" }
|
||||||
|
|
||||||
export class ReacordContainer {
|
export class ReacordContainer {
|
||||||
channel: TextBasedChannels
|
channel: TextBasedChannels
|
||||||
message?: Message
|
message?: Message
|
||||||
|
actions: Action[] = []
|
||||||
|
runningActions = false
|
||||||
|
|
||||||
constructor(channel: TextBasedChannels) {
|
constructor(channel: TextBasedChannels) {
|
||||||
this.channel = channel
|
this.channel = channel
|
||||||
}
|
}
|
||||||
|
|
||||||
render(instances: string[]) {
|
render(instances: string[]) {
|
||||||
if (instances.length === 0) {
|
|
||||||
if (this.message) {
|
|
||||||
this.channel.messages.cache.delete(this.message.id)
|
|
||||||
this.message.delete().catch(console.error)
|
|
||||||
this.message = undefined
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const messageOptions: MessageOptions = {
|
const messageOptions: MessageOptions = {
|
||||||
content: instances.join(""),
|
content: instances.join("") || undefined, // empty strings are not allowed
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.message) {
|
const hasContent = messageOptions.content !== undefined
|
||||||
this.message.edit(messageOptions).catch(console.error)
|
if (hasContent) {
|
||||||
|
this.addAction({ type: "updateMessage", options: messageOptions })
|
||||||
} else {
|
} else {
|
||||||
this.channel.send(messageOptions).then((message) => {
|
this.addAction({ type: "deleteMessage" })
|
||||||
this.message = message
|
|
||||||
}, console.error)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// clear() {
|
private addAction(action: Action) {
|
||||||
// for (const instance of this.instances) {
|
const lastAction = this.actions[this.actions.length - 1]
|
||||||
// instance.destroy()
|
if (lastAction?.type === action.type) {
|
||||||
// }
|
this.actions[this.actions.length - 1] = action
|
||||||
// this.instances.clear()
|
} else {
|
||||||
// }
|
this.actions.push(action)
|
||||||
|
}
|
||||||
|
void this.runActions()
|
||||||
|
}
|
||||||
|
|
||||||
|
private runActions() {
|
||||||
|
if (this.runningActions) return
|
||||||
|
this.runningActions = true
|
||||||
|
|
||||||
|
queueMicrotask(async () => {
|
||||||
|
let action: Action | undefined
|
||||||
|
while ((action = this.actions.shift())) {
|
||||||
|
try {
|
||||||
|
switch (action.type) {
|
||||||
|
case "updateMessage":
|
||||||
|
if (this.message) {
|
||||||
|
await this.message.edit(action.options)
|
||||||
|
} else {
|
||||||
|
this.message = await this.channel.send(action.options)
|
||||||
|
}
|
||||||
|
break
|
||||||
|
case "deleteMessage":
|
||||||
|
if (this.message) {
|
||||||
|
await this.message.delete()
|
||||||
|
this.message = undefined
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Failed to run action:`, action)
|
||||||
|
console.error(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.runningActions = false
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import test from "ava"
|
import test from "ava"
|
||||||
import { Client, TextChannel } from "discord.js"
|
import { Client, TextChannel } from "discord.js"
|
||||||
import { nanoid } from "nanoid"
|
import { nanoid } from "nanoid"
|
||||||
|
import { setTimeout } from "node:timers/promises"
|
||||||
import { raise } from "./helpers/raise.js"
|
import { raise } from "./helpers/raise.js"
|
||||||
import { waitForWithTimeout } from "./helpers/wait-for-with-timeout.js"
|
import { waitForWithTimeout } from "./helpers/wait-for-with-timeout.js"
|
||||||
import { render } from "./render.js"
|
import { render } from "./render.js"
|
||||||
@@ -31,31 +32,77 @@ test.after(() => {
|
|||||||
client.destroy()
|
client.destroy()
|
||||||
})
|
})
|
||||||
|
|
||||||
test("rendering text", async (t) => {
|
test.serial("rendering text", async (t) => {
|
||||||
const content = nanoid()
|
const content = nanoid()
|
||||||
const root = render(content, channel)
|
const root = render(content, channel)
|
||||||
|
|
||||||
await waitForWithTimeout(async () => {
|
await waitForWithTimeout(
|
||||||
const messages = await channel.messages.fetch()
|
async () => {
|
||||||
return messages.some((m) => m.content === content)
|
const messages = await channel.messages.fetch()
|
||||||
}, 4000)
|
return messages.some((m) => m.content === content)
|
||||||
|
},
|
||||||
|
10_000,
|
||||||
|
"Message not found",
|
||||||
|
)
|
||||||
|
|
||||||
const newContent = nanoid()
|
const newContent = nanoid()
|
||||||
root.rerender(newContent)
|
root.rerender(newContent)
|
||||||
|
|
||||||
await waitForWithTimeout(async () => {
|
await waitForWithTimeout(
|
||||||
const messages = await channel.messages.fetch({ limit: 1 })
|
async () => {
|
||||||
return messages.some((m) => m.content === content)
|
const messages = await channel.messages.fetch()
|
||||||
}, 4000)
|
return messages.some((m) => m.content === newContent)
|
||||||
|
},
|
||||||
|
10_000,
|
||||||
|
"Message not found",
|
||||||
|
)
|
||||||
|
|
||||||
root.destroy()
|
root.destroy()
|
||||||
|
|
||||||
await waitForWithTimeout(async () => {
|
await waitForWithTimeout(
|
||||||
const messages = await channel.messages.fetch({ limit: 1 })
|
async () => {
|
||||||
return messages
|
await setTimeout(1000)
|
||||||
.filter((m) => !m.deleted)
|
const messages = await channel.messages.fetch()
|
||||||
.every((m) => m.content !== content)
|
return messages
|
||||||
}, 4000)
|
.filter((m) => !m.deleted)
|
||||||
|
.every((m) => m.content !== content)
|
||||||
|
},
|
||||||
|
10_000,
|
||||||
|
"Message was not deleted",
|
||||||
|
)
|
||||||
|
|
||||||
|
t.pass()
|
||||||
|
})
|
||||||
|
|
||||||
|
test.serial("rapid updates", async (t) => {
|
||||||
|
const content = nanoid()
|
||||||
|
const newContent = nanoid()
|
||||||
|
|
||||||
|
const root = render(content, channel)
|
||||||
|
root.rerender(newContent)
|
||||||
|
|
||||||
|
await waitForWithTimeout(
|
||||||
|
async () => {
|
||||||
|
const messages = await channel.messages.fetch()
|
||||||
|
return messages.some((m) => m.content === newContent)
|
||||||
|
},
|
||||||
|
10_000,
|
||||||
|
"Message not found",
|
||||||
|
)
|
||||||
|
|
||||||
|
root.rerender(content)
|
||||||
|
root.destroy()
|
||||||
|
|
||||||
|
await waitForWithTimeout(
|
||||||
|
async () => {
|
||||||
|
const messages = await channel.messages.fetch()
|
||||||
|
return messages
|
||||||
|
.filter((m) => !m.deleted)
|
||||||
|
.every((m) => m.content !== content)
|
||||||
|
},
|
||||||
|
10_000,
|
||||||
|
"Message was not deleted",
|
||||||
|
)
|
||||||
|
|
||||||
t.pass()
|
t.pass()
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user