Skip to content

Commit

Permalink
Merge pull request #40 from edennis/confused-results
Browse files Browse the repository at this point in the history
Make it possible to use multiple websockets and callbacks
  • Loading branch information
saschatimme authored Nov 29, 2017
2 parents 14289d9 + 9a185c3 commit 584ecbb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
31 changes: 19 additions & 12 deletions src/Phoenix.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ effect module Phoenix where { command = MyCmd, subscription = MySub } exposing (
This package makes it easy to connect to Phoenix Channels, but in a more declarative manner than the Phoenix Socket Javascript library. Simply provide a `Socket` and a list of `Channel`s you want to join and this library handles the tedious parts like opening a connection, joining channels, reconnecting after a network error and registering event handlers.
#Connect with Phoenix
@docs connect
# Push messages
@docs push
-}

import Json.Encode as Encode exposing (Value)
Expand Down Expand Up @@ -54,6 +56,7 @@ type MySub msg
connect socket [channel]
**Note**: An empty channel list keeps the socket connection open.
-}
connect : Socket msg -> List (Channel msg) -> Sub msg
connect socket channels =
Expand All @@ -79,8 +82,8 @@ type MyCmd msg
push "ws://localhost:4000/socket/websocket" message
**Note**: The message will be queued until you successfully joined a channel to the topic of the message.
-}
push : String -> Push msg -> Cmd msg
push endpoint push_ =
Expand Down Expand Up @@ -118,7 +121,7 @@ type alias Message =
type alias State msg =
{ sockets : InternalSocketsDict msg
, channels : InternalChannelsDict msg
, selfCallbacks : Dict Ref (SelfCallback msg)
, selfCallbacks : SelfCallbackDict msg
, channelQueues : ChannelQueuesDict msg
}

Expand Down Expand Up @@ -147,6 +150,10 @@ type alias ChannelQueuesDict msg =
Dict Endpoint (Dict Topic (List ( Message, Maybe (SelfCallback msg) )))


type alias SelfCallbackDict msg =
Dict ( Ref, Endpoint ) (SelfCallback msg)


type alias Callback msg =
Value -> msg

Expand Down Expand Up @@ -372,7 +379,7 @@ updateChannels channels state =
{ state | channels = channels }


updateSelfCallbacks : Dict Ref (SelfCallback msg) -> State msg -> State msg
updateSelfCallbacks : SelfCallbackDict msg -> State msg -> State msg
updateSelfCallbacks selfCallbacks state =
{ state | selfCallbacks = selfCallbacks }

Expand All @@ -389,15 +396,15 @@ insertSocket endpoint socket state =
}


insertSelfCallback : Ref -> Maybe (SelfCallback msg) -> State msg -> State msg
insertSelfCallback ref maybeSelfCb state =
insertSelfCallback : ( Ref, Endpoint ) -> Maybe (SelfCallback msg) -> State msg -> State msg
insertSelfCallback ( ref, endpoint ) maybeSelfCb state =
case maybeSelfCb of
Nothing ->
state

Just selfCb ->
{ state
| selfCallbacks = Dict.insert ref selfCb state.selfCallbacks
| selfCallbacks = Dict.insert ( ref, endpoint ) selfCb state.selfCallbacks
}


Expand Down Expand Up @@ -633,20 +640,20 @@ onSelfMsg router selfMsg state =
Task.succeed state


handleSelfcallback : Platform.Router msg (Msg msg) -> Endpoint -> Message -> Dict Ref (SelfCallback msg) -> Task x (Dict Ref (SelfCallback msg))
handleSelfcallback : Platform.Router msg (Msg msg) -> Endpoint -> Message -> SelfCallbackDict msg -> Task x (SelfCallbackDict msg)
handleSelfcallback router endpoint message selfCallbacks =
case message.ref of
Nothing ->
Task.succeed selfCallbacks

Just ref ->
case Dict.get ref selfCallbacks of
case Dict.get ( ref, endpoint ) selfCallbacks of
Nothing ->
Task.succeed selfCallbacks

Just selfCb ->
Platform.sendToSelf router (selfCb message)
&> Task.succeed (Dict.remove ref selfCallbacks)
&> Task.succeed (Dict.remove ( ref, endpoint ) selfCallbacks)


processQueue : Endpoint -> List ( Message, Maybe (SelfCallback msg) ) -> State msg -> Task x (State msg)
Expand Down Expand Up @@ -968,7 +975,7 @@ pushSocket_ endpoint message maybeSelfCb state =

Just ref ->
insertSocket endpoint (InternalSocket.increaseRef socket) state
|> insertSelfCallback ref maybeSelfCb
|> insertSelfCallback ( ref, endpoint ) maybeSelfCb
|> Task.succeed


Expand All @@ -990,7 +997,7 @@ pushSocket endpoint message selfCb state =

Just ref ->
insertSocket endpoint (InternalSocket.increaseRef socket) state
|> insertSelfCallback ref selfCb
|> insertSelfCallback ( ref, endpoint ) selfCb
|> Task.succeed
in
case Dict.get endpoint state.sockets of
Expand Down
9 changes: 6 additions & 3 deletions src/Phoenix/Presence.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ module Phoenix.Presence exposing (Presence, create, onChange, onJoins, onLeaves,

{-| Presence is an extension for channels to support the Presence feature of Phoenix.
# Definition
@docs Presence
# Helpers
@docs init, withPayload, on, onJoin, onRequestJoin, onJoinError, onError, onDisconnect, onRejoin, onLeave, onLeaveError, withDebug, map
@docs init, withPayload, on, onJoin, onRequestJoin, onJoinError, onError, onDisconnect, onRejoin, onLeave, onLeaveError, withDebug, withPresence, map
@docs create, onChange, onJoins, onLeaves, map
-}

import Dict exposing (Dict)
Expand Down Expand Up @@ -49,7 +53,6 @@ then an example would be a Dict with
, "user2": [{online_at: 1491492646123}, {online_at: 1491492646624}]
}
-}
onChange : (Dict String (List Value) -> msg) -> PhoenixPresence msg -> PhoenixPresence msg
onChange func presence =
Expand Down

0 comments on commit 584ecbb

Please sign in to comment.