Skip to content

Commit 94a9dde

Browse files
committed
chore: add context to turn_element_into_payload
1 parent 6383f02 commit 94a9dde

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

packages/reactDom/src/ReactServerDOM.ml

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ module Model = struct
248248
let rec element_to_payload ~context ?(debug = false) ~to_chunk ~env element =
249249
let is_root = ref true in
250250

251-
let rec turn_element_into_payload element =
251+
let rec turn_element_into_payload ~context element =
252252
match (element : React.element) with
253253
| Empty -> `Null
254254
| DangerouslyInnerHtml _ ->
@@ -272,18 +272,18 @@ module Model = struct
272272
attributes
273273
in
274274
let props = props_to_json attributes in
275-
node ~key ~tag ~props (List.map turn_element_into_payload children)
275+
node ~key ~tag ~props (List.map (turn_element_into_payload ~context) children)
276276
| Fragment children ->
277277
if is_root.contents then is_root := false;
278-
turn_element_into_payload children
278+
turn_element_into_payload ~context children
279279
| List children ->
280280
if is_root.contents then is_root := false;
281-
`List (List.map turn_element_into_payload children)
281+
`List (List.map (turn_element_into_payload ~context) children)
282282
| Array children ->
283283
if is_root.contents then is_root := false;
284-
`List (Array.map turn_element_into_payload children |> Array.to_list)
285-
(* TODO: Get the stack info from component as well *)
284+
`List (Array.map (turn_element_into_payload ~context) children |> Array.to_list)
286285
| Upper_case_component (name, component) -> (
286+
(* TODO: Get the stack info from component as well *)
287287
match component () with
288288
| element ->
289289
(* TODO: Can we remove the is_root difference. It currently align with react.js behavior, but it's not clear what is the purpose of it *)
@@ -294,13 +294,13 @@ module Model = struct
294294
Root is a special case: https://github.com/facebook/react/blob/f3a803617ec4ba9d14bf5205ffece28ed1496a1d/packages/react-server/src/ReactFlightServer.js#L756-L766
295295
*)
296296
if debug then push_debug_info ~context ~to_chunk ~env ~index:0 ~ownerName:name else ();
297-
turn_element_into_payload element)
297+
turn_element_into_payload ~context element)
298298
else
299299
(* If it's not the root React push the element to the stream and return the reference value *)
300300
let element_index =
301301
Stream.push ~context (fun index ->
302302
if debug then push_debug_info ~context ~to_chunk ~env ~index ~ownerName:name else ();
303-
let payload = turn_element_into_payload element in
303+
let payload = turn_element_into_payload ~context element in
304304
to_chunk (Value payload) index)
305305
in
306306
`String (ref_value element_index)
@@ -318,12 +318,12 @@ module Model = struct
318318
let error = make_error ~message ~stack ~digest:"" in
319319
let index = Stream.push ~context (to_chunk (Error (env, error))) in
320320
`String (lazy_value index)
321-
| Return element -> turn_element_into_payload element
321+
| Return element -> turn_element_into_payload ~context element
322322
| Sleep ->
323323
let promise =
324324
try%lwt
325325
let%lwt element = promise in
326-
Lwt.return (to_chunk (Value (turn_element_into_payload element)))
326+
Lwt.return (to_chunk (Value (turn_element_into_payload ~context element)))
327327
with exn ->
328328
let message = Printexc.to_string exn in
329329
let stack = create_stack_trace () in
@@ -335,18 +335,18 @@ module Model = struct
335335
| Suspense { key; children; fallback } ->
336336
(* TODO: Need to check is_root? *)
337337
(* TODO: Maybe we need to push suspense index and suspense node separately *)
338-
let fallback = turn_element_into_payload fallback in
339-
suspense_node ~key ~fallback [ turn_element_into_payload children ]
338+
let fallback = turn_element_into_payload ~context fallback in
339+
suspense_node ~key ~fallback [ turn_element_into_payload ~context children ]
340340
| Client_component { import_module; import_name; props; client = _ } ->
341341
let ref = component_ref ~module_:import_module ~name:import_name in
342342
let index = Stream.push ~context (to_chunk (Component_ref ref)) in
343343
let client_props = client_values_to_json ~context ~to_chunk ~env props in
344344
node ~tag:(ref_value index) ~props:client_props []
345345
(* TODO: Do we need to do anything with Provider and Consumer? *)
346-
| Provider children -> turn_element_into_payload children
347-
| Consumer children -> turn_element_into_payload children
346+
| Provider children -> turn_element_into_payload ~context children
347+
| Consumer children -> turn_element_into_payload ~context children
348348
in
349-
turn_element_into_payload element
349+
turn_element_into_payload ~context element
350350

351351
and client_value_to_json ~context ?debug ~to_chunk ~env value =
352352
match (value : React.client_value) with
@@ -355,9 +355,8 @@ module Model = struct
355355
let index = Stream.push ~context (to_chunk (Error (env, error))) in
356356
`String (error_value index)
357357
| Element element ->
358-
let index =
359-
Stream.push ~context (to_chunk (Value (element_to_payload ~context ?debug ~to_chunk ~env element)))
360-
in
358+
let payload = element_to_payload ~context ?debug ~to_chunk ~env element in
359+
let index = Stream.push ~context (to_chunk (Value payload)) in
361360
`String (ref_value index)
362361
| Promise (promise, value_to_json) -> (
363362
match Lwt.state promise with

packages/reactDom/test/test_RSC_model.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -714,16 +714,17 @@ let env_development_adds_debug_info () =
714714
React.Upper_case_component
715715
( "app",
716716
fun () ->
717+
let value = "my friend" in
717718
React.createElement "input"
718719
[
719720
React.JSX.String ("id", "id", "sidebar-search-input");
720721
React.JSX.String ("placeholder", "placeholder", "Search");
721-
React.JSX.String ("value", "value", "my friend");
722+
React.JSX.String ("value", "value", value);
722723
]
723724
[] )
724725
in
725726
let output, subscribe = capture_stream () in
726-
let%lwt () = ReactServerDOM.render_model ~debug:true ~subscribe app in
727+
let%lwt () = ReactServerDOM.render_model ~subscribe ~debug:true app in
727728
assert_list_of_strings !output
728729
[
729730
"1:{\"name\":\"app\",\"env\":\"Server\",\"key\":null,\"owner\":null,\"stack\":[],\"props\":{}}\n";

0 commit comments

Comments
 (0)