beginnings of new api

This commit is contained in:
MapleLeaf
2021-12-25 00:52:21 -06:00
parent fa95b42be6
commit e799e71f1a
9 changed files with 177 additions and 45 deletions

View File

@@ -0,0 +1,14 @@
import type { ReactNode } from "react"
import React from "react"
export type TextProps = {
children?: ReactNode
}
export const TextTag = "reacord-text"
export function Text(props: TextProps) {
return React.createElement(TextTag, props)
}
export class TextElementNode {}

1
src.new/context.ts Normal file
View File

@@ -0,0 +1 @@
export type Context = {}

33
src.new/main.ts Normal file
View File

@@ -0,0 +1,33 @@
import type { CommandInteraction } from "discord.js"
import type { ReactNode } from "react"
import type { OpaqueRoot } from "react-reconciler"
import { reconciler } from "./reconciler.js"
import { RootNode } from "./root-node.js"
export class InstanceManager {
private instances = new Set<Instance>()
create(interaction: CommandInteraction) {
const instance = new Instance(interaction)
this.instances.add(instance)
return instance
}
destroy(instance: Instance) {
this.instances.delete(instance)
}
}
class Instance {
private rootNode: RootNode
private container: OpaqueRoot
constructor(interaction: CommandInteraction) {
this.rootNode = new RootNode(interaction)
this.container = reconciler.createContainer(this.rootNode, 0, false, {})
}
render(content: ReactNode) {
reconciler.updateContainer(content, this.container)
}
}

69
src.new/reconciler.ts Normal file
View File

@@ -0,0 +1,69 @@
import type { HostConfig } from "react-reconciler"
import ReactReconciler from "react-reconciler"
import { raise } from "../src/helpers/raise.js"
import type { RootNode } from "./root-node.js"
import { TextNode } from "./text-node.js"
const config: HostConfig<
string, // Type,
Record<string, unknown>, // Props,
RootNode, // Container,
never, // Instance,
TextNode, // TextInstance,
never, // SuspenseInstance,
never, // HydratableInstance,
never, // PublicInstance,
{}, // HostContext,
never, // UpdatePayload,
never, // ChildSet,
number, // TimeoutHandle,
number // NoTimeout,
> = {
// config
now: Date.now,
supportsMutation: true,
supportsPersistence: false,
supportsHydration: false,
isPrimaryRenderer: true,
scheduleTimeout: global.setTimeout,
cancelTimeout: global.clearTimeout,
noTimeout: -1,
getRootHostContext: () => ({}),
getChildHostContext: () => ({}),
createInstance: () => raise("not implemented"),
createTextInstance: (text) => new TextNode(text),
shouldSetTextContent: () => false,
clearContainer: (root) => {
root.clear()
},
appendChildToContainer: (root, child) => {
root.add(child)
},
removeChildFromContainer: (root, child) => {
root.remove(child)
},
// eslint-disable-next-line unicorn/no-null
prepareUpdate: () => null,
commitUpdate: () => {},
commitTextUpdate: (node, oldText, newText) => {
node.text = newText
},
// eslint-disable-next-line unicorn/no-null
prepareForCommit: () => null,
resetAfterCommit: (root) => {
root.render()
},
preparePortalMount: () => raise("Portals are not supported"),
getPublicInstance: () => raise("Refs are currently not supported"),
appendInitialChild: () => raise("not implemented"),
finalizeInitialChildren: () => false,
}
export const reconciler = ReactReconciler(config)

34
src.new/root-node.ts Normal file
View File

@@ -0,0 +1,34 @@
import type { CommandInteraction, MessageOptions } from "discord.js"
import type { TextNode } from "./text-node.js"
export class RootNode {
private children = new Set<TextNode>()
constructor(private interaction: CommandInteraction) {}
add(child: TextNode) {
this.children.add(child)
}
clear() {
this.children.clear()
}
remove(child: TextNode) {
this.children.delete(child)
}
render() {
this.interaction.reply(this.getMessageOptions()).catch(console.error)
}
getMessageOptions() {
const options: MessageOptions = {}
for (const child of this.children) {
options.content = (options.content ?? "") + child.text
}
return options
}
}

3
src.new/text-node.ts Normal file
View File

@@ -0,0 +1,3 @@
export class TextNode {
constructor(public text: string) {}
}