-
-
Notifications
You must be signed in to change notification settings - Fork 12
Improve the Fiber and Model stream context #312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
17f4e18
to
0eb4aea
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job
"1:[\"$\",\"span\",null,{\"children\":\"hi\"},null,[],{}]\n"; | ||
"2:[\"$\",\"span\",null,{\"children\":\"hola\"},null,[],{}]\n"; | ||
"0:[\"$1\",\"$2\"]\n"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are these changes "on purpose"?
"2:D\"$1\"\n"; | ||
"0:[\"$\",\"input\",null,{\"id\":\"sidebar-search-input\",\"placeholder\":\"Search\",\"value\":\"my \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whoa, this was a bug?
let push_async promise ~context = | ||
let index = context.index in | ||
context.index <- context.index + 1; | ||
context.pending <- context.pending + 1; | ||
Lwt.async (fun () -> | ||
let%lwt chunk = promise in | ||
context.pending <- context.pending - 1; | ||
context.push (chunk index); | ||
if context.pending = 0 then context.close (); | ||
Lwt.return ()); | ||
index |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like a decent abstraction, but caused some tests to behave differently. (since we pushed a few things instead of push once)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just solved some of those issues. I'm going to look better next week at the test cases.
|
||
module Model = struct | ||
type chunk_type = Chunk_value of json | Chunk_component_ref of json | Debug_info_map of json | Chunk_error of json | ||
type t = Value of json | Debug_info_map of json | Component_ref of json | Error of env * React.error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Chunk_value and Chunk_component_ref had different serialisation right? How come we join them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Never mind the diff confused me. Chunk_value -> Value looks... interesting?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You know, I was wondering if Json is better. Like, "push Json value".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Model.chunk is good type name btw, if you go with chunk, then the type make sense: value | component_ref | debug_info
context.push id (Chunk_value (`Assoc [ ("id", `String f.id); ("bound", `Null) ])); | ||
React.JSX.String (key, key, action_value id) | ||
let index = | ||
Stream.push ~context (on_push (Value (`Assoc [ ("id", `String f.id); ("bound", `Null) ]))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Stream.push ~context onpush?
shouldn't be just "on_push"?
| Chunk_component_ref json -> push (client_reference_to_chunk id json) | ||
| Chunk_error json -> push (error_to_chunk id json) | ||
module Fiber = struct | ||
type chunk = Model of Model.t | Suspense_Boundary of Html.element | Batch of chunk list |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how's possible that chunk contains Suspense_boundary (boundary could be lowercase) but context can stream Html.element?
This looks like an extra diff from the refactor?
match chunk with | ||
| Model model -> model_to_chunk model id | ||
| Suspense_Boundary str -> boundary_to_chunk id str | ||
| Batch chunks -> chunks |> List.map (fun chunk -> to_chunk chunk id) |> Html.list |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When Batch is needed?
let%lwt root_html, root_model = render_element_to_html ~fiber element in | ||
let root_chunk = client_value_chunk_script initial_index root_model in | ||
context.pending <- context.pending - 1; | ||
let%lwt root_html, root_model = Fiber.render_element_to_html ~fiber ~on_push:Fiber.to_chunk element in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we send fiber, why send on_push as a labelled arg?
in | ||
let stream, push, close = Push_stream.make () in | ||
let context : Fiber.context = { push; close; pending = 1; index = initial_index; env } in | ||
let stream, context = Stream.make ~env ~debug:false ~initial_index:1 in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the initial index starts at 1?
| Debug_info_map json -> push (debug_info_to_chunk id json) | ||
| Chunk_component_ref json -> push (client_reference_to_chunk id json) | ||
| Chunk_error json -> push (error_to_chunk id json) | ||
module Fiber = struct |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fiber got a lot of line movements that makes it a bit hard to review the diff and I only see one commit, could we maybe go step by step in person?
8c150ae
to
12df513
Compare
12df513
to
e3adf79
Compare
Description
This PR refactors the way we handle the context for Model and Fiber. Instead of 2 different contexts for the Stream, we will have the same context type for
both
.The context controls async and sync push as the index and pending, so we no longer need to worry about it. We declare the value to push and use
push/push_async
, and the context will handle the rest:The concept of inversion of control was applied at
render_element_to_html and
client_values_to_json. Then we can use the
client_values_to_jsonat
render_element_to_html, which reduces the amount of code to maintain, also keeping it way simpler to understand: