44 lines
1.1 KiB
Elixir
44 lines
1.1 KiB
Elixir
defmodule Localiser.Localisation.Floor.Manager do
|
|
@moduledoc """
|
|
Global GenServer that reacts to floor lifecycle events and drives Floor.Supervisor.
|
|
|
|
- {:floor_created, floor} → start a Floor.Server for the new floor
|
|
- {:floor_deleted, floor_id} → terminate the Floor.Server subtree
|
|
"""
|
|
|
|
use GenServer
|
|
|
|
alias Localiser.Localisation.Floor
|
|
|
|
def start_link(_args) do
|
|
GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
|
|
end
|
|
|
|
@impl true
|
|
def init(:ok) do
|
|
Phoenix.PubSub.subscribe(Localiser.PubSub, "floors")
|
|
{:ok, :ok}
|
|
end
|
|
|
|
@impl true
|
|
def handle_info({:floor_created, floor}, state) do
|
|
case Registry.lookup(Localiser.Registry, {:floor_server, floor.id}) do
|
|
[] -> Floor.Supervisor.start_floor_server(floor)
|
|
_ -> :ok
|
|
end
|
|
|
|
{:noreply, state}
|
|
end
|
|
|
|
def handle_info({:floor_deleted, floor_id}, state) do
|
|
case Registry.lookup(Localiser.Registry, {:floor_server, floor_id}) do
|
|
[{pid, _}] -> DynamicSupervisor.terminate_child(Floor.Supervisor, pid)
|
|
[] -> :ok
|
|
end
|
|
|
|
{:noreply, state}
|
|
end
|
|
|
|
def handle_info(_msg, state), do: {:noreply, state}
|
|
end
|