Current section

Files

Jump to
grains lib grains timer.ex
Raw

lib/grains/timer.ex

defmodule Grains.Timer do
@moduledoc """
A periodic timer.
"""
use Grains.GenGrain
defmodule State do
@enforce_keys [:period]
defstruct @enforce_keys ++ [from: nil, with_timestamps: false]
end
def name(left, grain) do
Module.concat([left, grain, Timer])
end
def init(opts) do
state = struct!(State, opts)
{:ok, _} = :timer.send_interval(state.period, :tick)
{:ok, state}
end
def handle_info(:tick, state) do
pull()
{:noreply, state}
end
def handle_push(msg, _from, state = %{with_timestamps: true}) do
now = DateTime.utc_now()
:ok = push({now, msg})
{:noreply, state}
end
def handle_push(msg, _from, state) do
:ok = push(msg)
{:noreply, state}
end
end