-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.tsx
82 lines (68 loc) · 2.04 KB
/
app.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import React, { useContext } from "react";
import { render } from "react-dom";
import {
TransferViewData,
ViewElement,
ElementType,
ViewElementText,
} from "./contract";
type WorkerViewContext = {
reportClick: (id: number) => void;
};
const WorkerViewContext = React.createContext<WorkerViewContext>({
reportClick: () => {},
});
const Button: React.FC<{ id: number }> = ({ id, children }) => {
const { reportClick } = useContext(WorkerViewContext);
return <button onClick={() => reportClick(id)}> {children}</button>;
};
const Text: React.FC<{ el: ViewElementText }> = ({
el: { type = "span", text, children },
}) => {
const lookup = {
header: "h2",
paragraph: "p",
span: "span",
};
return React.createElement(lookup[type], {}, text, renderIntances(children));
};
const ElementView: React.FC<{ el: ViewElement }> = ({ el }) => {
switch (el.tag) {
case ElementType.view:
return <div>{renderIntances(el.children)}</div>;
case ElementType.btn:
return <Button id={el.id}>{renderIntances(el.children)}</Button>;
case ElementType.text:
return <Text el={el} />;
case ElementType.raw:
return <span>{el.text}</span>;
}
};
const RootView: React.FC<{ data: TransferViewData }> = ({ data }) => (
<>{renderIntances(data.elements)}</>
);
const reactRoot = document.createElement("div");
document.body.appendChild(reactRoot);
const el = document.createElement("pre");
document.body.appendChild(el);
function renderIntances(elements?: ViewElement[]) {
return elements?.map((el, i) => <ElementView el={el} key={i} />);
}
const worker = new Worker("./worker.tsx");
const ctx: WorkerViewContext = {
reportClick: (id) => worker.postMessage({ click: id }),
};
worker.onmessage = (msg) => {
const data: "hi" | TransferViewData = msg.data;
if (data === "hi") {
console.log("worker handshake");
return;
}
el.innerText = JSON.stringify(data, null, 2);
render(
<WorkerViewContext.Provider value={ctx}>
<RootView data={data} />
</WorkerViewContext.Provider>,
reactRoot
);
};