Skip to content

Instantly share code, notes, and snippets.

@GoNZooo
Created November 19, 2015 21:56
Show Gist options
  • Save GoNZooo/73ffcfe846bb1a55b202 to your computer and use it in GitHub Desktop.
Save GoNZooo/73ffcfe846bb1a55b202 to your computer and use it in GitHub Desktop.
defmodule Consumer.Subscriptions do
def start_link do
Agent.start_link(fn -> %{} end, name: __MODULE__)
end
def get_subscribers(topic) do
Agent.get(__MODULE__, fn map -> Map.get(map, topic, []) end)
end
def add_subscription(sub, topic) do
Agent.update(__MODULE__, fn map -> Map.put(map, topic, [sub|Map.get(map, topic, [])]) end)
end
def remove_subscriber(sub, topic) do
Agent.update(__MODULE__, fn map ->
Map.put(map, topic, List.delete(Map.get(map, topic, []), sub))
end)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment