-
Notifications
You must be signed in to change notification settings - Fork 17
/
WebReconciler.re
209 lines (187 loc) · 4.86 KB
/
WebReconciler.re
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* This is an implementation of a reconciler for DOM elements via js_of_ocaml :
* http://ocsigen.org/js_of_ocaml/3.1.0/api/Dom_html
*
* This is just an example but you could use this to create interesting
* CLI apps, with a react-like functional API!
*/
exception InvalidNodePrimitiveMatchInUpdateInstance;
let str = string_of_int;
module Dom = Js_of_ocaml.Dom;
module Dom_html = Js_of_ocaml.Dom_html;
module Js = Js_of_ocaml.Js;
module RemoteAction = Brisk_reconciler.RemoteAction;
open Brisk_reconciler;
/*
Step 1: Define the reconciler template
*/
type hostElement = Js.t(Dom_html.element);
type node = Js.t(Dom_html.element);
let onStale: RemoteAction.t(unit) = RemoteAction.create();
let insertNode = (~parent: node, ~child: node, ~position as _) => {
Dom.appendChild(parent, child);
parent;
};
let deleteNode = (~parent: node, ~child: node, ~position as _) => {
Dom.removeChild(parent, child);
parent;
};
let moveNode = (~parent, ~child as _, ~from as _, ~to_ as _) => {
parent;
};
Brisk_reconciler.addStaleTreeHandler(() =>
RemoteAction.send(~action=(), onStale)
);
/*
Step 2: Define some native components (aka primitives)
*/
let document = Dom_html.window##.document;
let%nativeComponent view = (~children, (), hooks) => (
{
make: () => {
let node = Dom_html.createDiv(document);
node;
},
configureInstance: (~isFirstRender as _, node) => {
node;
},
children,
insertNode,
deleteNode,
moveNode,
},
hooks,
);
let%nativeComponent image = (~src, ~children, (), hooks) => (
{
make: () => {
let node = Dom_html.createImg(document);
node##.src := Js.string(src);
node |> Dom_html.element;
},
configureInstance: (~isFirstRender as _, n) => {
/* TODO: Proper way to downcast? */
let node: Js.t(Dom_html.imageElement) = Obj.magic(n);
node##.src := Js.string(src);
node |> Dom_html.element;
},
children,
insertNode,
deleteNode,
moveNode,
},
hooks,
);
let%nativeComponent text = (~text, (), hooks) => (
{
make: () => {
let node = Dom_html.createSpan(document);
node##.innerHTML := Js.string(text);
node |> Dom_html.element;
},
configureInstance: (~isFirstRender as _, n) => {
/* TODO: Proper way to downcast? */
let node: Js.t(Dom_html.imageElement) = Obj.magic(n);
node##.innerHTML := Js.string(text);
node |> Dom_html.element;
},
children: empty,
insertNode,
deleteNode,
moveNode,
},
hooks,
);
let%nativeComponent button = (~onPress, ~title, (), hooks) => (
{
make: () => {
let node = Dom_html.createButton(~_type=Js.string("button"), document);
let t = Js.string(title);
node##.title := t;
node##.innerHTML := t;
node##.onclick :=
Dom_html.handler(_e => {
onPress();
Js.bool(false);
});
node |> Dom_html.element;
},
configureInstance: (~isFirstRender as _, n) => {
/* TODO: Proper way to downcast? */
let node: Js.t(Dom_html.imageElement) = Obj.magic(n);
let t = Js.string(title);
node##.title := t;
node##.innerHTML := t;
node##.onclick :=
Dom_html.handler(_e => {
onPress();
Js.bool(false);
});
node |> Dom_html.element;
},
children: empty,
insertNode,
deleteNode,
moveNode,
},
hooks,
);
/*
Step 4: Use the reconciler + native elements to create something great!
*/
type action =
| Increment
| Decrement;
let reducer = (action, state) =>
switch (action) {
| Increment => state + 1
| Decrement => state - 1
};
let action = RemoteAction.create();
let%component counterButtons = () => {
let%hook (count, setCount) = Hooks.state(0);
RemoteAction.subscribe(~handler=n => setCount(_ => n), action) |> ignore;
<view>
<button title="Decrement" onPress={() => setCount(count => count - 1)} />
<text text={"Counter: " ++ str(count)} />
<button
title="Reset (Race condition)"
onPress={() =>
ignore(
Js_of_ocaml.Dom_html.setTimeout(
() => RemoteAction.send(0, action),
3000.,
),
)
}
/>
<button title="Increment" onPress={() => setCount(count => count + 1)} />
</view>;
};
let render = () =>
<view> <text text="Hello World" /> <counterButtons /> </view>;
/*
Step 5: Make the first render
*/
let rendered =
ref(
RenderedElement.render(
{
node: Dom_html.getElementById_exn("app"),
insertNode,
deleteNode,
moveNode,
},
render(),
),
);
RenderedElement.executeHostViewUpdates(rendered^) |> ignore;
RemoteAction.subscribe(
~handler=
() => {
let nextElement = RenderedElement.flushPendingUpdates(rendered^);
RenderedElement.executeHostViewUpdates(nextElement) |> ignore;
rendered := nextElement;
},
onStale,
);