2021-03-04 05:56:28 +08:00
|
|
|
defmodule LivebookWeb.SessionLive.ElixirStandaloneLive do
|
|
|
|
use LivebookWeb, :live_view
|
2021-02-27 03:53:29 +08:00
|
|
|
|
2021-03-04 05:56:28 +08:00
|
|
|
alias Livebook.{Session, Runtime}
|
2021-02-27 03:53:29 +08:00
|
|
|
|
|
|
|
@impl true
|
2021-09-05 01:16:01 +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.ElixirStandalone) do
|
|
|
|
raise "runtime module not allowed"
|
|
|
|
end
|
|
|
|
|
2021-03-24 02:41:03 +08:00
|
|
|
if connected?(socket) do
|
2022-04-19 17:29:38 +08:00
|
|
|
Session.subscribe(session.id)
|
2021-03-24 02:41:03 +08:00
|
|
|
end
|
|
|
|
|
2021-09-05 01:16:01 +08:00
|
|
|
{:ok, assign(socket, session: session, current_runtime: current_runtime, error_message: nil)}
|
2021-02-27 03:53:29 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def render(assigns) do
|
2021-07-07 20:32:49 +08:00
|
|
|
~H"""
|
2021-04-04 18:42:46 +08:00
|
|
|
<div class="flex-col space-y-5">
|
2021-05-10 20:37:38 +08:00
|
|
|
<%= if @error_message do %>
|
|
|
|
<div class="error-box">
|
|
|
|
<%= @error_message %>
|
|
|
|
</div>
|
|
|
|
<% end %>
|
2021-03-20 21:10:15 +08:00
|
|
|
<p class="text-gray-700">
|
2021-02-27 03:53:29 +08:00
|
|
|
Start a new local node to handle code evaluation.
|
|
|
|
</p>
|
2021-12-04 04:57:21 +08:00
|
|
|
<button class="button-base button-blue" phx-click="init">
|
2021-03-24 02:41:03 +08:00
|
|
|
<%= if(matching_runtime?(@current_runtime), do: "Reconnect", else: "Connect") %>
|
2021-02-27 03:53:29 +08:00
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
"""
|
|
|
|
end
|
|
|
|
|
2022-10-04 05:58:26 +08:00
|
|
|
defp matching_runtime?(%Runtime.ElixirStandalone{} = runtime), do: Runtime.connected?(runtime)
|
2021-03-24 02:41:03 +08:00
|
|
|
defp matching_runtime?(_runtime), do: false
|
|
|
|
|
2021-02-27 03:53:29 +08:00
|
|
|
@impl true
|
|
|
|
def handle_event("init", _params, socket) do
|
2022-04-02 02:13:37 +08:00
|
|
|
Runtime.ElixirStandalone.new()
|
|
|
|
|> Runtime.connect()
|
|
|
|
|> case do
|
2021-05-10 20:37:38 +08:00
|
|
|
{:ok, runtime} ->
|
2022-04-02 02:13:37 +08:00
|
|
|
Session.set_runtime(socket.assigns.session.pid, runtime)
|
2021-05-10 20:37:38 +08:00
|
|
|
{:noreply, assign(socket, error_message: nil)}
|
|
|
|
|
|
|
|
{:error, message} ->
|
|
|
|
{:noreply, assign(socket, error_message: message)}
|
|
|
|
end
|
2021-02-27 03:53:29 +08:00
|
|
|
end
|
2021-03-24 02:41:03 +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-02-27 03:53:29 +08:00
|
|
|
end
|