Skip to content

Commit

Permalink
Apply review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
aleDsz committed May 23, 2024
1 parent b4ac5d6 commit c037a82
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
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())) ::
Supervisor.child_spec() | {module(), term()} | module()
def get_proxy_handler_child_spec(fun) do
with {:ok, reply} <- io_request({:livebook_get_proxy_handler_child_spec, fun}), do: reply
end

defp io_request(request) do
gl = Process.group_leader()
ref = Process.monitor(gl)
Expand Down
18 changes: 6 additions & 12 deletions lib/kino/proxy.ex
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
defmodule Kino.Proxy do
@moduledoc """
A kino for handling proxy requests from the host.
Functionality for handling proxy requests forwarded from Livebook.
## Examples
Kino.Proxy.listen(fn conn ->
conn
|> Plug.Conn.put_resp_header("content-type", "application/text;charset=utf-8")
|> Plug.Conn.send_resp(200, "used " <> conn.method <> " method")
end)
TODO: Write an extensive docs here.
"""

@doc """
Persists the function to be listened by the proxy handler.
"""
@spec listen(atom(), module(), (Plug.Conn.t() -> Plug.Conn.t())) ::
DynamicSupervisor.on_start_child()
def listen(name \\ __MODULE__, mod \\ Livebook.Proxy.Handler, fun) when is_function(fun, 1) do
Kino.start_child({mod, name: name, listen: fun})
@spec listen((Plug.Conn.t() -> Plug.Conn.t())) :: DynamicSupervisor.on_start_child()
def listen(fun) when is_function(fun, 1) do
child_spec = Kino.Bridge.get_proxy_handler_child_spec(fun)
Kino.start_child(child_spec)
end
end

0 comments on commit c037a82

Please sign in to comment.