~statianzo/sevenlangs

72c182f0a1ac132a102168af410fb5f5e594085d — Jason Staten 1 year, 10 months ago c079ef1
start todos
1 files changed, 29 insertions(+), 0 deletions(-)

A erlang/todo/src/event.erl
A erlang/todo/src/event.erl => erlang/todo/src/event.erl +29 -0
@@ 0,0 1,29 @@
-module(event).

-record(state, {server, name = "", to_go = 0}).

-export([init/3, start/2, start_link/2]).

start(EventName, Delay) ->
    spawn(?MODULE, init, [self(), EventName, Delay]).

start_link(EventName, Delay) ->
    spawn_link(?MODULE, init, [self(), EventName, Delay]).

init(Server, EventName, Delay) ->
    loop(#state{server = Server, name = EventName,
		to_go = normalize(Delay)}).

normalize(N) ->
    Limit = 49 * 24 * 60 * 60,
    [N rem Limit | lists:duplicate(N div Limit, Limit)].

loop(S = #state{server = Server, to_go = [T | Next]}) ->
    receive
      {Server, Ref, cancel} -> Server ! {Ref, ok}
    after T * 1000 ->
      case Next of
        [] -> Server ! {done, S#state.name};
        _ -> loop(S#state{to_go = Next})
      end
    end.