use immutable/persistent reconciler
This commit is contained in:
@@ -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<ReacordInstance>()
|
||||
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()
|
||||
// }
|
||||
}
|
||||
|
||||
@@ -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<string, unknown>,
|
||||
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)
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user