Skip to content

Commit

Permalink
Fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
smondet committed Aug 24, 2023
1 parent 1f4f297 commit d2a8c9a
Show file tree
Hide file tree
Showing 25 changed files with 3,393 additions and 2,635 deletions.
6 changes: 3 additions & 3 deletions .ocamlformat
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
version=0.19.0
profile=compact
break-collection-expressions=fit-or-vertical
version=0.24.1
profile=default
exp-grouping=preserve
parse-docstrings
sequence-blank-line=compact
6 changes: 3 additions & 3 deletions src/client/.ocamlformat
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
version=0.19.0
profile=compact
break-collection-expressions=fit-or-vertical
version=0.24.1
profile=default
exp-grouping=preserve
parse-docstrings
sequence-blank-line=compact
103 changes: 56 additions & 47 deletions src/client/async_work.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,45 @@ type log_item = Html_types.div_content_fun Meta_html.H5.elt
type status = Empty | Work_in_progress | Done
type 'a content = ('a, log_item) Result.t list

type 'a t =
{ logs: log_item Reactive.Table.t
; status: status Reactive.var
; id: int
; content: 'a content Reactive.var }
type 'a t = {
logs : log_item Reactive.Table.t;
status : status Reactive.var;
id : int;
content : 'a content Reactive.var;
}

let _id = ref 0

let empty () =
let id = !_id in
Caml.incr _id ;
{ logs= Reactive.Table.make ()
; status= Reactive.var Empty
; id
; content= Reactive.var [] }
Caml.incr _id;
{
logs = Reactive.Table.make ();
status = Reactive.var Empty;
id;
content = Reactive.var [];
}

let logs_div_id t = Fmt.str "logs-of-async-work-%d" t.id

let reinit s =
Reactive.Table.clear s.logs ;
Reactive.set s.content [] ;
Reactive.Table.clear s.logs;
Reactive.set s.content [];
Reactive.set s.status Empty

let log t item =
Reactive.Table.append' t.logs item ;
Reactive.Table.append' t.logs item;
Lwt.async
Lwt.Infix.(
fun () ->
Js_of_ocaml.(
Js_of_ocaml_lwt.Lwt_js.sleep 0.1
>>= fun () ->
Js_of_ocaml_lwt.Lwt_js.sleep 0.1 >>= fun () ->
let divid = logs_div_id t in
dbgf "Trying to scroll down %s" divid ;
( match Dom_html.getElementById_opt divid with
dbgf "Trying to scroll down %s" divid;
(match Dom_html.getElementById_opt divid with
| Some e -> e##.scrollTop := 100000
| None -> dbgf "Cannot find: %s" divid ) ;
Lwt.return_unit)) ;
| None -> dbgf "Cannot find: %s" divid);
Lwt.return_unit));
()

let wip t = Reactive.set t.status Work_in_progress
Expand All @@ -50,41 +52,43 @@ let wip_add_error t err =
Reactive.set t.content (Error err :: Reactive.peek t.content)

let ok t o =
Reactive.set t.status Done ;
Reactive.set t.content [Ok o]
Reactive.set t.status Done;
Reactive.set t.content [ Ok o ]

let error t o =
Reactive.set t.status Done ;
Reactive.set t.content [Error o]
Reactive.set t.status Done;
Reactive.set t.content [ Error o ]

let finish t = Reactive.set t.status Done

let busy {status; _} =
let busy { status; _ } =
Reactive.(
get status |> map ~f:(function Work_in_progress -> true | _ -> false))

let peek_busy {status; _} =
let peek_busy { status; _ } =
Reactive.(peek status |> function Work_in_progress -> true | _ -> false)

let is_empty {status; _} =
let is_empty { status; _ } =
Reactive.(get status |> map ~f:(function Empty -> true | _ -> false))

let async_catch :
'a t
-> exn_to_html:(exn -> log_item)
-> (mkexn:(log_item -> exn) -> unit -> unit Lwt.t)
-> unit =
'a t ->
exn_to_html:(exn -> log_item) ->
(mkexn:(log_item -> exn) -> unit -> unit Lwt.t) ->
unit =
fun wip ~exn_to_html f ->
let open Lwt in
let exception Work_failed of log_item in
async (fun () ->
catch
(fun () -> f ~mkexn:(fun x -> Work_failed x) ())
(function
| Work_failed l -> error wip l ; return ()
| Work_failed l ->
error wip l;
return ()
| exn ->
error wip (exn_to_html exn) ;
return () ) )
error wip (exn_to_html exn);
return ()))

let default_show_error e =
let open Meta_html in
Expand All @@ -94,37 +98,42 @@ let render ?(done_empty = Meta_html.empty) ?(show_error = default_show_error)
work_status ~f =
let open Meta_html in
let show_logs ?(wip = false) () =
let make_logs_map _ x = H5.li [x] in
let make_logs_map _ x = H5.li [ x ] in
let logs = Reactive.Table.concat_map ~map:make_logs_map work_status.logs in
div
~a:
[ H5.a_style (Lwd.pure "max-height: 20em; overflow: auto")
; H5.a_id (Lwd.pure (logs_div_id work_status)) ]
[
H5.a_style (Lwd.pure "max-height: 20em; overflow: auto");
H5.a_id (Lwd.pure (logs_div_id work_status));
]
(Bootstrap.terminal_logs
(H5.ul
( if wip then
[logs; H5.li [Bootstrap.spinner ~kind:`Info (t "Working …")]]
else [logs] ) ) ) in
(if wip then
[ logs; H5.li [ Bootstrap.spinner ~kind:`Info (t "Working …") ] ]
else [ logs ])))
in
let collapsing_logs () =
let collapse = Bootstrap.Collapse.make () in
Bootstrap.Collapse.fixed_width_reactive_button_with_div_below collapse
~width:"12em" ~kind:`Secondary
~button:(function true -> t "Show Logs" | false -> t "Collapse Logs")
(fun () -> show_logs ~wip:false ()) in
(fun () -> show_logs ~wip:false ())
in
let content ~wip =
Reactive.bind_var work_status.content ~f:(function
| [] -> if wip then empty () else done_empty ()
| l ->
( if wip then
div
( it "Work in progress …"
%% Bootstrap.spinner ~kind:`Info (t "Working …") )
else empty () )
(if wip then
div
(it "Work in progress …"
%% Bootstrap.spinner ~kind:`Info (t "Working …"))
else empty ())
% list
(List.rev_map l ~f:(function
| Ok o -> div (f o)
| Error e -> show_error e ) ) ) in
| Error e -> show_error e)))
in
Reactive.bind_var work_status.status ~f:(function
| Empty -> empty ()
| Work_in_progress -> content ~wip:true %% show_logs ~wip:true ()
| Done -> content ~wip:false %% collapsing_logs () )
| Done -> content ~wip:false %% collapsing_logs ())
Loading

0 comments on commit d2a8c9a

Please sign in to comment.