From ade2464aaf9af82881cdd59933e2dff4c34cd24b Mon Sep 17 00:00:00 2001 From: MapleLeaf <19603573+itsMapleLeaf@users.noreply.github.com> Date: Thu, 9 Dec 2021 02:54:06 -0600 Subject: [PATCH] use immutable/persistent reconciler --- src/container.ts | 46 ++++++++++++++++++++++++++---------------- src/reconciler.ts | 51 ++++++++++++++++++++++++++++++----------------- 2 files changed, 62 insertions(+), 35 deletions(-) diff --git a/src/container.ts b/src/container.ts index d305f07..11f55f3 100644 --- a/src/container.ts +++ b/src/container.ts @@ -1,28 +1,40 @@ -import type { TextBasedChannels } from "discord.js" -import type { ReacordInstance } from "./instance.js" +import type { Message, MessageOptions, TextBasedChannels } from "discord.js" export class ReacordContainer { channel: TextBasedChannels - instances = new Set() + message?: Message constructor(channel: TextBasedChannels) { this.channel = channel } - add(instance: ReacordInstance) { - this.instances.add(instance) - instance.render(this.channel) - } - - remove(instance: ReacordInstance) { - this.instances.delete(instance) - instance.destroy() - } - - clear() { - for (const instance of this.instances) { - instance.destroy() + 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 = { + content: instances.join(""), + } + + if (this.message) { + this.message.edit(messageOptions).catch(console.error) + } else { + this.channel.send(messageOptions).then((message) => { + this.message = message + }, console.error) } - this.instances.clear() } + + // clear() { + // for (const instance of this.instances) { + // instance.destroy() + // } + // this.instances.clear() + // } } diff --git a/src/reconciler.ts b/src/reconciler.ts index 4ea0bc9..a2f48c1 100644 --- a/src/reconciler.ts +++ b/src/reconciler.ts @@ -2,15 +2,14 @@ import ReactReconciler from "react-reconciler" import type { ReacordContainer } from "./container.js" import { raise } from "./helpers/raise.js" -import { ReacordInstance } from "./instance.js" export const reconciler = ReactReconciler< unknown, Record, ReacordContainer, - ReacordInstance, - ReacordInstance, - ReacordInstance, + string, + string, + string, unknown, unknown, unknown, @@ -20,13 +19,13 @@ export const reconciler = ReactReconciler< unknown >({ now: Date.now, - supportsMutation: true, isPrimaryRenderer: true, - noTimeout: -1, + supportsMutation: false, + supportsPersistence: true, supportsHydration: false, - supportsPersistence: false, scheduleTimeout: setTimeout, cancelTimeout: clearTimeout, + noTimeout: -1, getRootHostContext: () => ({}), getChildHostContext: () => ({}), @@ -48,25 +47,41 @@ export const reconciler = ReactReconciler< hostContext, internalInstanceHandle, ) => { - return new ReacordInstance(text) + return text }, prepareForCommit: () => null, resetAfterCommit: () => null, - clearContainer: (container) => { - container.clear() - }, - appendChildToContainer: (container, child) => { - container.add(child) - }, - removeChildFromContainer: (container, child) => { - container.remove(child) - }, - appendInitialChild: (parent, child) => raise("Not implemented"), finalizeInitialChildren: () => raise("Not implemented"), getPublicInstance: () => raise("Not implemented"), prepareUpdate: () => raise("Not implemented"), preparePortalMount: () => raise("Not implemented"), + + createContainerChildSet: (container: ReacordContainer): string[] => { + console.log("createContainerChildSet", [container]) + return [] + }, + + appendChildToContainerChildSet: (children: string[], child: string) => { + console.log("appendChildToContainerChildSet", [children, child]) + children.push(child) + }, + + finalizeContainerChildren: ( + container: ReacordContainer, + children: string[], + ) => { + console.log("finalizeContainerChildren", [container, children]) + return false + }, + + replaceContainerChildren: ( + container: ReacordContainer, + children: string[], + ) => { + console.log("replaceContainerChildren", [container, children]) + container.render(children) + }, })