Display "Livebook is shutting down" flash on desktop app shutdown (#1645)

This commit is contained in:
Wojtek Mach 2023-01-18 14:11:40 +01:00 committed by GitHub
parent f13006490b
commit 32d0f89312
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 31 additions and 12 deletions

View file

@ -24,7 +24,6 @@ defmodule ElixirKit.Server do
@impl true
def handle_info({:tcp_closed, socket}, state) when socket == state.socket do
System.stop()
{:stop, :shutdown, state}
end
end

View file

@ -115,6 +115,20 @@ defmodule Livebook.Config do
Application.get_env(:livebook, :iframe_url)
end
@doc """
Shuts down the system, if possible.
"""
def shutdown do
case Livebook.Config.shutdown_callback() do
{m, f, a} ->
Phoenix.PubSub.broadcast(Livebook.PubSub, "sidebar", :shutdown)
apply(m, f, a)
nil ->
:ok
end
end
@doc """
Returns an mfa if there's a way to shut down the system.
"""

View file

@ -8,8 +8,9 @@ if Mix.target() == :app do
@impl true
def init(_) do
{:ok, _} = ElixirKit.start()
{:ok, nil}
{:ok, pid} = ElixirKit.start()
ref = Process.monitor(pid)
{:ok, %{ref: ref}}
end
@impl true
@ -18,6 +19,12 @@ if Mix.target() == :app do
{:noreply, state}
end
@impl true
def handle_info({:DOWN, ref, :process, _, :shutdown}, state) when ref == state.ref do
Livebook.Config.shutdown()
{:noreply, state}
end
defp open("") do
open(LivebookWeb.Endpoint.access_url())
end

View file

@ -7,17 +7,23 @@ defmodule LivebookWeb.SidebarHook do
def on_mount(:default, _params, _session, socket) do
if connected?(socket) do
Livebook.Hubs.subscribe([:crud, :connection])
Phoenix.PubSub.subscribe(Livebook.PubSub, "sidebar")
end
socket =
socket
|> assign(saved_hubs: Livebook.Hubs.get_metadatas())
|> attach_hook(:hubs, :handle_info, &handle_info/2)
|> attach_hook(:shutdown, :handle_info, &handle_info/2)
|> attach_hook(:shutdown, :handle_event, &handle_event/3)
{:cont, socket}
end
defp handle_info(:shutdown, socket) do
{:halt, put_flash(socket, :info, "Livebook is shutting down. You can close this page.")}
end
@connection_events ~w(hub_connected hub_disconnected hubs_metadata_changed)a
defp handle_info(event, socket) when event in @connection_events do
@ -33,15 +39,8 @@ defmodule LivebookWeb.SidebarHook do
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
Livebook.Config.shutdown()
{:halt, socket}
end
defp handle_event(_event, _params, socket), do: {:cont, socket}