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

Add Kino.Proxy module #431

Merged
merged 10 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 9 additions & 0 deletions lib/kino/bridge.ex
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,15 @@ defmodule Kino.Bridge do
match?({:ok, _}, io_request(:livebook_get_evaluation_file))
end

@doc """
Requests the child spec for proxy handler with the given function.
"""
@spec get_proxy_handler_child_spec((Plug.Conn.t() -> Plug.Conn.t())) ::
{:ok, {module(), term()}} | request_error()
def get_proxy_handler_child_spec(fun) do
io_request({:livebook_get_proxy_handler_child_spec, fun})
end

defp io_request(request) do
gl = Process.group_leader()
ref = Process.monitor(gl)
Expand Down
21 changes: 21 additions & 0 deletions lib/kino/proxy.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule Kino.Proxy do
@moduledoc """
Functionality for handling proxy requests forwarded from Livebook.

TODO: Write an extensive docs here.
"""

@doc """
Persists the function to be listened by the proxy handler.
"""
@spec listen((Plug.Conn.t() -> Plug.Conn.t())) :: DynamicSupervisor.on_start_child()
def listen(fun) when is_function(fun, 1) do
case Kino.Bridge.get_proxy_handler_child_spec(fun) do
{:ok, child_spec} ->
Kino.start_child(child_spec)

{:request_error, reason} ->
raise "failed to access the proxy handler child spec, reason: #{inspect(reason)}"
end
end
end
Loading