Skip to content

Commit

Permalink
It works, wat
Browse files Browse the repository at this point in the history
  • Loading branch information
davesnx committed Aug 1, 2023
1 parent a5c99a2 commit 969fdeb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
32 changes: 15 additions & 17 deletions packages/reactDom/src/reactDOM.ml
Original file line number Diff line number Diff line change
Expand Up @@ -176,23 +176,21 @@ let render_to_stream ~context_state element =
arr |> Array.to_list |> List.map render_inner |> String.concat ""
| Upper_case_component component -> (
print_endline "Upper_case_component";
let element =
try Some (component ()) with
| React.Suspend (Any_promise promise) ->
print_endline "| React.Suspend (Any_promise promise) ->";
context_state.waiting <- context_state.waiting + 1;
Lwt.map
(fun _ ->
context_state.push (render_inner element);
context_state.waiting <- context_state.waiting - 1;
if context_state.waiting = 0 then context_state.close ()
else ())
promise
|> Lwt.ignore_result;
None
| e -> raise e
in
match element with Some element -> render_inner element | None -> "")
match component () with
| element -> render_inner element
| exception React.Suspend (Any_promise promise) ->
print_endline "| React.Suspend (Any_promise promise) ->";
context_state.waiting <- context_state.waiting + 1;
Lwt.map
(fun _ ->
context_state.push (render_inner element);
context_state.waiting <- context_state.waiting - 1;
if context_state.waiting = 0 then context_state.close () else ())
promise
|> Lwt.ignore_result;
(* TODO: What should it return in this case? *)
""
| exception exn -> raise exn)
| Lower_case_element { tag; attributes; _ }
when Html.is_self_closing_tag tag ->
Printf.sprintf "<%s%s />" tag (attributes_to_string tag attributes)
Expand Down
15 changes: 13 additions & 2 deletions packages/reactDom/test/test_renderToLwtStream.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,19 @@ let assert_string left right =
let assert_list ty left right =
Alcotest.check (Alcotest.list ty) "should be equal" right left

module Sleep = struct
let cached = ref false

let delay v =
if cached.contents then Lwt.return ()
else
let open Lwt.Infix in
cached.contents <- true;
Lwt_unix.sleep v >>= fun () -> Lwt.return ()
end

let make ~delay =
let () = React.use (Lwt_unix.sleep delay) in
let () = React.use (Sleep.delay delay) in
React.createElement "div" [||]
[
React.createElement "span" [||]
Expand All @@ -28,7 +39,7 @@ let test_silly_stream _switch () : unit Lwt.t =
let suspense_one _switch () : unit Lwt.t =
let timer = React.Upper_case_component (fun () -> make ~delay:0.1) in
let stream, _abort = ReactDOM.renderToLwtStream timer in
assert_stream stream [ "Hello"; "1." ]
assert_stream stream [ ""; "<div><span>Hello0.1</span></div>" ]

let case title fn = Alcotest_lwt.test_case title `Quick fn

Expand Down

0 comments on commit 969fdeb

Please sign in to comment.