defmodule Mv.Vereinfacht.SyncFlash do @moduledoc """ Short-lived store for Vereinfacht sync results so the UI can show them after save. The SyncContact change runs in after_transaction and cannot access the LiveView socket. This module stores a message keyed by member_id; the form LiveView calls `take/1` after a successful save and displays the message in flash. """ @table :vereinfacht_sync_flash @doc """ Stores a sync result for the given member. Overwrites any previous message. - `:ok` - Sync succeeded (optional user message). - `:warning` - Sync failed; message should be shown as a warning. """ @spec store(String.t(), :ok | :warning, String.t()) :: :ok def store(member_id, kind, message) when is_binary(member_id) do :ets.insert(@table, {member_id, {kind, message}}) :ok end @doc """ Takes and removes the stored sync message for the given member. Returns `{kind, message}` if present, otherwise `nil`. """ @spec take(String.t()) :: {:ok | :warning, String.t()} | nil def take(member_id) when is_binary(member_id) do case :ets.take(@table, member_id) do [{^member_id, value}] -> value [] -> nil end end @doc false def create_table! do if :ets.whereis(@table) == :undefined do :ets.new(@table, [:set, :public, :named_table]) end :ok end end