-
Notifications
You must be signed in to change notification settings - Fork 17
/
Lambda_term.re
277 lines (245 loc) · 6.71 KB
/
Lambda_term.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
open Lwt;
open LTerm_widget;
/**
* This is an implementation of a reconciler for the Lambda_term widget library:
* https://github.com/ocaml-community/lambda-term
*
* This is just an example but you could use this to create interesting
* CLI apps, with a react-like functional API!
*/
/*
Step 1: Define the reconciler template
*/
module Reconciler = {
type hostElement =
| Label(LTerm_widget.label)
| Button(LTerm_widget.button)
| Container(LTerm_widget.box);
type node = hostElement;
module RemoteAction = Brisk_reconciler.RemoteAction;
let onStale = RemoteAction.create();
let insertNode = (~parent: node, ~child: node, ~position as _) => {
switch (parent, child) {
| (Container(box), Label(child)) => box#add(child)
| (Container(box), Button(child)) => box#add(child)
| (Container(box), Container(child)) => box#add(child)
| _ => ()
};
parent;
};
let deleteNode = (~parent: node, ~child: node, ~position as _) => {
switch (parent, child) {
| (Container(box), Label(child)) => box#remove(child)
| (Container(box), Button(child)) => box#remove(child)
| (Container(box), Container(child)) => box#remove(child)
| _ => ()
};
parent;
};
let moveNode = (~parent, ~child as _, ~from as _, ~to_ as _) => {
parent;
};
let markAsStale = () => RemoteAction.send(~action=(), onStale);
let beginChanges = () => ();
let commitChanges = () => ();
};
/*
Step 2: Create the actual reconciler using Brisk_reconciler.Make
*/
module LambdaReact = Brisk_reconciler.Make(Reconciler);
/*
Step 3: Define some native components (aka primitives)
*/
let hbox = {
let component = LambdaReact.nativeComponent("Hbox");
(~children, ()) => {
component(hooks =>
(
hooks,
{
make: () => {
let node = new LTerm_widget.hbox;
Container(node);
},
configureInstance: (~isFirstRender as _, node) => {
node;
},
children: LambdaReact.listToElement(children),
},
)
);
};
};
let vbox = {
let component = LambdaReact.nativeComponent("Vbox");
(~children, ()) => {
component(hooks =>
(
hooks,
{
make: () => {
let node = new LTerm_widget.vbox;
Container(node);
},
configureInstance: (~isFirstRender as _, node) => {
node;
},
children: LambdaReact.listToElement(children),
},
)
);
};
};
let label = {
let component = LambdaReact.nativeComponent("Label");
(~text, ~children, ()) => {
component(hooks =>
(
hooks,
{
make: () => {
let node = (new LTerm_widget.label)(text);
Label(node);
},
configureInstance: (~isFirstRender as _, node) => {
switch (node) {
| Label(n) => n#set_text(text)
| _ => () /* Should never happen */
};
node;
},
children: LambdaReact.listToElement(children),
},
)
);
};
};
let button = {
let component = LambdaReact.nativeComponent("Button");
(~text, ~onClick, ~children, ()) => {
component(hooks =>
(
hooks,
{
make: () => {
let button = (new button)(text);
button#on_click(onClick);
Button(button);
},
configureInstance: (~isFirstRender as _, node) => {
switch (node) {
| Button(n) => n#set_label(text)
// Lambda_term doesn't allow to replace event handlers, so we only attach it once on `make`
// n#on_click(onClick);
| _ => () /* Should never happen */
};
node;
},
children: LambdaReact.listToElement(children),
},
)
);
};
};
/*
Step 4: Use the reconciler + native elements to create something great!
*/
/*
CounterButtons
This shows how you can use reducers with a callback from a primitive.
*/
type action =
| Increment
| Decrement;
let reducer = (action, state) =>
switch (action) {
| Increment => state + 1
| Decrement => state - 1
};
let counterButtons = {
let component = LambdaReact.component("CounterButtons");
(~children as _: list(unit), ()) =>
component(hooks => {
let (count, dispatch, hooks) =
LambdaReact.Hooks.reducer(~initialState=0, reducer, hooks);
(
hooks,
<hbox>
<button text="Decrement" onClick={() => dispatch(Decrement)} />
<label text={"Counter: " ++ string_of_int(count)} />
<button text="Increment" onClick={() => dispatch(Increment)} />
</hbox>,
);
});
};
/*
Clock
Custom clock component to show the time. Demonstrates
use of `useEffect` and `setState` together.
*/
let clock = {
let component = LambdaReact.component("Clock");
(~children as _: list(unit), ()) =>
component(hooks => {
let (time, setTime, hooks) = LambdaReact.Hooks.state(0., hooks);
let hooks =
LambdaReact.Hooks.effect(
LambdaReact.Hooks.Effect.Always,
() => {
let evt =
Lwt_engine.on_timer(1.0, true, _ => setTime(_ => Unix.time()));
Some(() => Lwt_engine.stop_event(evt));
},
hooks,
);
(hooks, <label text={"Time: " ++ string_of_float(time)} />);
});
};
/*
Step 5: Make the first render
*/
let main = () => {
let (waiter, wakener) = wait();
let quit = () => wakeup(wakener, ());
/* Let's finally put our UI to use! */
let render = () =>
<vbox>
<label text="Hello World!" />
<clock />
<counterButtons />
<button onClick=quit text="Quit" />
</vbox>;
/* Create a container for our UI */
let body = new vbox;
let root = Reconciler.Container(body);
let rendered = {
let rendered = LambdaReact.RenderedElement.render(root, render());
LambdaReact.RenderedElement.executeHostViewUpdates(rendered) |> ignore;
ref(LambdaReact.RenderedElement.executePendingEffects(rendered));
};
let _unsubscribe =
LambdaReact.RemoteAction.subscribe(
~handler=() => {
let nextElement =
LambdaReact.RenderedElement.flushPendingUpdates(rendered^);
LambdaReact.RenderedElement.executeHostViewUpdates(nextElement)
|> ignore;
rendered :=
LambdaReact.RenderedElement.executePendingEffects(nextElement);
},
Reconciler.onStale,
);
Lazy.force(LTerm.stdout)
>>= (
term =>
LTerm.enable_mouse(term)
>>= (
() =>
Lwt.finalize(
() => run(term, body, waiter),
() => LTerm.disable_mouse(term),
)
)
);
};
let () = Lwt_main.run(main());