Skip to content
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

Update docs on nested Kino.start_child/1 #452

Merged
merged 3 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/kino.ex
Original file line number Diff line number Diff line change
Expand Up @@ -428,9 +428,11 @@ defmodule Kino do
> or `c:Kino.JS.Live.init/2`. If you do that, starting the process
> will block forever.
>
> Note that creating many kinos uses `start_child/1` underneath,
> hence the same restriction applies to starting those. See
> `c:Kino.JS.Live.init/2` for more details.
> On creation, many kinos use `start_child/1` underneath, which means
> that you cannot use functions such as `Kino.DataTable.new/1` in
> `c:GenServer.init/1`. If you need to do that, you must either
> create the kinos beforehand and pass in the `GenServer` argument,
> or create them in `c:GenServer.handle_continue/2`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhh, didn't know it was possible to do that inside handle_continue ! 💡

"""
@spec start_child(
Supervisor.child_spec()
Expand Down
40 changes: 2 additions & 38 deletions lib/kino/js/live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -202,44 +202,8 @@ defmodule Kino.JS.Live do

See `c:GenServer.init/1` for more details.

> #### Starting other kinos {: .warning}
>
> It is generally not possible to start kinos inside the `c:init/2`
> callback, as such operation would block forever. In case you need
> to start other kinos during initialization, you must start them
> beforehand and pass as an argument to `c:init/2`. So instead of
>
> defmodule KinoDocs.Custom do
> def new() do
> Kino.JS.Live.new(__MODULE__, {})
> end
>
> @impl true
> def init({}, ctx) do
> frame = Kino.Frame.new()
> {:ok, assign(ctx, frame: frame)}
Comment on lines -219 to -220
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example is confusing, we create a frame in Kino.JS.Live, but there is no obvious way how we would use that frame (since Kino.JS.Live itself is already about managing JS rendering). If we want a frame to render updates into, as we do in the dbg pipeline, it is actually more natural to create the frame upfront and pass as an argument.

I think it's more about creating kinos like Kino.DataTable.new() and rendering into a provided frame, and it's also more about GenServer, because it's a rare case for Kino.JS.Live.

> end
>
> ...
> end
>
> do the following
>
> defmodule KinoDocs.Custom do
> def new() do
> frame = Kino.Frame.new()
> Kino.JS.Live.new(__MODULE__, {frame})
> end
>
> @impl true
> def init({frame}, ctx) do
> {:ok, assign(ctx, frame: frame)}
> end
>
> ...
> end
>
> Also see `Kino.start_child/1`.
If you want to create other kinos on initialization, see the
limitations described in `Kino.start_child/1`.
"""
@callback init(arg :: term(), ctx :: Context.t()) ::
{:ok, ctx :: Context.t()} | {:ok, ctx :: Context.t(), opts :: keyword()}
Expand Down
Loading