Skip to content

Commit

Permalink
[client] Factor websocket connection out into Realtime store
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Prior committed Nov 14, 2021
1 parent 54c4a9b commit 59d07ac
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions client/source/Chat.mint
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
store Realtime {
state shouldConnect = false
state socket : Maybe(WebSocket) = Maybe::Nothing

fun connect {
next {
shouldConnect = true
}
}

fun disconnect {
next {
shouldConnect = false
}
}

fun closeSocket {
next {
socket = Maybe::Nothing
}
}

fun openSocket(socket : WebSocket) {
next {
socket = Maybe::Just(socket)
}
}
}

component Chat {
connect Api exposing { ws }
connect Messages exposing { list, update }
connect Theme exposing { theme }
connect Characters exposing { character }
connect Realtime exposing { shouldConnect, socket }

property room : Room
property roomKey : String
state socket : Maybe(WebSocket) = Maybe::Nothing
state shouldConnect = true

use Provider.WebSocket {
url = "#{ws}/chat/#{room.id}?key=#{roomKey}",
Expand Down Expand Up @@ -47,14 +75,10 @@ component Chat {
}
}
fun handleClose {
next {
socket = Maybe::Nothing
}
Realtime.closeSocket()
}
fun handleOpen(socket : WebSocket) {
next {
socket = Maybe::Just(socket)
}
Realtime.openSocket(socket)
}

style chat {
Expand Down

0 comments on commit 59d07ac

Please sign in to comment.