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

View file

@ -3,34 +3,79 @@ port module PlanningPokerAPI exposing
, gotReset
, gotVote
, joinRoom
, joinedRoom
, newProfile
, reset
, reveal
, 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 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 ->
( { model | showVotes = True }
, Cmd.none
, API.reveal
)
Reset ->
( model
, API.reset ()
, API.reset
)
PlayerNameChanged newName ->

View file

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