@arona/core 0.0.1-dev.9d74001

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # @arona/core
package/bin/cli.js ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env node
2
+
3
+ const { run } = require("@arona/core/cli")
4
+
5
+ run()
package/biome.json ADDED
@@ -0,0 +1,32 @@
1
+ {
2
+ "$schema": "../../node_modules/@biomejs/biome/configuration_schema.json",
3
+ "vcs": {
4
+ "enabled": false,
5
+ "clientKind": "git",
6
+ "useIgnoreFile": false
7
+ },
8
+ "files": {
9
+ "ignoreUnknown": false,
10
+ "ignore": [".turbo", "dist"],
11
+ "include": ["src/*", "biome.json", "package.json"]
12
+ },
13
+ "formatter": {
14
+ "enabled": true,
15
+ "indentStyle": "tab"
16
+ },
17
+ "organizeImports": {
18
+ "enabled": true
19
+ },
20
+ "linter": {
21
+ "enabled": true,
22
+ "rules": {
23
+ "recommended": true
24
+ }
25
+ },
26
+ "javascript": {
27
+ "formatter": {
28
+ "quoteStyle": "double",
29
+ "semicolons": "asNeeded"
30
+ }
31
+ }
32
+ }
package/package.json ADDED
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "@arona/core",
3
+ "version": "0.0.1-dev.9d74001",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "git+https://github.com/djobbo/arona.git"
7
+ },
8
+ "license": "MIT",
9
+ "main": "./dist/index.cjs",
10
+ "module": "./dist/index.js",
11
+ "types": "./dist/index.d.ts",
12
+ "type": "module",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "require": "./dist/index.cjs",
17
+ "import": "./dist/index.js"
18
+ },
19
+ "./cli": {
20
+ "types": "./dist/cli.d.ts",
21
+ "require": "./dist/cli.cjs",
22
+ "import": "./dist/cli.js"
23
+ }
24
+ },
25
+ "bin": {
26
+ "arona": "bin/cli.js"
27
+ },
28
+ "scripts": {
29
+ "build": "tsup --entry.index src/index.ts --entry.cli src/cli/index.ts --format esm,cjs --dts",
30
+ "dev": "tsup --entry.index src/index.ts --entry.cli src/cli/index.ts --format esm,cjs --dts --watch",
31
+ "lint": "biome check --write",
32
+ "check:lint": "biome check",
33
+ "check:types": "tsc --noEmit"
34
+ },
35
+ "devDependencies": {
36
+ "@biomejs/biome": "^1.9.4",
37
+ "@types/bun": "latest",
38
+ "@types/react": "18.3.0",
39
+ "@types/react-reconciler": "0.28.8",
40
+ "@types/uuid": "^10.0.0",
41
+ "@types/webpack-node-externals": "^3.0.4",
42
+ "node-loader": "^2.1.0",
43
+ "react-refresh": "^0.17.0",
44
+ "tsup": "^8.5.0"
45
+ },
46
+ "peerDependencies": {
47
+ "typescript": "^5.8.3"
48
+ },
49
+ "dependencies": {
50
+ "@rspack/core": "^1.3.10",
51
+ "@rspack/plugin-react-refresh": "^1.4.3",
52
+ "dotenv": "^16.5.0",
53
+ "react": "18.3.0",
54
+ "react-reconciler": "0.28.0",
55
+ "uuid": "^11.1.0",
56
+ "webpack": "^5.99.8",
57
+ "webpack-node-externals": "^3.0.0"
58
+ }
59
+ }
@@ -0,0 +1,46 @@
1
+ import path from "node:path"
2
+ import { rspack } from "@rspack/core"
3
+ import { createRspackConfig } from "./rspack.config"
4
+
5
+ export const build = async (entry: string) => {
6
+ console.log("🏗️ Starting production build...")
7
+
8
+ const rootPath = process.cwd()
9
+ const outputFolder = path.resolve(rootPath, "dist")
10
+ const outputFile = "index.js"
11
+
12
+ const compiler = rspack(
13
+ createRspackConfig({
14
+ entry,
15
+ outputFile,
16
+ outputFolder,
17
+ mode: "production",
18
+ hot: false,
19
+ }),
20
+ )
21
+
22
+ return new Promise<void>((resolve, reject) => {
23
+ compiler.run((err, stats) => {
24
+ if (err) {
25
+ console.error("Build failed:", err)
26
+ reject(err)
27
+ return
28
+ }
29
+
30
+ if (stats?.hasErrors()) {
31
+ console.error("Build failed with errors:", stats.toString())
32
+ reject(new Error("Build failed with errors"))
33
+ return
34
+ }
35
+
36
+ console.log("✅ Build completed successfully!")
37
+ console.log(
38
+ stats?.toString({
39
+ chunks: false,
40
+ colors: true,
41
+ }),
42
+ )
43
+ resolve()
44
+ })
45
+ })
46
+ }
package/src/cli/dev.ts ADDED
@@ -0,0 +1,79 @@
1
+ import child from "node:child_process"
2
+ import path from "node:path"
3
+ import { rspack } from "@rspack/core"
4
+ import { createRspackConfig } from "./rspack.config"
5
+
6
+ const startDevServer = async (
7
+ {
8
+ entry,
9
+ outputFile,
10
+ outputFolder: outputPath,
11
+ }: {
12
+ entry: string
13
+ outputFile: string
14
+ outputFolder: string
15
+ },
16
+ callback: Parameters<typeof rspack.Compiler.prototype.watch>[1],
17
+ ) => {
18
+ const compiler = rspack(
19
+ createRspackConfig({
20
+ entry,
21
+ outputFile,
22
+ outputFolder: outputPath,
23
+ mode: "development",
24
+ hot: true,
25
+ }),
26
+ )
27
+
28
+ compiler.hooks.watchRun.tap("WatchRunPlugin", (compiler) => {
29
+ console.log("Compilation starting after file change...")
30
+ })
31
+
32
+ return compiler.watch(
33
+ {
34
+ ignored: /node_modules/,
35
+ aggregateTimeout: 300,
36
+ poll: 1000,
37
+ },
38
+ callback,
39
+ )
40
+ }
41
+
42
+ export const dev = async (entry: string, argv: string[] = []) => {
43
+ console.log("⚙️ Starting development server...")
44
+
45
+ const rootPath = process.cwd()
46
+ const outputFolder = path.resolve(rootPath, "node_modules", ".arona")
47
+ const outputFile = "index.js"
48
+ const outputPath = path.join(outputFolder, outputFile)
49
+
50
+ let childProcess: child.ChildProcess | null = null
51
+
52
+ await new Promise<void>((resolve, reject) => {
53
+ startDevServer({ entry, outputFile, outputFolder }, (err, stats) => {
54
+ if (err) {
55
+ console.error({ err })
56
+ reject(err)
57
+ return
58
+ }
59
+
60
+ if (stats?.hash) {
61
+ if (!childProcess) {
62
+ childProcess = child.fork(outputPath, argv, {
63
+ env: {
64
+ ...process.env,
65
+ NODE_ENV: "development",
66
+ },
67
+ stdio: "inherit",
68
+ })
69
+
70
+ resolve()
71
+ console.log("⬆️ Development server is up!")
72
+ }
73
+ // Don't restart the process - let HMR handle the updates
74
+ }
75
+ })
76
+ })
77
+
78
+ console.log("⬆️ Development server is ready for HMR!")
79
+ }
@@ -0,0 +1,51 @@
1
+ import path from "node:path"
2
+ import dotenv from "dotenv"
3
+ import { build } from "./build"
4
+ import { dev } from "./dev"
5
+
6
+ export const run = () => {
7
+ const [, , cmd, ...rest] = process.argv
8
+
9
+ // Load environment variables from .env file
10
+ dotenv.config({
11
+ path: path.resolve(process.cwd(), ".env"),
12
+ })
13
+
14
+ switch (cmd) {
15
+ case "dev": {
16
+ const [entryPoint] = rest
17
+ if (!entryPoint) {
18
+ console.error("Please provide an entry point file path.")
19
+ console.error("Usage: arona dev <entry-point>")
20
+ process.exit(1)
21
+ }
22
+ const entry = path.resolve(process.cwd(), entryPoint)
23
+ dev(entry)
24
+
25
+ break
26
+ }
27
+ case "build": {
28
+ const [entryPoint] = rest
29
+ if (!entryPoint) {
30
+ console.error("Please provide an entry point file path.")
31
+ console.error("Usage: arona build <entry-point>")
32
+ process.exit(1)
33
+ }
34
+ const entry = path.resolve(process.cwd(), entryPoint)
35
+ build(entry)
36
+ break
37
+ }
38
+ case undefined:
39
+ case "help":
40
+ console.log("Usage: arona <command> [options]")
41
+ console.log("Commands:")
42
+ console.log(
43
+ " dev <entry-point> Start the development server with the specified entry point.",
44
+ )
45
+ break
46
+ default:
47
+ console.error(`Unknown command: ${cmd}`)
48
+ console.error("Usage: arona dev <entry-point>")
49
+ process.exit(1)
50
+ }
51
+ }
@@ -0,0 +1,93 @@
1
+ import type { Configuration } from "@rspack/core"
2
+ import { rspack } from "@rspack/core"
3
+ import type { ExternalItemFunctionData } from "@rspack/core"
4
+ import ReactRefreshPlugin from "@rspack/plugin-react-refresh"
5
+ import nodeExternals from "webpack-node-externals"
6
+
7
+ export interface RspackConfigOptions {
8
+ entry: string
9
+ outputFile: string
10
+ outputFolder: string
11
+ mode: "development" | "production"
12
+ hot?: boolean
13
+ }
14
+
15
+ export const createRspackConfig = ({
16
+ entry,
17
+ outputFile,
18
+ outputFolder,
19
+ mode,
20
+ hot = false,
21
+ }: RspackConfigOptions): Configuration => {
22
+ const plugins = []
23
+
24
+ if (hot) {
25
+ plugins.push(
26
+ new ReactRefreshPlugin(),
27
+ new rspack.HotModuleReplacementPlugin(),
28
+ )
29
+ }
30
+
31
+ return {
32
+ target: "node",
33
+ mode,
34
+ entry: {
35
+ main: hot ? ["webpack/hot/poll?100", entry] : entry,
36
+ },
37
+ devtool: mode === "development" ? "source-map" : false,
38
+ externals: [
39
+ // Use nodeExternals to handle most node_modules
40
+ // @ts-expect-error - webpack-node-externals type is not compatible with rspack
41
+ nodeExternals({
42
+ allowlist: hot ? ["webpack/hot/poll"] : [],
43
+ }),
44
+ ],
45
+ externalsPresets: {
46
+ node: true,
47
+ },
48
+ plugins,
49
+ output: {
50
+ filename: outputFile,
51
+ path: outputFolder,
52
+ clean: true,
53
+ },
54
+ module: {
55
+ rules: [
56
+ {
57
+ test: /\.(j|t)sx?$/,
58
+ use: {
59
+ loader: "builtin:swc-loader",
60
+ options: {
61
+ jsc: {
62
+ parser: {
63
+ syntax: "typescript",
64
+ tsx: true,
65
+ },
66
+ transform: {
67
+ react: {
68
+ runtime: "automatic",
69
+ development: mode === "development",
70
+ refresh: hot,
71
+ },
72
+ },
73
+ },
74
+ },
75
+ },
76
+ type: "javascript/auto",
77
+ },
78
+ {
79
+ test: /\.node$/,
80
+ loader: "node-loader",
81
+ type: "javascript/auto",
82
+ },
83
+ {
84
+ test: /\.woff2$/,
85
+ type: "asset",
86
+ },
87
+ ],
88
+ },
89
+ resolve: {
90
+ extensions: [".tsx", ".ts", ".js", ".jsx", ".node"],
91
+ },
92
+ }
93
+ }
@@ -0,0 +1,7 @@
1
+ export function assertIsDefined<T>(
2
+ value: T | undefined,
3
+ errorMessage?: string,
4
+ ): asserts value is T {
5
+ if (value !== undefined) return
6
+ throw new Error(errorMessage ?? "Unexpected undefined value")
7
+ }
@@ -0,0 +1,21 @@
1
+ export function debounce<T extends unknown[], R>(
2
+ fn: (...args: T) => Promise<R> | R,
3
+ delay = 300,
4
+ ): (...args: T) => Promise<R> {
5
+ let timeoutId: ReturnType<typeof setTimeout> | undefined
6
+
7
+ return (...args: T): Promise<R> => {
8
+ return new Promise((resolve, reject) => {
9
+ if (timeoutId) clearTimeout(timeoutId)
10
+
11
+ timeoutId = setTimeout(async () => {
12
+ try {
13
+ const result = await fn(...args)
14
+ resolve(result)
15
+ } catch (error) {
16
+ reject(error)
17
+ }
18
+ }, delay)
19
+ })
20
+ }
21
+ }
package/src/index.ts ADDED
@@ -0,0 +1,8 @@
1
+ export * from "./renderer/create-renderer"
2
+ export * from "./renderer/nodes/root"
3
+ export * from "./renderer/nodes/node"
4
+ export * from "./renderer/nodes/text"
5
+ export * from "./renderer/helpers/define-component"
6
+ export * from "./renderer/helpers/render-nodes"
7
+ export * from "./renderer/helpers/render-text-content"
8
+ export * from "./renderer/root-node-provider"
@@ -0,0 +1,33 @@
1
+ import createReconciler, { type Reconciler } from "react-reconciler"
2
+ import { hostConfig } from "./host-config"
3
+ import type { AronaNode } from "./nodes/node"
4
+ import type { AronaRootNode } from "./nodes/root"
5
+ import type { AronaTextNode } from "./nodes/text"
6
+
7
+ export type ReconcilerInstance = Reconciler<
8
+ AronaNode,
9
+ AronaNode,
10
+ AronaTextNode,
11
+ unknown,
12
+ unknown
13
+ >
14
+
15
+ export const createRenderer = <RootNode extends AronaRootNode, Props>(
16
+ createRootNode: (
17
+ reconcilerInstance: ReconcilerInstance,
18
+ props: Props,
19
+ ) => RootNode,
20
+ ) => {
21
+ const reactReconcilerInstance = createReconciler(hostConfig)
22
+ reactReconcilerInstance.injectIntoDevTools({
23
+ bundleType: process.env.NODE_ENV === "development" ? 1 : 0,
24
+ rendererPackageName: "arona",
25
+ version: "0.0.0",
26
+ })
27
+
28
+ return async (Code: () => JSX.Element, props: Props) => {
29
+ const rootNode = createRootNode(reactReconcilerInstance, props)
30
+ await rootNode.initialRender(Code)
31
+ return rootNode
32
+ }
33
+ }
@@ -0,0 +1,48 @@
1
+ import type { FC } from "react"
2
+ import type { AronaNode } from "../nodes/node"
3
+ import { renderComponentNodes } from "./render-nodes"
4
+
5
+ export type AronaComponent<
6
+ Props = unknown,
7
+ RenderOutput extends object | null = null,
8
+ > = {
9
+ name: string
10
+ component: FC<Props>
11
+ render: (node: AronaNode<Props>) => RenderOutput
12
+ guard: (node?: AronaNode | null) => node is AronaNode<Props>
13
+ }
14
+ export const defineComponent = <
15
+ Props = unknown,
16
+ RenderOutput extends object | null = null,
17
+ // biome-ignore lint/suspicious/noExplicitAny: we need to allow any here
18
+ AllowedChildren extends AronaComponent<any, any>[] = AronaComponent<
19
+ // biome-ignore lint/suspicious/noExplicitAny: we need to allow any here
20
+ any,
21
+ // biome-ignore lint/suspicious/noExplicitAny: we need to allow any here
22
+ any
23
+ >[],
24
+ >({
25
+ name,
26
+ allowedChildren,
27
+ render,
28
+ }: {
29
+ name: string
30
+ allowedChildren: AllowedChildren
31
+ render: (
32
+ renderChildren: (nodes: AronaNode | AronaNode[]) => NonNullable<
33
+ // biome-ignore lint/suspicious/noExplicitAny: we need to allow any here
34
+ AllowedChildren[number] extends AronaComponent<any, infer R> ? R : never
35
+ >[],
36
+ ) => (node: AronaNode<Props>) => RenderOutput
37
+ }): AronaComponent<Props, RenderOutput> => {
38
+ const renderChildren = renderComponentNodes(allowedChildren)
39
+
40
+ return {
41
+ name,
42
+ component: name as unknown as FC<Props>,
43
+ render: render(renderChildren),
44
+ guard: (node): node is AronaNode<Props> => {
45
+ return node?.type === name
46
+ },
47
+ }
48
+ }
@@ -0,0 +1,41 @@
1
+ import type { AronaNode } from "../nodes/node"
2
+ import type { AronaComponent } from "./define-component"
3
+
4
+ const renderNode =
5
+ // biome-ignore lint/suspicious/noExplicitAny: we need to allow any here
6
+ <Components extends AronaComponent<any, any>[]>(components: Components) => {
7
+ return (
8
+ node?: AronaNode | null,
9
+ ): NonNullable<
10
+ // biome-ignore lint/suspicious/noExplicitAny: we need to allow any here
11
+ Components[number] extends AronaComponent<any, infer Output>
12
+ ? Output
13
+ : never
14
+ > | null => {
15
+ if (!node) return null
16
+
17
+ for (const component of components) {
18
+ if (component.guard(node)) {
19
+ return component.render(node)
20
+ }
21
+ }
22
+
23
+ // TODO: add a callback to the renderer to handle unhandled nodes
24
+ throw new Error(
25
+ `Unhandled element type: ${node.type} inside ${node.parent?.type}`,
26
+ )
27
+ }
28
+ }
29
+
30
+ export const renderComponentNodes =
31
+ // biome-ignore lint/suspicious/noExplicitAny: we need to allow any here
32
+ <Components extends AronaComponent<any, any>[]>(components: Components) => {
33
+ return (nodes: AronaNode | AronaNode[]) => {
34
+ const nodesToRender = Array.isArray(nodes) ? nodes : [nodes]
35
+ const render = renderNode(components)
36
+
37
+ return nodesToRender
38
+ .map(render)
39
+ .filter((output): output is NonNullable<typeof output> => !!output)
40
+ }
41
+ }
@@ -0,0 +1,26 @@
1
+ import type { AronaNode } from "../nodes/node"
2
+ import { isTextNode } from "../nodes/text"
3
+
4
+ export const renderTextContent = (
5
+ node: AronaNode,
6
+ textElementsOnly?: boolean,
7
+ ): string => {
8
+ if (isTextNode(node)) {
9
+ return node.innerText
10
+ }
11
+
12
+ const innerText = node.children
13
+ .map((child) => renderTextContent(child, true))
14
+ .join("")
15
+
16
+ if (!innerText) return ""
17
+
18
+ switch (node.type) {
19
+ case "arona:__text":
20
+ return innerText
21
+ default:
22
+ if (textElementsOnly)
23
+ throw new Error(`Unexpected element type: ${node.type} inside Text`)
24
+ return innerText
25
+ }
26
+ }
@@ -0,0 +1,84 @@
1
+ import type { HostConfig } from "react-reconciler"
2
+ import { AronaNode } from "./nodes/node"
3
+ import type { AronaRootNode } from "./nodes/root"
4
+ import { AronaTextNode } from "./nodes/text"
5
+
6
+ export const hostConfig: HostConfig<
7
+ string,
8
+ Record<string, unknown>,
9
+ AronaNode,
10
+ AronaNode,
11
+ AronaTextNode,
12
+ unknown,
13
+ unknown,
14
+ unknown,
15
+ null,
16
+ true,
17
+ unknown,
18
+ unknown,
19
+ unknown
20
+ > = {
21
+ getChildHostContext: () => null,
22
+ prepareForCommit: () => null,
23
+ resetAfterCommit(root: AronaRootNode) {
24
+ root.render()
25
+ },
26
+ createInstance: (tag, attr) => new AronaNode(tag, attr),
27
+ appendInitialChild: (parent, node) => {
28
+ parent.insertBefore(node)
29
+ },
30
+ finalizeInitialChildren: () => false,
31
+ prepareUpdate: () => true,
32
+ shouldSetTextContent: () => false,
33
+ createTextInstance: (textContent: string) => new AronaTextNode(textContent),
34
+ commitMount() {},
35
+ commitUpdate(node, _updatePayload, _tag, _oldAttr, attr) {
36
+ node.replaceAttributes(attr)
37
+ },
38
+ resetTextContent() {},
39
+ commitTextUpdate(textNode, _oldTextContent, textContent) {
40
+ textNode.setTextContent(textContent)
41
+ },
42
+ appendChild(parent, node) {
43
+ parent.insertBefore(node)
44
+ },
45
+ appendChildToContainer(parent, node) {
46
+ parent.insertBefore(node)
47
+ },
48
+ insertBefore(parent, node, anchor?: AronaNode) {
49
+ parent.insertBefore(node, anchor)
50
+ },
51
+ insertInContainerBefore(parent, node, anchor?: AronaNode) {
52
+ parent.insertBefore(node, anchor)
53
+ },
54
+ removeChild(parent, node: AronaNode) {
55
+ parent.removeChild(node)
56
+ },
57
+ removeChildFromContainer(parent, node: AronaNode) {
58
+ parent.removeChild(node)
59
+ },
60
+ hideInstance() {},
61
+ hideTextInstance() {},
62
+ unhideInstance() {},
63
+ unhideTextInstance() {},
64
+ clearContainer(node: AronaNode) {
65
+ node.clear()
66
+ },
67
+ getRootHostContext: () => null,
68
+ supportsMutation: true,
69
+ supportsPersistence: false,
70
+ getPublicInstance() {},
71
+ preparePortalMount() {},
72
+ scheduleTimeout() {},
73
+ cancelTimeout() {},
74
+ noTimeout: true,
75
+ isPrimaryRenderer: true,
76
+ supportsHydration: false,
77
+ getCurrentEventPriority: () => 99,
78
+ getInstanceFromNode: () => null,
79
+ beforeActiveInstanceBlur: () => null,
80
+ afterActiveInstanceBlur: () => null,
81
+ prepareScopeUpdate: () => null,
82
+ getInstanceFromScope: () => null,
83
+ detachDeletedInstance: () => null,
84
+ }
@@ -0,0 +1,76 @@
1
+ import { v4 as uuidv4 } from "uuid"
2
+
3
+ export class AronaNode<Props = Record<string, unknown>> {
4
+ uuid: string
5
+ type: string
6
+ children: AronaNode[] = []
7
+ root: AronaNode | null = null
8
+ parent: AronaNode | null = null
9
+
10
+ props: Partial<Props>
11
+
12
+ constructor(type: string, props?: Partial<Props>, root?: AronaNode) {
13
+ this.uuid = uuidv4()
14
+ this.type = type
15
+ this.children = []
16
+ this.props = props || {}
17
+ this.root = root || null
18
+ }
19
+
20
+ setParent(node: AronaNode): void {
21
+ this.parent = node
22
+ }
23
+
24
+ insertBefore(node: AronaNode, anchor?: AronaNode): void {
25
+ if (!node) throw new Error("Wrong child type")
26
+ if (anchor) {
27
+ const anchorIndex = this.children.findIndex((child) => anchor === child)
28
+ this.children.splice(anchorIndex, 0, node)
29
+ } else {
30
+ this.children.push(node)
31
+ }
32
+
33
+ node.setParent(this)
34
+ }
35
+
36
+ setAttribute(name: string, value: unknown): void {
37
+ this.props[name as keyof Props] = value as Props[keyof Props]
38
+ }
39
+
40
+ replaceAttributes(attr: Record<string, unknown>): void {
41
+ this.props = attr as Props
42
+ }
43
+
44
+ get parentNode(): AronaNode {
45
+ if (!this.parent) throw new TypeError(`Couldn't find parent of ${this}`)
46
+ return this.parent
47
+ }
48
+
49
+ get firstChild(): AronaNode | undefined {
50
+ return this.children[0]
51
+ }
52
+
53
+ get nextSibling(): AronaNode["children"][number] | undefined {
54
+ const parent = this.parentNode
55
+ if (!parent) throw new TypeError(`Couldn't find parent of ${this}`)
56
+
57
+ const nodeIndex = parent.children.findIndex((child) => child === this)
58
+
59
+ if (nodeIndex < 0) throw new TypeError(`Bad node ${this}`)
60
+
61
+ return parent.children[nodeIndex + 1]
62
+ }
63
+
64
+ removeChild(node: AronaNode): void {
65
+ this.children = this.children.filter((child) => child !== node)
66
+ }
67
+
68
+ clear(): void {
69
+ this.children.map((child) => child.clear())
70
+ this.children = []
71
+ }
72
+
73
+ render() {
74
+ this.root?.render()
75
+ }
76
+ }
@@ -0,0 +1,81 @@
1
+ import type { FiberRoot, Reconciler } from "react-reconciler"
2
+ import { debounce } from "../../helpers/debounce"
3
+ import type { AronaComponent } from "../helpers/define-component"
4
+ import { RootNodeProvider } from "../root-node-provider"
5
+ import { AronaNode } from "./node"
6
+ import type { AronaTextNode } from "./text"
7
+
8
+ const ROOT_NODE_UPDATE_DEBOUNCE_MS = 50
9
+
10
+ export class AronaRootNode<
11
+ Props = unknown,
12
+ RenderOutput extends object | null = null,
13
+ > extends AronaNode {
14
+ reconcilerInstance: Reconciler<
15
+ AronaNode,
16
+ AronaNode,
17
+ AronaTextNode,
18
+ unknown,
19
+ unknown
20
+ >
21
+ #rootContainer: FiberRoot
22
+ #unmountTimeout: Timer | null = null
23
+ unmounted = false
24
+
25
+ constructor(
26
+ reconcilerInstance: Reconciler<
27
+ AronaNode,
28
+ AronaNode,
29
+ AronaTextNode,
30
+ unknown,
31
+ unknown
32
+ >,
33
+ components: AronaComponent<Props, RenderOutput>[],
34
+ ) {
35
+ super("arona:__root")
36
+ this.reconcilerInstance = reconcilerInstance
37
+ this.#rootContainer = this.reconcilerInstance.createContainer(
38
+ this,
39
+ 1, // ConcurrentRoot
40
+ null,
41
+ false,
42
+ false,
43
+ "",
44
+ (error) => {
45
+ // console.trace("Error in root", error)
46
+ },
47
+ null,
48
+ )
49
+ }
50
+
51
+ unmount() {
52
+ this.unmounted = true
53
+ this.#unmountTimeout && clearTimeout(this.#unmountTimeout)
54
+ this.reconcilerInstance.updateContainer(null, this.#rootContainer)
55
+ }
56
+
57
+ async initialRender(Code: () => JSX.Element) {
58
+ this.reconcilerInstance.updateContainer(
59
+ <RootNodeProvider<Props, RenderOutput> rootNode={this}>
60
+ <Code />
61
+ </RootNodeProvider>,
62
+ this.#rootContainer,
63
+ )
64
+
65
+ await this.render()
66
+ }
67
+
68
+ render = debounce(async () => {
69
+ if (this.unmounted) return
70
+ await this.performRender()
71
+ }, ROOT_NODE_UPDATE_DEBOUNCE_MS)
72
+
73
+ protected async performRender() {
74
+ // To be implemented by subclasses
75
+ throw new Error("performRender must be implemented by subclass")
76
+ }
77
+ }
78
+
79
+ export const isRootNode = (node?: AronaNode | null): node is AronaRootNode => {
80
+ return node?.type === "arona:__root"
81
+ }
@@ -0,0 +1,22 @@
1
+ import { AronaNode } from "./node"
2
+
3
+ export class AronaTextNode extends AronaNode {
4
+ #textContent = ""
5
+
6
+ constructor(textContent: string) {
7
+ super("arona:__text")
8
+ this.#textContent = textContent
9
+ }
10
+
11
+ setTextContent(textContent: string) {
12
+ this.#textContent = textContent
13
+ }
14
+
15
+ get innerText(): string {
16
+ return this.#textContent
17
+ }
18
+ }
19
+
20
+ export const isTextNode = (node?: AronaNode | null): node is AronaTextNode => {
21
+ return node?.type === "arona:__text"
22
+ }
@@ -0,0 +1,42 @@
1
+ import { createContext, useContext } from "react"
2
+ import type { AronaRootNode } from "./nodes/root"
3
+
4
+ const rootNodeContext = createContext<AronaRootNode | null>(null)
5
+
6
+ export const useRootNode = <
7
+ Props = unknown,
8
+ RenderOutput extends object | null = null,
9
+ >() => {
10
+ const rootNode = useContext(rootNodeContext) as AronaRootNode<
11
+ Props,
12
+ RenderOutput
13
+ > | null
14
+
15
+ if (!rootNode) {
16
+ throw new Error("useRootNode must be used inside a RootNodeProvider")
17
+ }
18
+
19
+ return rootNode
20
+ }
21
+
22
+ export type MessageProviderProps<
23
+ Props = unknown,
24
+ RenderOutput extends object | null = null,
25
+ > = {
26
+ rootNode: AronaRootNode<Props, RenderOutput>
27
+ children?: JSX.Element
28
+ }
29
+
30
+ export const RootNodeProvider = <
31
+ Props = unknown,
32
+ RenderOutput extends object | null = null,
33
+ >({
34
+ rootNode,
35
+ children,
36
+ }: MessageProviderProps<Props, RenderOutput>) => {
37
+ return (
38
+ <rootNodeContext.Provider value={rootNode}>
39
+ {children}
40
+ </rootNodeContext.Provider>
41
+ )
42
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "compilerOptions": {
3
+ // Environment setup & latest features
4
+ "lib": [
5
+ "ESNext"
6
+ ],
7
+ "target": "ESNext",
8
+ "module": "ESNext",
9
+ "moduleDetection": "force",
10
+ "jsx": "react-jsx",
11
+ "allowJs": true,
12
+ // Bundler mode
13
+ "moduleResolution": "bundler",
14
+ "allowImportingTsExtensions": true,
15
+ "verbatimModuleSyntax": true,
16
+ "noEmit": true,
17
+ // Best practices
18
+ "strict": true,
19
+ "skipLibCheck": true,
20
+ "noFallthroughCasesInSwitch": true,
21
+ "noUncheckedIndexedAccess": true,
22
+ // Some stricter flags (disabled by default)
23
+ "noUnusedLocals": false,
24
+ "noUnusedParameters": false,
25
+ "noPropertyAccessFromIndexSignature": false,
26
+ "declaration": true,
27
+ },
28
+ "include": [
29
+ "src/**/*"
30
+ ]
31
+ }