Skip to content

Instantly share code, notes, and snippets.

@ccapndave
Created March 26, 2019 13:31
Show Gist options
  • Save ccapndave/b959b0296178034a596c635f4b507e5d to your computer and use it in GitHub Desktop.
Save ccapndave/b959b0296178034a596c635f4b507e5d to your computer and use it in GitHub Desktop.
defmodule AlchemyWeb.UserChannelListener do
use GenServer
def start_link(name: name) do
GenServer.start_link(__MODULE__, [], name: name)
end
def init(_) do
{:ok, %{}}
end
def handle_info({:register, Registry.UserChannels, _key, pid, token}, state) do
{:noreply, open_channel(pid, token, state)}
end
def handle_info({:unregister, Registry.UserChannels, _, pid}, state) do
{:noreply, close_channel(pid, state)}
end
def handle_info({:DOWN, _, _, pid, _}, state) do
{:noreply, close_channel(pid, state)}
end
defp open_channel(pid, token, state) do
Process.monitor(pid)
# Do stuff
state |> Map.put(pid, token)
end
defp close_channel(pid, state) do
case state |> Map.get(pid) do
nil ->
state
token ->
# do stuff
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment