~statianzo/sevenlangs

ref: 72c182f0a1ac132a102168af410fb5f5e594085d sevenlangs/erlang/todo/src/event.erl -rw-r--r-- 769 bytes
72c182f0Jason Staten start todos 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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.