Skip to content

Commit

Permalink
🚧 adapter/local: Add a Registry-based adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandra Wolf committed Oct 4, 2023
1 parent cbee3ae commit c0da428
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions lib/gazet/adapter/local.ex
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

0 comments on commit c0da428

Please sign in to comment.