From f62b80287db2d92e3ca16d3143f10ac23a0a8de8 Mon Sep 17 00:00:00 2001 From: MapleLeaf <19603573+itsMapleLeaf@users.noreply.github.com> Date: Tue, 28 Dec 2021 13:10:37 -0600 Subject: [PATCH] simplify adapter extension --- library/core/reacord-discord-js.ts | 14 ++++++---- library/core/reacord-tester.ts | 45 +++++++++++++++++++----------- library/core/reacord.ts | 30 ++------------------ 3 files changed, 38 insertions(+), 51 deletions(-) diff --git a/library/core/reacord-discord-js.ts b/library/core/reacord-discord-js.ts index 31b32ae..fc41bb6 100644 --- a/library/core/reacord-discord-js.ts +++ b/library/core/reacord-discord-js.ts @@ -1,6 +1,8 @@ import type * as Discord from "discord.js" import { raise } from "../../helpers/raise" import { toUpper } from "../../helpers/to-upper" +import { ChannelMessageRenderer } from "../internal/channel-message-renderer" +import { CommandReplyRenderer } from "../internal/command-reply-renderer" import type { ComponentInteraction } from "../internal/interaction" import type { Message, MessageOptions } from "../internal/message" import type { ReacordConfig, ReacordInstance } from "./reacord" @@ -23,8 +25,8 @@ export class ReacordDiscordJs extends Reacord { channelId: string, initialContent?: React.ReactNode, ): ReacordInstance { - return this.createChannelRendererInstance( - { + return this.createInstance( + new ChannelMessageRenderer({ send: async (options) => { const channel = this.client.channels.cache.get(channelId) ?? @@ -38,7 +40,7 @@ export class ReacordDiscordJs extends Reacord { const message = await channel.send(getDiscordMessageOptions(options)) return createReacordMessage(message) }, - }, + }), initialContent, ) } @@ -47,8 +49,8 @@ export class ReacordDiscordJs extends Reacord { interaction: Discord.CommandInteraction, initialContent?: React.ReactNode, ): ReacordInstance { - return this.createCommandReplyRendererInstance( - { + return this.createInstance( + new CommandReplyRenderer({ type: "command", id: interaction.id, channelId: interaction.channelId, @@ -66,7 +68,7 @@ export class ReacordDiscordJs extends Reacord { }) return createReacordMessage(message as Discord.Message) }, - }, + }), initialContent, ) } diff --git a/library/core/reacord-tester.ts b/library/core/reacord-tester.ts index 2095b40..9c0e20b 100644 --- a/library/core/reacord-tester.ts +++ b/library/core/reacord-tester.ts @@ -8,6 +8,8 @@ import { logPretty } from "../../helpers/log-pretty" import { omit } from "../../helpers/omit" import { raise } from "../../helpers/raise" import type { Channel } from "../internal/channel" +import { ChannelMessageRenderer } from "../internal/channel-message-renderer" +import { CommandReplyRenderer } from "../internal/command-reply-renderer" import { Container } from "../internal/container" import type { ButtonInteraction, @@ -36,15 +38,17 @@ export class ReacordTester extends Reacord { return [...this.messageContainer] } - send(): ReacordInstance { - return this.createChannelRendererInstance( - new TestChannel(this.messageContainer), + override send(): ReacordInstance { + return this.createInstance( + new ChannelMessageRenderer(new TestChannel(this.messageContainer)), ) } - reply(): ReacordInstance { - return this.createCommandReplyRendererInstance( - new TestCommandInteraction(this.messageContainer), + override reply(): ReacordInstance { + return this.createInstance( + new CommandReplyRenderer( + new TestCommandInteraction(this.messageContainer), + ), ) } @@ -177,8 +181,7 @@ class TestCommandInteraction implements CommandInteraction { } } -class TestButtonInteraction implements ButtonInteraction { - readonly type = "button" +class TestInteraction { readonly id = nanoid() readonly channelId = "test-channel-id" @@ -187,21 +190,29 @@ class TestButtonInteraction implements ButtonInteraction { async update(options: MessageOptions): Promise { this.message.options = options } + + async deferUpdate(): Promise {} } -class TestSelectInteraction implements SelectInteraction { +class TestButtonInteraction + extends TestInteraction + implements ButtonInteraction +{ + readonly type = "button" +} + +class TestSelectInteraction + extends TestInteraction + implements SelectInteraction +{ readonly type = "select" - readonly id = nanoid() - readonly channelId = "test-channel-id" constructor( - readonly customId: string, - readonly message: TestMessage, + customId: string, + message: TestMessage, readonly values: string[], - ) {} - - async update(options: MessageOptions): Promise { - this.message.options = options + ) { + super(customId, message) } } diff --git a/library/core/reacord.ts b/library/core/reacord.ts index bdf0a02..92e25a0 100644 --- a/library/core/reacord.ts +++ b/library/core/reacord.ts @@ -1,11 +1,5 @@ import type { ReactNode } from "react" -import type { Channel } from "../internal/channel" -import { ChannelMessageRenderer } from "../internal/channel-message-renderer" -import { CommandReplyRenderer } from "../internal/command-reply-renderer.js" -import type { - CommandInteraction, - ComponentInteraction, -} from "../internal/interaction" +import type { ComponentInteraction } from "../internal/interaction" import { reconciler } from "../internal/reconciler.js" import type { Renderer } from "../internal/renderer" @@ -49,27 +43,7 @@ export abstract class Reacord { return this.config.maxInstances ?? 50 } - protected createChannelRendererInstance( - channel: Channel, - initialContent?: ReactNode, - ) { - return this.createInstance( - new ChannelMessageRenderer(channel), - initialContent, - ) - } - - protected createCommandReplyRendererInstance( - commandInteraction: CommandInteraction, - initialContent?: ReactNode, - ): ReacordInstance { - return this.createInstance( - new CommandReplyRenderer(commandInteraction), - initialContent, - ) - } - - private createInstance(renderer: Renderer, initialContent?: ReactNode) { + protected createInstance(renderer: Renderer, initialContent?: ReactNode) { if (this.renderers.length > this.maxInstances) { this.deactivate(this.renderers[0]!) }