Permalink
Browse files

Allow the callback to be module (instead of fun)

  • Loading branch information...
1 parent 3562931 commit 977e1a4f7e35c078ec210dc69217d34a0eb15a03 @majek majek committed Mar 1, 2012
Showing with 24 additions and 1 deletion.
  1. +13 −0 src/sockjs_service.erl
  2. +11 −1 src/sockjs_session.erl
View
@@ -0,0 +1,13 @@
+-module(sockjs_service).
+
+-export([behaviour_info/1]).
+
+behaviour_info(callbacks) ->
+ [
+ {sockjs_init, 2},
+ {sockjs_handle, 3},
+ {sockjs_terminate, 2}
+ ];
+
+behaviour_info(_Other) ->
+ undefined.
View
@@ -150,7 +150,17 @@ unmark_waiting(RPid, State = #session{response_pid = Pid,
emit(What, State = #session{callback = Callback,
state = UserState,
handle = Handle}) ->
- case Callback(Handle, What, UserState) of
+ R = case Callback of
+ _ when is_function(Callback) ->
+ Callback(Handle, What, UserState);
+ _ when is_atom(Callback) ->
+ case What of
+ init -> Callback:sockjs_init(Handle, UserState);
+ {recv, Data} -> Callback:sockjs_handle(Handle, Data, UserState);
+ closed -> Callback:sockjs_terminate(Handle, UserState)
+ end
+ end,
+ case R of
{ok, UserState1} -> State#session{state = UserState1};
ok -> State
end.

0 comments on commit 977e1a4

Please sign in to comment.