-
Notifications
You must be signed in to change notification settings - Fork 0
/
eliom_shared_frp_POC.eliom
245 lines (209 loc) · 6.58 KB
/
eliom_shared_frp_POC.eliom
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
[%%shared
open Printf
open Eliom_lib
module S = Eliom_shared.React.S
module R = Eliom_content.Html.R
]
[%%shared
type row_model = { v : int; row_valid : bool }
type row_action =
| Set of int
| Try_set of string
| Invalid
(* [@@deriving show] *)
module ROW =
struct
let create v = { v; row_valid = true }
let update (port : row_action FRP.port) m action =
match action with
| Set v -> { v; row_valid = true; }
| Invalid -> { m with row_valid = false; }
| Try_set s -> begin
match int_of_string s with
| exception _ -> { m with row_valid = false }
| v -> { v; row_valid = true; }
end
let view (port : row_action FRP.Types.port) m =
let open Eliom_content.Html.D in
let inp =
Raw.input
~a:[ R.a_value @@ S.map [%shared fun m -> string_of_int m.v] m;
R.a_style @@
S.map
[%shared
function
| { row_valid = true; _ } -> ""
| { row_valid = false; _ } -> "border: 2px solid red"]
m
]
() in
let () =
ignore [%client ( FRP.on_keyups (fun s -> (Try_set s, None)) ~%inp ~%port : unit)]
in
div [ inp ]
end
module IM = ReactMap.Make(struct type t = int let compare = compare end)
type test_model =
{ a : int;
valid : bool;
row_id : int;
rows : row_model IM.t;
submitting : bool;
result : string option;
}
type test_action =
| Set of int
| Try_set of string
| Invalid
| Increment
| Decrement
| Add_row
| Row_action of int * row_action
| Submit
| Submit_result of string
(* [@@deriving show] *)
]
let do_stuff x = Lwt_unix.sleep 2. >> Lwt.return (x ^ "++" ^ x)
[%%shared
let dummy_serv = ~%(Eliom_client.server_function [%derive.json: string] do_stuff)
let mk_row_view port =
[%shared fun id row ->
ROW.view (FRP.tag_port (fun a -> Row_action (id, a)) ~%port) row]
module TEST =
struct
let make a =
{ a; valid = true; row_id = 1;
rows = IM.add 0 (ROW.create 111) IM.empty;
submitting = false; result = None;
}
let update (port : test_action FRP.port) m = function
| Set a -> { m with a; valid = true; }
| Increment -> { m with a = m.a + 1 }
| Decrement -> { m with a = m.a - 1 }
| Invalid -> { m with valid = false }
| Try_set s -> begin
match int_of_string s with
| exception _ -> { m with valid = false }
| a -> { m with a; valid = true; }
end
| Add_row ->
let id = m.row_id in
{ m with row_id = m.row_id + 1;
rows = IM.add id (ROW.create 1) m.rows;
}
| Row_action (id, action) -> begin
match IM.find id m.rows with
| exception Not_found -> m
| row ->
let row =
ROW.update
(FRP.tag_port (fun a -> Row_action (id, a)) port)
row action
in
{ m with rows = IM.add id row m.rows }
end
| Submit ->
let sum = sprintf "%d" @@ IM.fold (fun _ { v; _ } s -> v + s) m.rows 0 in
ignore [%client (
let%lwt x = ~%dummy_serv ~%sum in
FRP.push ~%port (Submit_result x) ();
Lwt.return_unit
: unit Lwt.t)
];
{ m with submitting = true }
| Submit_result result -> { m with submitting = false; result = Some result }
let view (port : test_action FRP.Types.port) m =
let open Eliom_content.Html.D in
let input_a =
Raw.input
~a:[ R.a_value @@ S.map [%shared (fun m -> string_of_int m.a)] m;
R.a_style @@
S.map
[%shared
function
| { valid = false } -> "border: 2px solid red"
| _ -> "" ]
m;
]
() in
let () =
ignore [%client (FRP.on_keyups (fun s -> (Try_set s, None)) ~%input_a ~%port : unit)] in
let input_b =
Raw.input
~a:[ R.a_value @@ S.map [%shared (fun m -> sprintf "(%d)" m.a)] m ] () in
let rows =
R.div @@
Eliom_shared.ReactiveData.RList.from_signal @@
S.map [%shared fun rows -> List.rev @@ IM.fold (fun _ v l -> v :: l) rows []] @@
S.switch @@
S.map [%shared fun m -> IM.React.map ~eq:(FRP.shared_eq ()) (mk_row_view ~%port) m] @@
S.const @@
S.map ~eq:[%shared (==)] [%shared fun m -> m.rows] m
in
div
[
input_a; br ();
input_b; br ();
R.pcdata @@ S.map [%shared (fun m -> sprintf "value: %d" m.a)] m;
br ();
button ~a:[FRP.onclick port Decrement] [ pcdata "- " ];
pcdata " ";
button ~a:[FRP.onclick port Increment] [ pcdata "+ " ];
br ();
button
~a:[a_onclick [%client FRP.effect ~%port @@ FRP.focus ~%input_a]]
[ pcdata "Focus A" ];
pcdata " ";
button
~a:[a_onclick [%client FRP.effect ~%port @@ FRP.focus ~%input_b]]
[ pcdata "Focus B" ];
br();
button ~a:[FRP.onclick port Add_row] [ pcdata "ADD ROW" ];
pcdata " ";
pcdata "SUM: ";
R.pcdata @@
S.map [%shared string_of_int] @@
S.map [%shared fun m -> IM.fold (fun _ { v; _ } s -> v + s) m.rows 0] m;
br ();
button
~a:[FRP.onclick port Submit;
R.filter_attrib (a_disabled ()) @@
S.map [%shared fun m -> m.submitting] m
]
[ pcdata "SUBMIT" ];
R.pcdata @@
S.map
[%shared function | { submitting = true; _ } -> " SUBMITTING" | _ -> ""]
m;
R.pcdata @@
S.map
[%shared
function
| { result = None; _ } -> ""
| { result = Some s; _ } -> sprintf " result: %s" s]
m;
hr ();
rows;
]
end
]
module Testreactive_app =
Eliom_registration.App (
struct
let application_name = "eliom_shared_frp_POC"
let global_data_path = None end)
let main_service =
Eliom_service.create
~path:(Eliom_service.Path [])
~meth:(Eliom_service.Get Eliom_parameter.unit)
()
let () =
Testreactive_app.register ~service:main_service @@ fun () () ->
Lwt.return @@
Eliom_tools.F.html
~title:"eliom_shared_frp_POC"
~css:[["css";"eliom_shared_frp_POC.css"]]
Eliom_content.Html.F.(body [
h1 [pcdata "Welcome from Eliom's distillery!"];
div [ FRP.make [%client TEST.update] TEST.view (TEST.make 42) ]
])