Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Changing mechanism to push new messages to clients.

... from direct callback to sessions.
  • Loading branch information...
commit 65023c1d84308c67987afa33c44156611d1a9c03 1 parent 8d045fb
@akoprow akoprow authored
Showing with 18 additions and 15 deletions.
  1. +12 −12 src/msg_factory.opa
  2. +6 −3 src/pages.opa
View
24 src/msg_factory.opa
@@ -8,27 +8,26 @@ package mlstate.twopenny
/**
* @private
**/
-type MsgFactory.msg_subscription = channel(
+type MsgFactory.msg_listener = channel(
{ newmsg : Msg.t }
)
MsgFactory = {{
- @private on_subscription(callbacks, msg) =
+ @private on_subscription(listeners, msg) =
match msg with
- | { SubscribeToAll=callback } ->
- {set = [callback | callbacks]}
+ | { NewListener=listener } ->
+ {set = [listener | listeners]}
| { NewMessage=newmsg } ->
- /* REMARK Type annotation below needed to avoid value
- restriction error */
- notify(callback) = callback(newmsg : Msg.t)
- do List.iter(notify, callbacks)
+ do Debug.jlog("Notifying {List.length(listeners)} clients about a new message")
+ notify(listener) = Session.send(listener, ~{newmsg})
+ do List.iter(notify, listeners)
{unchanged}
@private @server subscribers =
- Session.make([], on_subscription)
+ Session.make([] : list(MsgFactory.msg_listener), on_subscription)
- submit(msg : Msg.t) =
+ @server submit(msg : Msg.t) =
do Data.new_message(msg)
index =
| ~{user} -> Data.new_user_mention(user, msg)
@@ -39,8 +38,9 @@ MsgFactory = {{
do Session.send(subscribers, {NewMessage=msg})
void
- subscribe_to_all(callback : Msg.t -> void) : void =
- do Session.send(subscribers, {SubscribeToAll=callback})
+ subscribe_to_all(listener : MsgFactory.msg_listener) : void =
+ do Debug.jlog("sb. is subscribing to all new msgs.")
+ do Session.send(subscribers, {NewListener=listener})
void
}}
View
9 src/pages.opa
@@ -19,10 +19,13 @@ Pages = {{
main_page() =
("Twopenny", unimplemented)
+ @client show_new_message(~{newmsg}) =
+ exec([#msgs -<- Msg.render(newmsg, {final})])
+
@client setup_msg_updates(_) =
- show_new_message(msg) =
- exec([#msgs -<- Msg.render(msg, {final})])
- do MsgFactory.subscribe_to_all(show_new_message)
+ chan = Session.make_callback(show_new_message)
+ do MsgFactory.subscribe_to_all(chan)
+ do debug("subscribing to new messages")
void
@client setup_newmsg_box(user)(_) =
Please sign in to comment.
Something went wrong with that request. Please try again.