-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🚧 adapter/local: Add a Registry-based adapter
- Loading branch information
Alexandra Wolf
committed
Oct 4, 2023
1 parent
cbee3ae
commit c0da428
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
defmodule Gazet.Adapter.Local do | ||
alias Gazet.Message | ||
alias Gazet.Subscriber | ||
|
||
@behaviour Gazet.Adapter | ||
|
||
@impl true | ||
def child_spec(%{name: name}) do | ||
Registry.child_spec( | ||
keys: :duplicate, | ||
name: name, | ||
partitions: System.schedulers_online() | ||
) | ||
end | ||
|
||
@impl true | ||
def publish(%{name: name, topic: topic}, %Message{} = message) do | ||
Registry.dispatch(name, topic, fn entries -> | ||
for {pid, _} <- entries, do: send(pid, {:message, topic, message}) | ||
end) | ||
end | ||
|
||
@impl true | ||
def subscriber_spec(%{name: name, topic: topic}, %Subscriber{} = subscriber) do | ||
subscriber | ||
|> put_in([Access.key!(:adapter_opts), :on_start], fn -> | ||
with {:ok, _} <- Registry.register(name, topic, :ignored) do | ||
:ok | ||
end | ||
end) | ||
|> Subscriber.Generic.child_spec() | ||
end | ||
end |