use immutable/persistent reconciler
This commit is contained in:
@@ -1,28 +1,40 @@
|
|||||||
import type { TextBasedChannels } from "discord.js"
|
import type { Message, MessageOptions, TextBasedChannels } from "discord.js"
|
||||||
import type { ReacordInstance } from "./instance.js"
|
|
||||||
|
|
||||||
export class ReacordContainer {
|
export class ReacordContainer {
|
||||||
channel: TextBasedChannels
|
channel: TextBasedChannels
|
||||||
instances = new Set<ReacordInstance>()
|
message?: Message
|
||||||
|
|
||||||
constructor(channel: TextBasedChannels) {
|
constructor(channel: TextBasedChannels) {
|
||||||
this.channel = channel
|
this.channel = channel
|
||||||
}
|
}
|
||||||
|
|
||||||
add(instance: ReacordInstance) {
|
render(instances: string[]) {
|
||||||
this.instances.add(instance)
|
if (instances.length === 0) {
|
||||||
instance.render(this.channel)
|
if (this.message) {
|
||||||
|
this.channel.messages.cache.delete(this.message.id)
|
||||||
|
this.message.delete().catch(console.error)
|
||||||
|
this.message = undefined
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
remove(instance: ReacordInstance) {
|
const messageOptions: MessageOptions = {
|
||||||
this.instances.delete(instance)
|
content: instances.join(""),
|
||||||
instance.destroy()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
clear() {
|
if (this.message) {
|
||||||
for (const instance of this.instances) {
|
this.message.edit(messageOptions).catch(console.error)
|
||||||
instance.destroy()
|
} else {
|
||||||
}
|
this.channel.send(messageOptions).then((message) => {
|
||||||
this.instances.clear()
|
this.message = message
|
||||||
|
}, console.error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// clear() {
|
||||||
|
// for (const instance of this.instances) {
|
||||||
|
// instance.destroy()
|
||||||
|
// }
|
||||||
|
// this.instances.clear()
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,15 +2,14 @@
|
|||||||
import ReactReconciler from "react-reconciler"
|
import ReactReconciler from "react-reconciler"
|
||||||
import type { ReacordContainer } from "./container.js"
|
import type { ReacordContainer } from "./container.js"
|
||||||
import { raise } from "./helpers/raise.js"
|
import { raise } from "./helpers/raise.js"
|
||||||
import { ReacordInstance } from "./instance.js"
|
|
||||||
|
|
||||||
export const reconciler = ReactReconciler<
|
export const reconciler = ReactReconciler<
|
||||||
unknown,
|
unknown,
|
||||||
Record<string, unknown>,
|
Record<string, unknown>,
|
||||||
ReacordContainer,
|
ReacordContainer,
|
||||||
ReacordInstance,
|
string,
|
||||||
ReacordInstance,
|
string,
|
||||||
ReacordInstance,
|
string,
|
||||||
unknown,
|
unknown,
|
||||||
unknown,
|
unknown,
|
||||||
unknown,
|
unknown,
|
||||||
@@ -20,13 +19,13 @@ export const reconciler = ReactReconciler<
|
|||||||
unknown
|
unknown
|
||||||
>({
|
>({
|
||||||
now: Date.now,
|
now: Date.now,
|
||||||
supportsMutation: true,
|
|
||||||
isPrimaryRenderer: true,
|
isPrimaryRenderer: true,
|
||||||
noTimeout: -1,
|
supportsMutation: false,
|
||||||
|
supportsPersistence: true,
|
||||||
supportsHydration: false,
|
supportsHydration: false,
|
||||||
supportsPersistence: false,
|
|
||||||
scheduleTimeout: setTimeout,
|
scheduleTimeout: setTimeout,
|
||||||
cancelTimeout: clearTimeout,
|
cancelTimeout: clearTimeout,
|
||||||
|
noTimeout: -1,
|
||||||
|
|
||||||
getRootHostContext: () => ({}),
|
getRootHostContext: () => ({}),
|
||||||
getChildHostContext: () => ({}),
|
getChildHostContext: () => ({}),
|
||||||
@@ -48,25 +47,41 @@ export const reconciler = ReactReconciler<
|
|||||||
hostContext,
|
hostContext,
|
||||||
internalInstanceHandle,
|
internalInstanceHandle,
|
||||||
) => {
|
) => {
|
||||||
return new ReacordInstance(text)
|
return text
|
||||||
},
|
},
|
||||||
|
|
||||||
prepareForCommit: () => null,
|
prepareForCommit: () => null,
|
||||||
resetAfterCommit: () => 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"),
|
appendInitialChild: (parent, child) => raise("Not implemented"),
|
||||||
finalizeInitialChildren: () => raise("Not implemented"),
|
finalizeInitialChildren: () => raise("Not implemented"),
|
||||||
getPublicInstance: () => raise("Not implemented"),
|
getPublicInstance: () => raise("Not implemented"),
|
||||||
prepareUpdate: () => raise("Not implemented"),
|
prepareUpdate: () => raise("Not implemented"),
|
||||||
preparePortalMount: () => 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