Last active
April 5, 2017 00:17
-
-
Save thiagoa/e1baae3f2d9ea5a556173461a6e5b2ae to your computer and use it in GitHub Desktop.
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 Hub do | |
defmacro on_match(pattern, do: on_match, after: on_after) do | |
on_match = quote do | |
term -> Hub.match term, unquote(pattern), do: unquote(on_match) | |
end | |
quote do | |
receive(do: unquote(on_match), after: unquote(on_after)) | |
end | |
end | |
# Dummy macro | |
defmacro match(_term, _pattern, do: block) do | |
block | |
end | |
# Dummy subscribe | |
def subscribe(pid) do | |
IO.puts "Subscribed to #{pid}" | |
end | |
end | |
defmodule Subscriber do | |
require Hub | |
def subscribe do | |
Hub.subscribe :hub | |
Hub.on_match %{foo: foo} when foo < 5 do | |
# Do the job | |
after | |
1_000 -> IO.puts "timed out!" | |
end | |
end | |
end | |
Subscriber.subscribe |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment