Packages
ex_sorcery
0.1.0
A toolkit for building event-driven applications in Elixir with event sourcing and CQRS patterns
Current section
Files
Jump to
Current section
Files
lib/sorcery/pub_sub/registry.ex
defmodule Sorcery.PubSub.Registry do
@behaviour Sorcery.PubSub.Behaviour
@registry_name :sorcery_pubsub_registry
@impl true
def start(_opts \\ []) do
case Registry.start_link(keys: :duplicate, name: @registry_name, partitions: System.schedulers_online()) do
{:ok, pid} -> {:ok, pid}
{:error, {:already_started, pid}} -> {:ok, pid}
error -> error
end
end
@impl true
def subscribe() do
Registry.register(@registry_name, "events", [])
end
@impl true
def subscribe(subscription, opts \\ []) do
Registry.register(@registry_name, subscription, opts)
end
@impl true
def publish(events) do
events
|> List.wrap()
|> Enum.each(fn event ->
Registry.dispatch(@registry_name, "events", fn entries ->
for {pid, _} <- entries do
send(pid, {:event, event} )
end
end)
end)
:ok
end
end