Skip to content

Commit 584ecbb

Browse files
authored
Merge pull request #40 from edennis/confused-results
Make it possible to use multiple websockets and callbacks
2 parents 14289d9 + 9a185c3 commit 584ecbb

File tree

2 files changed

+25
-15
lines changed

2 files changed

+25
-15
lines changed

src/Phoenix.elm

+19-12
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ effect module Phoenix where { command = MyCmd, subscription = MySub } exposing (
44
55
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.
66
7-
87
#Connect with Phoenix
98
@docs connect
109
10+
1111
# Push messages
12+
1213
@docs push
14+
1315
-}
1416

1517
import Json.Encode as Encode exposing (Value)
@@ -54,6 +56,7 @@ type MySub msg
5456
connect socket [channel]
5557
5658
**Note**: An empty channel list keeps the socket connection open.
59+
5760
-}
5861
connect : Socket msg -> List (Channel msg) -> Sub msg
5962
connect socket channels =
@@ -79,8 +82,8 @@ type MyCmd msg
7982
8083
push "ws://localhost:4000/socket/websocket" message
8184
82-
8385
**Note**: The message will be queued until you successfully joined a channel to the topic of the message.
86+
8487
-}
8588
push : String -> Push msg -> Cmd msg
8689
push endpoint push_ =
@@ -118,7 +121,7 @@ type alias Message =
118121
type alias State msg =
119122
{ sockets : InternalSocketsDict msg
120123
, channels : InternalChannelsDict msg
121-
, selfCallbacks : Dict Ref (SelfCallback msg)
124+
, selfCallbacks : SelfCallbackDict msg
122125
, channelQueues : ChannelQueuesDict msg
123126
}
124127

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

149152

153+
type alias SelfCallbackDict msg =
154+
Dict ( Ref, Endpoint ) (SelfCallback msg)
155+
156+
150157
type alias Callback msg =
151158
Value -> msg
152159

@@ -372,7 +379,7 @@ updateChannels channels state =
372379
{ state | channels = channels }
373380

374381

375-
updateSelfCallbacks : Dict Ref (SelfCallback msg) -> State msg -> State msg
382+
updateSelfCallbacks : SelfCallbackDict msg -> State msg -> State msg
376383
updateSelfCallbacks selfCallbacks state =
377384
{ state | selfCallbacks = selfCallbacks }
378385

@@ -389,15 +396,15 @@ insertSocket endpoint socket state =
389396
}
390397

391398

392-
insertSelfCallback : Ref -> Maybe (SelfCallback msg) -> State msg -> State msg
393-
insertSelfCallback ref maybeSelfCb state =
399+
insertSelfCallback : ( Ref, Endpoint ) -> Maybe (SelfCallback msg) -> State msg -> State msg
400+
insertSelfCallback ( ref, endpoint ) maybeSelfCb state =
394401
case maybeSelfCb of
395402
Nothing ->
396403
state
397404

398405
Just selfCb ->
399406
{ state
400-
| selfCallbacks = Dict.insert ref selfCb state.selfCallbacks
407+
| selfCallbacks = Dict.insert ( ref, endpoint ) selfCb state.selfCallbacks
401408
}
402409

403410

@@ -633,20 +640,20 @@ onSelfMsg router selfMsg state =
633640
Task.succeed state
634641

635642

636-
handleSelfcallback : Platform.Router msg (Msg msg) -> Endpoint -> Message -> Dict Ref (SelfCallback msg) -> Task x (Dict Ref (SelfCallback msg))
643+
handleSelfcallback : Platform.Router msg (Msg msg) -> Endpoint -> Message -> SelfCallbackDict msg -> Task x (SelfCallbackDict msg)
637644
handleSelfcallback router endpoint message selfCallbacks =
638645
case message.ref of
639646
Nothing ->
640647
Task.succeed selfCallbacks
641648

642649
Just ref ->
643-
case Dict.get ref selfCallbacks of
650+
case Dict.get ( ref, endpoint ) selfCallbacks of
644651
Nothing ->
645652
Task.succeed selfCallbacks
646653

647654
Just selfCb ->
648655
Platform.sendToSelf router (selfCb message)
649-
&> Task.succeed (Dict.remove ref selfCallbacks)
656+
&> Task.succeed (Dict.remove ( ref, endpoint ) selfCallbacks)
650657

651658

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

969976
Just ref ->
970977
insertSocket endpoint (InternalSocket.increaseRef socket) state
971-
|> insertSelfCallback ref maybeSelfCb
978+
|> insertSelfCallback ( ref, endpoint ) maybeSelfCb
972979
|> Task.succeed
973980

974981

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

991998
Just ref ->
992999
insertSocket endpoint (InternalSocket.increaseRef socket) state
993-
|> insertSelfCallback ref selfCb
1000+
|> insertSelfCallback ( ref, endpoint ) selfCb
9941001
|> Task.succeed
9951002
in
9961003
case Dict.get endpoint state.sockets of

src/Phoenix/Presence.elm

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ module Phoenix.Presence exposing (Presence, create, onChange, onJoins, onLeaves,
22

33
{-| Presence is an extension for channels to support the Presence feature of Phoenix.
44
5+
56
# Definition
7+
68
@docs Presence
79
10+
811
# Helpers
9-
@docs init, withPayload, on, onJoin, onRequestJoin, onJoinError, onError, onDisconnect, onRejoin, onLeave, onLeaveError, withDebug, map
10-
@docs init, withPayload, on, onJoin, onRequestJoin, onJoinError, onError, onDisconnect, onRejoin, onLeave, onLeaveError, withDebug, withPresence, map
12+
13+
@docs create, onChange, onJoins, onLeaves, map
14+
1115
-}
1216

1317
import Dict exposing (Dict)
@@ -49,7 +53,6 @@ then an example would be a Dict with
4953
, "user2": [{online_at: 1491492646123}, {online_at: 1491492646624}]
5054
}
5155
52-
5356
-}
5457
onChange : (Dict String (List Value) -> msg) -> PhoenixPresence msg -> PhoenixPresence msg
5558
onChange func presence =

0 commit comments

Comments
 (0)