-
Notifications
You must be signed in to change notification settings - Fork 2
/
mixvdomandeliom.eliom
115 lines (95 loc) · 3.1 KB
/
mixvdomandeliom.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
module Service = struct
let random_value =
Eliom_registration.Ocaml.create
(* having a path is better to avoid broken links if the user doesn't refresh the page after a server restart *)
~path:(Eliom_service.Path ["rnd"])
~meth:(Eliom_service.Get (Eliom_parameter.unit))
(fun () () ->
let i = Random.int 200 in
if i > 100 then
Lwt.return_ok(Printf.sprintf "Ok: random value %u > 100" i)
else
Lwt.return_error(Printf.sprintf "Error: random value %u <= 100" i)
)
end
[%%client
type 'msg Vdom.Cmd.t +=
| Service of { on_ok: (string -> 'msg); on_error: (string -> 'msg) }
let create_service ~on_ok ~on_error = Service { on_ok; on_error }
let run_service ~on_ok ~on_error () =
let%lwt v = Eliom_client.call_ocaml_service ~service:~%Service.random_value () () in
match v with
| Ok v -> on_ok v
| Error v -> on_error v
let cmd_handler ctx = function
| Service { on_ok; on_error } ->
let _ = run_service
~on_ok:(fun s -> Lwt.return(Vdom_blit.Cmd.send_msg ctx (on_ok s)))
~on_error:(fun s -> Lwt.return(Vdom_blit.Cmd.send_msg ctx (on_error s)))
()
in
true
let () = Vdom_blit.(register (cmd {Vdom_blit.Cmd.f = cmd_handler}))
type model = {
counter_ok : int ;
counter_error : int ;
message : string ;
}
type action =
| Call_service
| Service_called_ok of string
| Service_called_error of string
let update m = function
| Call_service -> (
Vdom.return m ~c:[create_service ~on_ok:(fun r -> Service_called_ok r) ~on_error:(fun r -> Service_called_error r)]
)
| Service_called_ok v -> Vdom.return { m with counter_ok = m.counter_ok + 1; message = v }
| Service_called_error v -> Vdom.return { m with counter_error = m.counter_error + 1; message = v }
let init = Vdom.return { counter_ok = 0; counter_error = 0; message = "No message" }
let button txt msg =
Vdom.(input [] ~a:[onclick msg; type_button; value txt])
let view m =
Vdom.(
div [
div [
button "Fetch a random value from server" Call_service
] ;
div [
text (Printf.sprintf "%u OK value(s)" m.counter_ok) ;
text " / " ;
text (Printf.sprintf "%u ERROR value(s)" m.counter_error) ;
] ;
div [
text m.message
] ;
]
)
let app = Vdom.app ~init ~view ~update ()
let run () =
Vdom_blit.run app
|> Vdom_blit.dom
|> Js_browser.Element.append_child (Js_browser.Document.body Js_browser.document)
let () = Js_browser.Window.set_onload Js_browser.window run
]
module Mixvdomandeliom_app =
Eliom_registration.App (
struct
let application_name = "mixvdomandeliom"
let global_data_path = None
end)
let main_service =
Eliom_service.create
~path:(Eliom_service.Path [])
~meth:(Eliom_service.Get Eliom_parameter.unit)
()
let () =
Mixvdomandeliom_app.register
~service:main_service
(fun () () ->
Lwt.return
(Eliom_tools.F.html
~title:"mixvdomandeliom"
~css:[["css";"mixvdomandeliom.css"]]
Eliom_content.Html.F.(body [
h1 [pcdata "Demo mix Vdom / Eliom"];
])))