Packages

Generic Timer that broadcasts :tick to subscribers

Current section

Files

Jump to
vt_gen_timer lib gen_timer.ex
Raw

lib/gen_timer.ex

defmodule GenTimer do
@moduledoc """
Intervaled Timer, broadcasts `{:tick, state}` to topic subscribers every 1 second.
Timer is initialized with a topic and duration in seconds. Once initialized the timer can be started and paused.
Resetting the timer returns elasped time to zero.
"""
def initialize(duration, topic) do
with :ok <- GenTimer.Manager.initialize(duration, topic) do
GenTimer.PubSub.subscribe(topic)
{:ok, :initialized}
else
e -> e
end
end
defdelegate start(name), to: GenTimer.Manager
defdelegate pause(name), to: GenTimer.Manager
defdelegate reset(name), to: GenTimer.Manager
def subscribe(topic) do
case lookup(topic) do
[] ->
{:error, :unknown}
_ ->
GenTimer.PubSub.subscribe(topic)
{:ok, GenTimer.Manager.get_timer(topic)}
end
end
def unsubscribe(topic) do
GenTimer.PubSub.unsubscribe(topic)
end
def lookup(topic) do
Registry.lookup(GenTimer.Registry.Manager, topic)
end
end