Skip to content

Instantly share code, notes, and snippets.

@thiagoa
Last active April 5, 2017 00:17
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 thiagoa/e1baae3f2d9ea5a556173461a6e5b2ae to your computer and use it in GitHub Desktop.
Save thiagoa/e1baae3f2d9ea5a556173461a6e5b2ae to your computer and use it in GitHub Desktop.
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