Skip to content

Instantly share code, notes, and snippets.

@heri16
Created July 7, 2016 06:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heri16/f9223445a6f8ae65f2f10a2180010ddf to your computer and use it in GitHub Desktop.
Save heri16/f9223445a6f8ae65f2f10a2180010ddf to your computer and use it in GitHub Desktop.
Simple module that wraps the :syn erlang library, in order to conform to Elixir's send "via-module-tuple" schematics.
defmodule Syn.Registry do
@moduledoc ~S"""
Simple module that wraps the :syn erlang library, in order to conform to Elixir's send "via-module-tuple" schematics.
This wrapper is temporary until :sync erlang library supports the use from Elixir directly.
"""
# "Via" API
@spec whereis_name(any) :: pid | :undefined
def whereis_name(key) do
:syn.find_by_key(key)
end
@spec register_name(any, pid) :: :yes | :no
def register_name(key, pid) do
case :syn.register(key, pid) do
:ok -> :yes
{:error, _} -> :no
_ -> :no
end
end
@spec unregister_name(any) :: any | :nil
def unregister_name(key) do
case :syn.unregister(key) do
:ok -> key
{:error, _} -> nil
_ -> nil
end
end
@spec send(any, any) :: {:badarg, {any, any}} | pid
def send(key, message) do
case whereis_name(key) do
:undefined ->
{:badarg, {key, message}}
pid ->
Kernel.send(pid, message)
pid
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment