add channel renderer + try to simplify adapter generics

This commit is contained in:
MapleLeaf
2021-12-27 19:22:21 -06:00
parent 6bfb1ab6de
commit 3682f67bfe
13 changed files with 143 additions and 43 deletions

View File

@@ -0,0 +1,13 @@
import type { Channel } from "./channel"
import type { Message, MessageOptions } from "./message"
import { Renderer } from "./renderer"
export class ChannelMessageRenderer extends Renderer {
constructor(private channel: Channel) {
super()
}
protected createMessage(options: MessageOptions): Promise<Message> {
return this.channel.send(options)
}
}

View File

@@ -0,0 +1,5 @@
import type { Message, MessageOptions } from "./message"
export type Channel = {
send(message: MessageOptions): Promise<Message>
}

View File

@@ -0,0 +1,22 @@
import type { CommandInteraction } from "./interaction"
import type { Message, MessageOptions } from "./message"
import { Renderer } from "./renderer"
// keep track of interaction ids which have replies,
// so we know whether to call reply() or followUp()
const repliedInteractionIds = new Set<string>()
export class CommandReplyRenderer extends Renderer {
constructor(private interaction: CommandInteraction) {
super()
}
protected createMessage(options: MessageOptions): Promise<Message> {
if (repliedInteractionIds.has(this.interaction.id)) {
return this.interaction.followUp(options)
}
repliedInteractionIds.add(this.interaction.id)
return this.interaction.reply(options)
}
}

View File

@@ -2,7 +2,7 @@ import type { HostConfig } from "react-reconciler"
import ReactReconciler from "react-reconciler"
import { raise } from "../../helpers/raise.js"
import { Node } from "./node.js"
import type { Renderer } from "./renderer.js"
import type { Renderer } from "./renderer"
import { TextNode } from "./text-node.js"
const config: HostConfig<

View File

@@ -1,32 +1,24 @@
import type { Subscription } from "rxjs"
import { Subject } from "rxjs"
import { concatMap } from "rxjs/operators"
import { Container } from "./container.js"
import type { CommandInteraction, ComponentInteraction } from "./interaction"
import type { ComponentInteraction } from "./interaction"
import type { Message, MessageOptions } from "./message"
import type { Node } from "./node.js"
// keep track of interaction ids which have replies,
// so we know whether to call reply() or followUp()
const repliedInteractionIds = new Set<string>()
type UpdatePayload =
| { action: "update" | "deactivate"; options: MessageOptions }
| { action: "destroy" }
export class Renderer {
export abstract class Renderer {
readonly nodes = new Container<Node<unknown>>()
private componentInteraction?: ComponentInteraction
private message?: Message
private updates = new Subject<UpdatePayload>()
private updateSubscription: Subscription
private active = true
private updates = new Subject<UpdatePayload>()
constructor(private interaction: CommandInteraction) {
this.updateSubscription = this.updates
.pipe(concatMap((payload) => this.updateMessage(payload)))
.subscribe({ error: console.error })
}
private updateSubscription = this.updates
.pipe(concatMap((payload) => this.updateMessage(payload)))
.subscribe({ error: console.error })
render() {
if (!this.active) {
@@ -62,6 +54,8 @@ export class Renderer {
}
}
protected abstract createMessage(options: MessageOptions): Promise<Message>
private getMessageOptions(): MessageOptions {
const options: MessageOptions = {
content: "",
@@ -99,12 +93,6 @@ export class Renderer {
return
}
if (repliedInteractionIds.has(this.interaction.id)) {
this.message = await this.interaction.followUp(payload.options)
return
}
repliedInteractionIds.add(this.interaction.id)
this.message = await this.interaction.reply(payload.options)
this.message = await this.createMessage(payload.options)
}
}