Consolidate outgoing events

This commit is contained in:
Correl Roush 2020-05-09 02:06:30 -04:00
parent 29174d8246
commit a27394878f
4 changed files with 74 additions and 29 deletions

View file

@ -34,6 +34,8 @@ var app = Elm.Main.init({
app.ports.joinRoom.subscribe(options => { app.ports.joinRoom.subscribe(options => {
let channel = socket.channel("room:" + options.room, {}) let channel = socket.channel("room:" + options.room, {})
// Presence events
let presences = {} let presences = {}
channel.on("presence_state", state => { channel.on("presence_state", state => {
console.log("presence state", state) console.log("presence state", state)
@ -45,21 +47,21 @@ app.ports.joinRoom.subscribe(options => {
presences = Presence.syncDiff(presences, diff) presences = Presence.syncDiff(presences, diff)
app.ports.gotPresence.send(presences) app.ports.gotPresence.send(presences)
}) })
channel.on("vote", vote => {
app.ports.gotVote.send(vote) // Incoming room events
const handle = (type) => (data) =>
app.ports.roomEvents.send({
"type": type,
"data": data
}) })
channel.on("reset", reset => { ["vote", "reset"]
app.ports.gotReset.send(reset) .forEach(event => channel.on(event, handle(event)))
})
app.ports.newProfile.subscribe(profile => { // Outgoing room events
channel.push("new_profile", { "name": profile.playerName }) app.ports.roomActions.subscribe(action => {
}) channel.push(action.type, action.data)
app.ports.vote.subscribe(value => {
channel.push("vote", value)
})
app.ports.reset.subscribe(_ => {
channel.push("reset", null)
}) })
channel.join() channel.join()
.receive("ok", resp => { .receive("ok", resp => {
console.log("Joined successfully", resp); console.log("Joined successfully", resp);

View file

@ -3,34 +3,79 @@ port module PlanningPokerAPI exposing
, gotReset , gotReset
, gotVote , gotVote
, joinRoom , joinRoom
, joinedRoom
, newProfile , newProfile
, reset , reset
, reveal
, vote , vote
) )
import Json.Decode exposing (Value) import Json.Decode as Decode
import Json.Encode as Encode
type RoomAction
= NewProfile { playerName : String }
| Vote String
| ResetGame
| RevealVotes
port joinRoom : { room : String } -> Cmd msg port joinRoom : { room : String } -> Cmd msg
port newProfile : { playerName : String } -> Cmd msg port roomActions : Encode.Value -> Cmd msg
port vote : String -> Cmd msg newProfile : { playerName : String } -> Cmd msg
newProfile =
NewProfile >> encodeAction >> roomActions
port reset : () -> Cmd msg vote : String -> Cmd msg
vote =
Vote >> encodeAction >> roomActions
port joinedRoom : (String -> msg) -> Sub msg reset : Cmd msg
reset =
ResetGame |> encodeAction >> roomActions
port gotPresence : (Value -> msg) -> Sub msg reveal : Cmd msg
reveal =
RevealVotes |> encodeAction >> roomActions
port gotVote : (Value -> msg) -> Sub msg encodeAction : RoomAction -> Encode.Value
encodeAction action =
let
wrap : String -> Encode.Value -> Encode.Value
wrap name data =
Encode.object
[ ( "type", Encode.string name )
, ( "data", data )
]
in
case action of
NewProfile { playerName } ->
wrap "new_profile"
(Encode.object [ ( "name", Encode.string playerName ) ])
Vote value ->
wrap "vote"
(Encode.object [ ( "value", Encode.string value ) ])
ResetGame ->
wrap "reset" (Encode.object [])
RevealVotes ->
wrap "reveal" (Encode.object [])
port gotReset : (Value -> msg) -> Sub msg port gotPresence : (Decode.Value -> msg) -> Sub msg
port gotVote : (Decode.Value -> msg) -> Sub msg
port gotReset : (Decode.Value -> msg) -> Sub msg

View file

@ -121,12 +121,12 @@ update key msg model =
Reveal -> Reveal ->
( { model | showVotes = True } ( { model | showVotes = True }
, Cmd.none , API.reveal
) )
Reset -> Reset ->
( model ( model
, API.reset () , API.reset
) )
PlayerNameChanged newName -> PlayerNameChanged newName ->

View file

@ -6,11 +6,9 @@ defmodule PlanningpokerWeb.RoomChannel do
def join("room:" <> room_id, params, socket) do def join("room:" <> room_id, params, socket) do
send(self(), :after_join) send(self(), :after_join)
Logger.debug "Proc: #{inspect self()}, Socket: #{inspect socket}"
{:ok, %{channel: room_id, topic: "Planning Poker"}, {:ok, %{channel: room_id, topic: "Planning Poker"},
socket socket
|> assign(:room_id, room_id) |> assign(:room_id, room_id)}
|> assign(:player_name, params["playerName"])}
end end
def handle_info(:after_join, socket) do def handle_info(:after_join, socket) do
push(socket, "presence_state", Presence.list(socket)) push(socket, "presence_state", Presence.list(socket))
@ -28,7 +26,7 @@ defmodule PlanningpokerWeb.RoomChannel do
) )
{:noreply, socket} {:noreply, socket}
end end
def handle_in("vote", value, socket) do def handle_in("vote", %{"value" => value}, socket) do
Db.save_vote( Db.save_vote(
socket.assigns.player_id, socket.assigns.player_id,
socket.assigns.room_id, socket.assigns.room_id,