2021-05-10 20:37:38 +08:00
|
|
|
defmodule LivebookWeb.SessionLive.EmbeddedLive do
|
|
|
|
use LivebookWeb, :live_view
|
|
|
|
|
|
|
|
alias Livebook.{Session, Runtime}
|
|
|
|
|
|
|
|
@impl true
|
2021-10-29 01:41:07 +08:00
|
|
|
def mount(_params, %{"session" => session, "current_runtime" => current_runtime}, socket) do
|
2022-04-04 18:20:36 +08:00
|
|
|
unless Livebook.Config.runtime_enabled?(Livebook.Runtime.Embedded) do
|
|
|
|
raise "runtime module not allowed"
|
|
|
|
end
|
|
|
|
|
2021-10-29 01:41:07 +08:00
|
|
|
if connected?(socket) do
|
2022-04-19 17:29:38 +08:00
|
|
|
Session.subscribe(session.id)
|
2021-10-29 01:41:07 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
{:ok, assign(socket, session: session, current_runtime: current_runtime)}
|
2021-05-10 20:37:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def render(assigns) do
|
2021-07-07 20:32:49 +08:00
|
|
|
~H"""
|
2021-05-10 20:37:38 +08:00
|
|
|
<div class="flex-col space-y-5">
|
|
|
|
<p class="text-gray-700">
|
|
|
|
Run the notebook code within the Livebook node itself.
|
|
|
|
This is reserved for specific cases where there is no option
|
|
|
|
of starting a separate Elixir runtime (for example, on embedded
|
|
|
|
devices or cases where the amount of memory available is
|
|
|
|
limited). Prefer the "Elixir standalone" runtime whenever possible.
|
|
|
|
</p>
|
|
|
|
<p class="text-gray-700">
|
|
|
|
<span class="font-semibold">Warning:</span>
|
|
|
|
any module that you define will be defined globally until
|
|
|
|
you restart Livebook. Furthermore, code in one notebook
|
|
|
|
may interfere with code from another notebook.
|
|
|
|
</p>
|
2021-12-04 04:57:21 +08:00
|
|
|
<button class="button-base button-blue" phx-click="init">
|
2021-10-29 01:41:07 +08:00
|
|
|
<%= if(matching_runtime?(@current_runtime), do: "Reconnect", else: "Connect") %>
|
2021-05-10 20:37:38 +08:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
2021-10-29 01:41:07 +08:00
|
|
|
defp matching_runtime?(%Runtime.Embedded{}), do: true
|
|
|
|
defp matching_runtime?(_runtime), do: false
|
|
|
|
|
2021-05-10 20:37:38 +08:00
|
|
|
@impl true
|
|
|
|
def handle_event("init", _params, socket) do
|
2022-04-02 02:13:37 +08:00
|
|
|
{:ok, runtime} = Runtime.Embedded.new() |> Runtime.connect()
|
|
|
|
Session.set_runtime(socket.assigns.session.pid, runtime)
|
2021-05-10 20:37:38 +08:00
|
|
|
{:noreply, socket}
|
|
|
|
end
|
2021-10-29 01:41:07 +08:00
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_info({:operation, {:set_runtime, _pid, runtime}}, socket) do
|
|
|
|
{:noreply, assign(socket, current_runtime: runtime)}
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_info(_message, socket), do: {:noreply, socket}
|
2021-05-10 20:37:38 +08:00
|
|
|
end
|