mirror of
https://github.com/livebook-dev/livebook.git
synced 2024-11-16 21:05:38 +08:00
52e5273401
This commit lets you customize Livebook shutdown to gracefully power off. One use case is for Nerves-based devices that need to do more than call `System.stop/0` to power off. This doesn't change the `LIVEBOOK_SHUTDOWN_ENABLED` environment variable. It still works the same. The `:shutdown_enabled` configuration is now `:shutdown_callback`. Valid values are `nil` or an MFA. An unset or `nil` callback hides the shutdown button in the UI.
38 lines
978 B
Elixir
38 lines
978 B
Elixir
defmodule LivebookWeb.SidebarHook do
|
|
import Phoenix.Component
|
|
import Phoenix.LiveView
|
|
|
|
def on_mount(:default, _params, _session, socket) do
|
|
if connected?(socket) do
|
|
Livebook.Hubs.subscribe()
|
|
end
|
|
|
|
socket =
|
|
socket
|
|
|> assign(saved_hubs: Livebook.Hubs.fetch_metadatas())
|
|
|> attach_hook(:hubs, :handle_info, &handle_info/2)
|
|
|> attach_hook(:shutdown, :handle_event, &handle_event/3)
|
|
|
|
{:cont, socket}
|
|
end
|
|
|
|
defp handle_info({:hubs_metadata_changed, hubs}, socket) do
|
|
{:halt, assign(socket, :saved_hubs, hubs)}
|
|
end
|
|
|
|
defp handle_info(_event, socket), do: {:cont, socket}
|
|
|
|
defp handle_event("shutdown", _params, socket) do
|
|
case Livebook.Config.shutdown_callback() do
|
|
{m, f, a} ->
|
|
apply(m, f, a)
|
|
|
|
{:halt, put_flash(socket, :info, "Livebook is shutting down. You can close this page.")}
|
|
|
|
_ ->
|
|
socket
|
|
end
|
|
end
|
|
|
|
defp handle_event(_event, _params, socket), do: {:cont, socket}
|
|
end
|