'after' in gen_server

Ulf Wiger <>
Fri Mar 24 07:08:12 CET 2006


Den 2006-03-24 04:24:17 skrev <>:

> The reason I don't like 1 that much is because my code looks something  
> like:
> handle_cast({irc_connect, Nick}, #state{state=connecting, dict=Dict,  
> irclib=Irclib} = State) ->
>     % Do connect stuff
>     join_channels(Irclib, dict_proc:fetch(join_on_connect, Dict)),
>     {noreply, State#state{nick=Nick, state=idle}};
> handle_cast({stop, _}, #state{state=connecting} = State) ->
>     {stop, stop, State};
...
> handle_cast({irc_message, {_, "KICK", [Channel, Nick, _]}},  
> #state{irclib=Irclib, nick=Nick} = State) ->
>     irc_lib:join(Irclib, Channel),
>     {noreply, State};
>  So it gets ugly to return a timeout in all of these places.

So wrap the return value in e.g. a noreply/1 function:


noreply(State) ->
   {noreply, State, _Timeout = 10000}.

Then your code becomes:

handle_cast({irc_connect, Nick}, #state{state=connecting, dict=Dict,  
irclib=Irclib} = State) ->
     % Do connect stuff
     join_channels(Irclib, dict_proc:fetch(join_on_connect, Dict)),
     noreply(State#state{nick=Nick, state=idle});
handle_cast({stop, _}, #state{state=connecting} = State) ->
     {stop, stop, State};
handle_cast({irc_message, {_, "PONG", _}}, #state{state=pong,  
pong_timeout=Ref} = State) ->
     {ok, cancel} = timer:cancel(Ref),
     noreply(State#state{state=idle, pong_timeout=undefined});
handle_cast(irc_closed, #state{irclib=Irclib} = State) ->
     irc_lib:connect(Irclib),
     noreply(State#state{state=connecting});
handle_cast({irc_message, {_, "PING", [Server]}}, #state{irclib=Irclib} =  
State) ->
     irc_lib:pong(Irclib, Server),
     noreply(State);
handle_cast({irc_message, {_, "KICK", [Channel, Nick, _]}},  
#state{irclib=Irclib, nick=Nick} = State) ->
     irc_lib:join(Irclib, Channel),
     noreply(State);

BR,
Ulf W

-- 
Ulf Wiger



More information about the erlang-questions mailing list