Skip to content

Instantly share code, notes, and snippets.

@Slavenin
Last active April 23, 2018 09:43
Show Gist options
  • Save Slavenin/f78fa3f9531b63d126078f7df9a2779c to your computer and use it in GitHub Desktop.
Save Slavenin/f78fa3f9531b63d126078f7df9a2779c to your computer and use it in GitHub Desktop.
defmodule Behaviours.Telegram do
@moduledoc false
@callback sendInfo(accountId :: term, info :: term) :: {:ok}
end
defmodule TelegramModule do
@moduledoc false
defmacro __using__(opts) do
reg = Keyword.get(opts, :reg)
quote do
@behaviour Behaviours.Telegram
def start_link(accountId) do
name = via_tuple(accountId)
GenServer.start_link(__MODULE__, [accountId], name: name)
end
defp via_tuple(accountId) do
{:via, Registry, {unquote(reg), accountId}}
end
def sendInfo(accountId, info) do
GenServer.cast(via_tuple(accountId), info)
end
end
end
end
defmodule Start do
@moduledoc false
use TelegramModule, reg: :process_registry
use GenServer
def init(_opts) do
{:ok, %{}}
end
def handle_cast(_msg, state) do
{:noreply, state}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment