Created
July 7, 2016 06:14
-
-
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.
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
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