Created
March 26, 2019 13:31
-
-
Save ccapndave/b959b0296178034a596c635f4b507e5d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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