This repository has been archived by the owner on Feb 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathLambda_term.re
209 lines (175 loc) · 4.95 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
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!
*/
module Reconciler = {
open LTerm_widget;
/*
Step 1: Define primitives
*/
type buttonProperties = {
text: string,
onClick: unit => unit,
};
type primitives =
| Vbox
| Hbox
| Label(string)
| Button(buttonProperties);
/*
Step 2: Define node type
*/
/* For this, ideally we could just use the base widget class
* There are some quirks with polymorphic typing, but this
* could possibly be simplified.
*/
class widget = class LTerm_widget.t;
type node =
| Label(LTerm_widget.label)
| Button(LTerm_widget.button)
| Container(LTerm_widget.box);
/*
Step 3: Implement a create function
*/
let createInstance = prim =>
switch (prim) {
| Vbox => Container(new vbox)
| Hbox => Container(new hbox)
| Label(txt) =>
let label = (new label)(txt);
Label(label);
| Button(props) =>
let button = (new button)(props.text);
button#on_click(props.onClick);
Button(button);
};
/*
Step 4: Implement remaining primitives
*/
let _getInnerNode = node =>
switch (node) {
| Label(x) => (x :> widget)
| Button(y) => (y :> widget)
| Container(z) => (z :> widget)
};
let updateInstance =
(node: node, _oldPrimitive: primitives, newPrimitive: primitives) =>
switch (newPrimitive, node) {
| (Label(txt), Label(n)) => n#set_text(txt)
| (Button(buttonProps), Button(n)) =>
n#set_label(buttonProps.text);
n#on_click(buttonProps.onClick);
| _ => ()
};
let appendChild = (parentNode: node, childNode: node) =>
switch (parentNode) {
| Container(n) => n#add(_getInnerNode(childNode))
| _ => ()
};
let removeChild = (parentNode: node, childNode: node) =>
switch (parentNode) {
| Container(n) => n#remove(_getInnerNode(childNode))
| _ => ()
};
let replaceChild = (parentNode: node, oldChild: node, newChild: node) => {
removeChild(parentNode, oldChild);
appendChild(parentNode, newChild);
};
};
/* Step 5: Hook it up! */
module LambdaReact = Reactify.Make(Reconciler);
open LambdaReact;
/* Define our primitive components */
let hbox = (~children, ()) => primitiveComponent(Hbox, ~children);
let vbox = (~children, ()) => primitiveComponent(Vbox, ~children);
let label = (~children, ~text, ()) =>
primitiveComponent(Label(text), ~children);
let button = (~children, ~text, ~onClick, ()) => {
let buttonProps: Reconciler.buttonProperties = {text, onClick};
primitiveComponent(Button(buttonProps), ~children);
};
/* And let's create some custom components! */
/*
CounterButtons
This shows how you can use reducers with a callback from a primitive.
*/
type action =
| Increment
| Decrement;
let reducer = (state, action) =>
switch (action) {
| Increment => state + 1
| Decrement => state - 1
};
let renderCounter = () =>
useReducerExperimental(reducer, 0, ((count, dispatch)) =>
<hbox>
<button text="Decrement" onClick={() => dispatch(Decrement)} />
<label text={"Counter: " ++ string_of_int(count)} />
<button text="Increment" onClick={() => dispatch(Increment)} />
</hbox>
);
module CounterButtons = (
val createComponent((render, ~children, ()) =>
render(renderCounter, ~children)
)
);
/*
Clock
Custom clock component to show the time. Demonstrates
use of `useEffect` and `setState` together.
*/
module Clock = (
val createComponent((render, ~children, ()) =>
render(
() =>
useStateExperimental(0., ((time, setTime)) =>
useEffectExperimental(
() => {
let evt =
Lwt_engine.on_timer(1.0, true, _ => setTime(Unix.time()));
() => Lwt_engine.stop_event(evt);
},
() => <label text={"Time: " ++ string_of_float(time)} />,
)
),
~children,
)
)
);
let main = () => {
let (waiter, wakener) = wait();
/* Create a container for our UI */
let body = new vbox;
let root = Reconciler.Container(body);
let container = createContainer(root);
let quit = () => wakeup(wakener, ());
/* Let's finally put our UI to use! */
let render = () =>
<vbox>
<label text="Hello from Reactify!" />
<Clock />
<CounterButtons />
<button onClick=quit text="Quit" />
</vbox>;
/* First render! */
LambdaReact.updateContainer(container, render());
Lazy.force(LTerm.stdout)
>>= (
term =>
LTerm.enable_mouse(term)
>>= (
() =>
Lwt.finalize(
() => run(term, body, waiter),
() => LTerm.disable_mouse(term),
)
)
);
};
let () = Lwt_main.run(main());