38 lines
846 B
Elixir
38 lines
846 B
Elixir
defmodule Prymn.Messaging.ConnectionManager do
|
|
use GenServer
|
|
|
|
defstruct subscriptions: %{}
|
|
|
|
def publish(subject, payload) do
|
|
GenServer.call(__MODULE__, {:pub, subject, payload})
|
|
end
|
|
|
|
def subscribe(subject) do
|
|
GenServer.call(__MODULE__, {:pub, subject})
|
|
end
|
|
|
|
@impl true
|
|
def init(_init_arg) do
|
|
children = [
|
|
{Prymn.Messaging.Connection, %{name: :nats}}
|
|
]
|
|
|
|
Process.monitor(:nats)
|
|
|
|
{:ok, %__MODULE__{}}
|
|
end
|
|
|
|
@impl true
|
|
def handle_call({:pub, subject, payload}, _from, state) do
|
|
Prymn.Messaging.Connection.publish(:nats, subject, payload)
|
|
{:reply, :ok, state}
|
|
end
|
|
|
|
def handle_call({:sub, subject}, {pid, _}, %__MODULE__{subscriptions: subsciptions} = state) do
|
|
Prymn.Messaging.Connection.subscribe(:nats, subject, pid)
|
|
|
|
# Map.put(subsciptions)
|
|
|
|
{:reply, :ok, state}
|
|
end
|
|
end
|