|
| 1 | +effect module BroadcastChannel |
| 2 | + where { command = MyCmd, subscription = MySub } |
| 3 | + exposing |
| 4 | + ( send |
| 5 | + , listen |
| 6 | + ) |
| 7 | + |
| 8 | +{-| BroadcastChannel makes it possible to talk to other browsing contexts with |
| 9 | +the same origin. |
| 10 | +
|
| 11 | +Browsing contexts are windows, tabs, frames, iframes and workers. |
| 12 | +
|
| 13 | +The API here attempts to cover the typical usage scenarios. |
| 14 | +
|
| 15 | +**Note:** This package is heavily inspired by `elm-lang/websocket`. |
| 16 | +Most of its code is reused here. |
| 17 | +
|
| 18 | +# BroadcastChannel |
| 19 | +
|
| 20 | +@docs listen, send |
| 21 | +
|
| 22 | +-} |
| 23 | + |
| 24 | +import Dict |
| 25 | +import Task exposing (Task) |
| 26 | +import BroadcastChannel.LowLevel as BC |
| 27 | + |
| 28 | + |
| 29 | +-- COMMANDS |
| 30 | + |
| 31 | + |
| 32 | +type MyCmd msg |
| 33 | + = Send String String |
| 34 | + |
| 35 | + |
| 36 | +{-| Send a message to a particular channel name. You might say something like this: |
| 37 | +
|
| 38 | + send "user" "logout" |
| 39 | +
|
| 40 | +-} |
| 41 | +send : String -> String -> Cmd msg |
| 42 | +send name message = |
| 43 | + command (Send name message) |
| 44 | + |
| 45 | + |
| 46 | +cmdMap : (a -> b) -> MyCmd a -> MyCmd b |
| 47 | +cmdMap _ (Send url msg) = |
| 48 | + Send url msg |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +-- SUBSCRIPTIONS |
| 53 | + |
| 54 | + |
| 55 | +type MySub msg |
| 56 | + = Listen String (String -> msg) |
| 57 | + |
| 58 | + |
| 59 | +{-| Subscribe to any incoming messages on a broadcast channel. You might say something |
| 60 | +like this: |
| 61 | +
|
| 62 | + type Msg = UserLogout | ... |
| 63 | +
|
| 64 | + subscriptions model = |
| 65 | + listen "user" UserLogout |
| 66 | +
|
| 67 | +Useful if the user logs out in another tab. We can then do something about it |
| 68 | +in this tab. |
| 69 | +
|
| 70 | +-} |
| 71 | +listen : String -> (String -> msg) -> Sub msg |
| 72 | +listen name tagger = |
| 73 | + subscription (Listen name tagger) |
| 74 | + |
| 75 | + |
| 76 | +subMap : (a -> b) -> MySub a -> MySub b |
| 77 | +subMap func sub = |
| 78 | + case sub of |
| 79 | + Listen url tagger -> |
| 80 | + Listen url (tagger >> func) |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | +-- MANAGER |
| 85 | + |
| 86 | + |
| 87 | +type alias State msg = |
| 88 | + { channels : ChannelsDict |
| 89 | + , subs : SubsDict msg |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | +type alias ChannelsDict = |
| 94 | + Dict.Dict String BC.BroadcastChannel |
| 95 | + |
| 96 | + |
| 97 | +type alias SubsDict msg = |
| 98 | + Dict.Dict String (List (String -> msg)) |
| 99 | + |
| 100 | + |
| 101 | +init : Task Never (State msg) |
| 102 | +init = |
| 103 | + Task.succeed (State Dict.empty Dict.empty) |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | +-- HANDLE APP MESSAGES |
| 108 | + |
| 109 | + |
| 110 | +(&>) t1 t2 = |
| 111 | + Task.andThen (\_ -> t2) t1 |
| 112 | + |
| 113 | + |
| 114 | +onEffects : |
| 115 | + Platform.Router msg Msg |
| 116 | + -> List (MyCmd msg) |
| 117 | + -> List (MySub msg) |
| 118 | + -> State msg |
| 119 | + -> Task Never (State msg) |
| 120 | +onEffects router cmds subs state = |
| 121 | + let |
| 122 | + sendMessages = |
| 123 | + sendMessagesHelp cmds state.channels |
| 124 | + |
| 125 | + newSubs = |
| 126 | + buildSubDict subs Dict.empty |
| 127 | + |
| 128 | + cleanup _ = |
| 129 | + let |
| 130 | + newEntries = |
| 131 | + Dict.map (\k v -> []) newSubs |
| 132 | + |
| 133 | + leftStep name _ getNewChannels = |
| 134 | + getNewChannels |
| 135 | + |> Task.andThen |
| 136 | + (\newChannels -> |
| 137 | + open router name |
| 138 | + |> Task.andThen (\channel -> Task.succeed (Dict.insert name channel newChannels)) |
| 139 | + ) |
| 140 | + |
| 141 | + bothStep name _ channel getNewChannels = |
| 142 | + Task.map (Dict.insert name channel) getNewChannels |
| 143 | + |
| 144 | + rightStep name channel getNewChannels = |
| 145 | + close channel &> getNewChannels |
| 146 | + |
| 147 | + collectNewChannels = |
| 148 | + Dict.merge leftStep bothStep rightStep newEntries state.channels (Task.succeed Dict.empty) |
| 149 | + in |
| 150 | + collectNewChannels |
| 151 | + |> Task.andThen (\newChannels -> Task.succeed (State newChannels newSubs)) |
| 152 | + in |
| 153 | + sendMessages |
| 154 | + |> Task.andThen cleanup |
| 155 | + |
| 156 | + |
| 157 | +sendMessagesHelp : List (MyCmd msg) -> ChannelsDict -> Task Never ChannelsDict |
| 158 | +sendMessagesHelp cmds channelsDict = |
| 159 | + case cmds of |
| 160 | + [] -> |
| 161 | + Task.succeed channelsDict |
| 162 | + |
| 163 | + (Send name msg) :: rest -> |
| 164 | + case Dict.get name channelsDict of |
| 165 | + Just channel -> |
| 166 | + BC.send channel msg |
| 167 | + &> sendMessagesHelp rest channelsDict |
| 168 | + |
| 169 | + _ -> |
| 170 | + sendMessagesHelp rest channelsDict |
| 171 | + |
| 172 | + |
| 173 | +buildSubDict : List (MySub msg) -> SubsDict msg -> SubsDict msg |
| 174 | +buildSubDict subs dict = |
| 175 | + case subs of |
| 176 | + [] -> |
| 177 | + dict |
| 178 | + |
| 179 | + (Listen name tagger) :: rest -> |
| 180 | + buildSubDict rest (Dict.update name (add tagger) dict) |
| 181 | + |
| 182 | + |
| 183 | +add : a -> Maybe (List a) -> Maybe (List a) |
| 184 | +add value maybeList = |
| 185 | + case maybeList of |
| 186 | + Nothing -> |
| 187 | + Just [ value ] |
| 188 | + |
| 189 | + Just list -> |
| 190 | + Just (value :: list) |
| 191 | + |
| 192 | + |
| 193 | + |
| 194 | +-- HANDLE SELF MESSAGES |
| 195 | + |
| 196 | + |
| 197 | +type Msg |
| 198 | + = Receive String String |
| 199 | + | Open String BC.BroadcastChannel |
| 200 | + |
| 201 | + |
| 202 | +onSelfMsg : Platform.Router msg Msg -> Msg -> State msg -> Task Never (State msg) |
| 203 | +onSelfMsg router selfMsg state = |
| 204 | + case selfMsg of |
| 205 | + Receive name str -> |
| 206 | + let |
| 207 | + sends = |
| 208 | + Dict.get name state.subs |
| 209 | + |> Maybe.withDefault [] |
| 210 | + |> List.map (\tagger -> Platform.sendToApp router (tagger str)) |
| 211 | + in |
| 212 | + Task.sequence sends &> Task.succeed state |
| 213 | + |
| 214 | + Open name channel -> |
| 215 | + Task.succeed (updateChannel name channel state) |
| 216 | + |
| 217 | + |
| 218 | +updateChannel : String -> BC.BroadcastChannel -> State msg -> State msg |
| 219 | +updateChannel name channel state = |
| 220 | + { state | channels = Dict.insert name channel state.channels } |
| 221 | + |
| 222 | + |
| 223 | +open : Platform.Router msg Msg -> String -> Task Never BC.BroadcastChannel |
| 224 | +open router name = |
| 225 | + let |
| 226 | + doOpen channel = |
| 227 | + Platform.sendToSelf router (Open name channel) |> Task.andThen (\_ -> Task.succeed channel) |
| 228 | + in |
| 229 | + BC.open name |
| 230 | + { onMessage = \_ msg -> Platform.sendToSelf router (Receive name msg) |
| 231 | + } |
| 232 | + |> Task.andThen doOpen |
| 233 | + |
| 234 | + |
| 235 | + |
| 236 | +-- CLOSE CONNECTIONS |
| 237 | + |
| 238 | + |
| 239 | +close : BC.BroadcastChannel -> Task Never () |
| 240 | +close channel = |
| 241 | + BC.close channel |
0 commit comments