Skip to content

Commit bf54ae6

Browse files
committed
WIPWIPWIP
1 parent 5f92d27 commit bf54ae6

11 files changed

+166
-225
lines changed

src/lib/eliom_bus.client.ml

Lines changed: 36 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -34,37 +34,37 @@ type ('a, 'b) t =
3434
; mutable max_size : int
3535
; write : 'a list -> unit
3636
; mutable waiter : unit -> unit
37-
; mutable last_wait : unit Promise.t
37+
; mutable last_wait : Switch.t option
3838
; mutable original_stream_available : bool
39-
; error_h : 'b option Promise.t * exn Lwt.u }
39+
; error_h : 'b option Promise.or_exn * (exn, exn) result Promise.u }
4040

4141
(* clone streams such that each clone of the original stream raise the same exceptions *)
4242
let consume (t, u) s =
43-
let t' =
44-
try Eliom_stream.iter (fun _ -> ()) s
43+
let p, w = Promise.create () in
44+
Eliom_lib.fork (fun () ->
45+
try Promise.resolve_ok w (Eliom_stream.iter (fun _ -> ()) s)
4546
with e ->
46-
(match Promise.peek t with None -> Lwt.wakeup_exn u e | _ -> ());
47-
raise e
48-
in
49-
Fiber.any
50-
(List.map
51-
(fun p () -> Promise.await p)
52-
(* TODO: ciao-lwt: The list [[ Lwt.bind t (fun _ -> Lwt.return_unit); t' ]] is expected to be a list of promises. Use [Fiber.fork_promise] to make a promise. *)
53-
[ (let _ = t in
54-
())
55-
; t' ])
47+
(match Promise.peek t with None -> Promise.resolve_error u e | _ -> ());
48+
Promise.resolve_error w e);
49+
Eliom_lib.fork (fun () ->
50+
try Promise.resolve_ok w (ignore (Promise.await_exn t))
51+
with e -> Promise.resolve_error w e);
52+
Promise.await_exn p
5653

5754
let clone_exn (t, u) s =
5855
let s' = Eliom_stream.clone s in
5956
Eliom_stream.from (fun () ->
6057
try
61-
Fiber.any
62-
(List.map
63-
(fun p () -> Promise.await p)
64-
(* TODO: ciao-lwt: The list [[ Eliom_stream.get s'; t ]] is expected to be a list of promises. Use [Fiber.fork_promise] to make a promise. *)
65-
[Eliom_stream.get s'; t])
58+
let p, w = Promise.create () in
59+
Eliom_lib.fork (fun () ->
60+
try Promise.resolve_ok w (Eliom_stream.get s')
61+
with e -> Promise.resolve_error w e);
62+
Eliom_lib.fork (fun () ->
63+
try Promise.resolve_ok w (Promise.await_exn t)
64+
with e -> Promise.resolve_error w e);
65+
Promise.await_exn p
6666
with e ->
67-
(match Promise.peek t with None -> Lwt.wakeup_exn u e | _ -> ());
67+
(match Promise.peek t with None -> Promise.resolve_error u e | _ -> ());
6868
raise e)
6969

7070
type ('a, 'att, 'co, 'ext, 'reg) callable_bus_service =
@@ -93,15 +93,16 @@ let create service channel waiter =
9393
with Eliom_request.Failed_request 204 -> ()
9494
in
9595
let error_h =
96-
let t, u =
96+
let t, (u : (exn, exn) result Promise.u) =
9797
Promise.create
9898
(* TODO: ciao-lwt: Translation is incomplete, [Promise.await] must be called on the promise when it's part of control-flow. *)
9999
()
100100
in
101+
let tt, uu = Promise.create () in
101102
( (try
102-
let _ = t in
103+
ignore (Promise.await t);
103104
assert false
104-
with e -> raise e)
105+
with e -> Promise.resolve_error uu e; tt)
105106
, u )
106107
in
107108
let stream =
@@ -118,7 +119,7 @@ let create service channel waiter =
118119
; max_size = 20
119120
; write
120121
; waiter
121-
; last_wait = ()
122+
; last_wait = None
122123
; original_stream_available = true
123124
; error_h }
124125
in
@@ -132,7 +133,7 @@ let create service channel waiter =
132133
t
133134

134135
let internal_unwrap ((wrapped_bus : ('a, 'b) Ecb.wrapped_bus), _unwrapper) =
135-
let waiter () = Js_of_ocaml_lwt.Lwt_js.sleep 0.05 in
136+
let waiter () = Js_of_ocaml_eio.Eio_js.sleep 0.05 in
136137
let channel, Eliom_comet_base.Bus_send_service service = wrapped_bus in
137138
create service channel waiter
138139

@@ -148,41 +149,30 @@ let original_stream t =
148149
raise_error ~section
149150
"original_stream: the original stream is not available anymore"
150151

151-
let stream t =
152-
let stream, push = Lwt_stream.create () in
153-
register t (fun data -> push data);
154-
stream
155-
156152
let flush t =
157153
let l = List.rev (Queue.fold (fun l v -> v :: l) [] t.queue) in
158154
Queue.clear t.queue; t.write l
159155

156+
exception Cancelled
157+
160158
let try_flush t =
161-
Lwt.cancel
162-
(* TODO: ciao-lwt: Use [Switch] or [Cancel] for defining a cancellable context. *)
163-
(* TODO: ciao-lwt: Use [Switch] or [Cancel] for defining a cancellable context. *)
164-
t.last_wait;
159+
Option.iter (fun o -> Switch.fail o Cancelled) t.last_wait;
165160
if Queue.length t.queue >= t.max_size
166161
then flush t
167162
else
168-
let th =
169-
Lwt.protected
170-
(* TODO: ciao-lwt: Use [Switch] or [Cancel] for defining a cancellable context. *)
171-
(* TODO: ciao-lwt: Use [Switch] or [Cancel] for defining a cancellable context. *)
172-
(t.waiter ())
173-
in
174-
t.last_wait <- th;
175-
let _ = th; flush t in
176-
()
163+
Eliom_lib.fork (fun () ->
164+
Switch.run_protected (fun sw ->
165+
(*VVV ???*)
166+
t.last_wait <- Some sw;
167+
t.waiter ());
168+
flush t)
177169

178170
let write t v = Queue.add v t.queue; try_flush t
179171
let close {channel; _} = Eliom_comet.close channel
180172
let set_queue_size b s = b.max_size <- s
181173

182174
let set_time_before_flush b t =
183175
b.waiter <-
184-
(if t <= 0.
185-
then fun x1 -> Fiber.yield x1
186-
else fun () -> Js_of_ocaml_lwt.Lwt_js.sleep t)
176+
(if t <= 0. then Fiber.yield else fun () -> Js_of_ocaml_eio.Eio_js.sleep t)
187177

188178
let force_link = ()

src/lib/eliom_bus.client.mli

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ val original_stream : ('a, 'b) t -> 'b Eliom_stream.t
3939
received. This function can be called only in the onload event
4040
handler, if called outside, it will raise a Failure. *)
4141

42-
val stream : ('a, 'b) t -> 'b Lwt_stream.t
43-
(** Create a new stream from the messages from the server. This has the same
44-
behavior as {!register}. *)
45-
4642
val write : ('a, 'b) t -> 'a -> unit
4743
(** [write b v] send [v] to the bus [b]. Every participant of the bus
4844
will receive [v], including the sender. *)

src/lib/eliom_client.client.ml

Lines changed: 63 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -432,11 +432,11 @@ let scroll_to_fragment ?offset fragment =
432432
let elem = Dom_html.document##(getElementById (Js.string fragment)) in
433433
Js.Opt.iter elem scroll_to_element)
434434

435-
let with_progress_cursor : 'a Promise.t -> 'a =
436-
fun t ->
435+
let with_progress_cursor : (unit -> 'a) -> 'a =
436+
fun f ->
437437
try
438438
Dom_html.document##.body##.style##.cursor := Js.string "progress";
439-
let res = t in
439+
let res = f () in
440440
Dom_html.document##.body##.style##.cursor := Js.string "auto";
441441
res
442442
with exn ->
@@ -698,7 +698,7 @@ let set_active_page p =
698698

699699
(* This key serves as a hook to access the page the currently running code is
700700
generating. *)
701-
let this_page : page Lwt.key = Fiber.create_key ()
701+
let this_page : page Fiber.key = Fiber.create_key ()
702702

703703
let get_this_page () =
704704
match Fiber.get this_page with
@@ -1905,8 +1905,8 @@ and change_page :
19051905
~service ?hostname ?port ?fragment ?keep_nl_params ~nl_params
19061906
?keep_get_na_params get_params post_params
19071907
else
1908-
with_progress_cursor
1909-
(match xhr with
1908+
with_progress_cursor (fun () ->
1909+
match xhr with
19101910
| Some (Some tmpl as t)
19111911
when t = Eliom_request_info.get_request_template () ->
19121912
Logs.debug ~src:section_page (fun fmt ->
@@ -2042,29 +2042,28 @@ and reload_without_na_params ~replace ~uri ~fallback =
20422042
(* Function used in "onclick" event handler of <a>. *)
20432043
let change_page_uri_a ?cookies_info ?tmpl ?(get_params = []) full_uri =
20442044
Logs.debug ~src:section_page (fun fmt -> fmt "Change page uri");
2045-
with_progress_cursor
2046-
(let uri, fragment = Url.split_fragment full_uri in
2047-
if uri <> get_current_uri () || fragment = None
2048-
then (
2049-
if is_client_app ()
2050-
then failwith "Change_page_uri_a called on client app";
2051-
match tmpl with
2052-
| Some t when tmpl = Eliom_request_info.get_request_template () ->
2053-
let uri, content =
2054-
Eliom_request.http_get ?cookies_info uri
2055-
((Eliom_request.nl_template_string, t) :: get_params)
2056-
Eliom_request.string_result
2057-
in
2058-
set_template_content ~replace:false ~uri ?fragment content
2059-
| _ ->
2060-
let uri, content =
2061-
Eliom_request.http_get ~expecting_process_page:true ?cookies_info
2062-
uri get_params Eliom_request.xml_result
2063-
in
2064-
set_content ~replace:false ~uri ?fragment content)
2065-
else (
2066-
change_url_string ~replace:true full_uri;
2067-
scroll_to_fragment fragment))
2045+
with_progress_cursor (fun () ->
2046+
let uri, fragment = Url.split_fragment full_uri in
2047+
if uri <> get_current_uri () || fragment = None
2048+
then (
2049+
if is_client_app () then failwith "Change_page_uri_a called on client app";
2050+
match tmpl with
2051+
| Some t when tmpl = Eliom_request_info.get_request_template () ->
2052+
let uri, content =
2053+
Eliom_request.http_get ?cookies_info uri
2054+
((Eliom_request.nl_template_string, t) :: get_params)
2055+
Eliom_request.string_result
2056+
in
2057+
set_template_content ~replace:false ~uri ?fragment content
2058+
| _ ->
2059+
let uri, content =
2060+
Eliom_request.http_get ~expecting_process_page:true ?cookies_info
2061+
uri get_params Eliom_request.xml_result
2062+
in
2063+
set_content ~replace:false ~uri ?fragment content)
2064+
else (
2065+
change_url_string ~replace:true full_uri;
2066+
scroll_to_fragment fragment))
20682067

20692068
let change_page_uri ?replace full_uri =
20702069
Logs.debug ~src:section_page (fun fmt -> fmt "Change page uri");
@@ -2088,42 +2087,42 @@ let change_page_uri ?replace full_uri =
20882087
(* Functions used in "onsubmit" event handler of <form>. *)
20892088

20902089
let change_page_get_form ?cookies_info ?tmpl form full_uri =
2091-
with_progress_cursor
2092-
(let form = Js.Unsafe.coerce form in
2093-
let uri, fragment = Url.split_fragment full_uri in
2094-
match tmpl with
2095-
| Some t when tmpl = Eliom_request_info.get_request_template () ->
2096-
let uri, content =
2097-
Eliom_request.send_get_form
2098-
~get_args:[Eliom_request.nl_template_string, t]
2099-
?cookies_info form uri Eliom_request.string_result
2100-
in
2101-
set_template_content ~replace:false ~uri ?fragment content
2102-
| _ ->
2103-
let uri, content =
2104-
Eliom_request.send_get_form ~expecting_process_page:true
2105-
?cookies_info form uri Eliom_request.xml_result
2106-
in
2107-
set_content ~replace:false ~uri ?fragment content)
2090+
with_progress_cursor (fun () ->
2091+
let form = Js.Unsafe.coerce form in
2092+
let uri, fragment = Url.split_fragment full_uri in
2093+
match tmpl with
2094+
| Some t when tmpl = Eliom_request_info.get_request_template () ->
2095+
let uri, content =
2096+
Eliom_request.send_get_form
2097+
~get_args:[Eliom_request.nl_template_string, t]
2098+
?cookies_info form uri Eliom_request.string_result
2099+
in
2100+
set_template_content ~replace:false ~uri ?fragment content
2101+
| _ ->
2102+
let uri, content =
2103+
Eliom_request.send_get_form ~expecting_process_page:true ?cookies_info
2104+
form uri Eliom_request.xml_result
2105+
in
2106+
set_content ~replace:false ~uri ?fragment content)
21082107

21092108
let change_page_post_form ?cookies_info ?tmpl form full_uri =
2110-
with_progress_cursor
2111-
(let form = Js.Unsafe.coerce form in
2112-
let uri, fragment = Url.split_fragment full_uri in
2113-
match tmpl with
2114-
| Some t when tmpl = Eliom_request_info.get_request_template () ->
2115-
let uri, content =
2116-
Eliom_request.send_post_form
2117-
~get_args:[Eliom_request.nl_template_string, t]
2118-
?cookies_info form uri Eliom_request.string_result
2119-
in
2120-
set_template_content ~replace:false ~uri ?fragment content
2121-
| _ ->
2122-
let uri, content =
2123-
Eliom_request.send_post_form ~expecting_process_page:true
2124-
?cookies_info form uri Eliom_request.xml_result
2125-
in
2126-
set_content ~replace:false ~uri ?fragment content)
2109+
with_progress_cursor (fun () ->
2110+
let form = Js.Unsafe.coerce form in
2111+
let uri, fragment = Url.split_fragment full_uri in
2112+
match tmpl with
2113+
| Some t when tmpl = Eliom_request_info.get_request_template () ->
2114+
let uri, content =
2115+
Eliom_request.send_post_form
2116+
~get_args:[Eliom_request.nl_template_string, t]
2117+
?cookies_info form uri Eliom_request.string_result
2118+
in
2119+
set_template_content ~replace:false ~uri ?fragment content
2120+
| _ ->
2121+
let uri, content =
2122+
Eliom_request.send_post_form ~expecting_process_page:true
2123+
?cookies_info form uri Eliom_request.xml_result
2124+
in
2125+
set_content ~replace:false ~uri ?fragment content)
21272126

21282127
let _ =
21292128
(Eliom_client_core.change_page_uri_ :=
@@ -2189,7 +2188,7 @@ let () =
21892188
Js_of_ocaml_eio.Eio_js.start (fun () ->
21902189
with_progress_cursor
21912190
(* TODO: ciao-lwt: This computation might not be suspended correctly. *)
2192-
@@
2191+
@@ fun () ->
21932192
let uri, fragment = Url.split_fragment full_uri in
21942193
if uri = get_current_uri ()
21952194
then (

src/lib/eliom_client_core.client.ml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,14 +273,10 @@ let in_onload, broadcast_load_end, wait_load_end, set_loading_phase =
273273
let in_onload () = !loading_phase in
274274
let broadcast_load_end () =
275275
loading_phase := false;
276-
Lwt_condition.broadcast load_end ()
276+
Eio.Condition.broadcast load_end
277277
in
278278
let wait_load_end () =
279-
if !loading_phase
280-
then
281-
Eio.Condition.await (* TODO: ciao-lwt: A mutex must be passed *) load_end
282-
__mutex__
283-
else ()
279+
if !loading_phase then Eio.Condition.await_no_mutex load_end else ()
284280
in
285281
in_onload, broadcast_load_end, wait_load_end, set
286282

0 commit comments

Comments
 (0)