diff --git a/src/broadcast/actions.ts b/src/broadcast/actions.ts new file mode 100644 index 00000000..0178149a --- /dev/null +++ b/src/broadcast/actions.ts @@ -0,0 +1,8 @@ +import { createAction } from "@reduxjs/toolkit"; + +export const REGISTERED = createAction<{ connID: number }>( + "Broadcast/REGISTERING" +); +export const CONNECTED = createAction("Broadcast/CONNECTED"); +export const GONE_LIVE = createAction("Broadcast/GONE_LIVE"); +export const DISCONNECTED = createAction("Broadcast/DISCONNECTED"); diff --git a/src/broadcast/state.ts b/src/broadcast/state.ts index e4a94acb..bdb23635 100644 --- a/src/broadcast/state.ts +++ b/src/broadcast/state.ts @@ -8,6 +8,7 @@ import { ConnectionStateEnum } from "./streamer"; import { RecordingStreamer } from "./recording_streamer"; import { audioEngine } from "../mixer/audio"; import { setItemPlayed } from "../showplanner/state"; +import { CONNECTED, DISCONNECTED, GONE_LIVE, REGISTERED } from "./actions"; export let streamer: WebRTCStreamer | null = null; @@ -62,14 +63,6 @@ const broadcastState = createSlice({ setTracklisting(state, action: PayloadAction) { state.liveForThePurposesOfTracklisting = action.payload; }, - setConnID(state, action: PayloadAction) { - state.connID = action.payload; - if (action.payload != null) { - state.stage = "REGISTERED"; - } else { - state.stage = "NOT_REGISTERED"; - } - }, setWsID(state, action: PayloadAction) { state.wsID = action.payload; }, @@ -80,6 +73,11 @@ const broadcastState = createSlice({ state.recordingState = action.payload; }, }, + extraReducers: (builder) => + builder.addCase(REGISTERED, (state, action) => { + state.connID = action.payload.connID; + state.stage = "REGISTERED"; + }), }); export default broadcastState.reducer; @@ -133,7 +131,7 @@ export const registerForShow = (): AppThunk => async (dispatch, getState) => { ); console.log(connID); if (connID !== undefined) { - dispatch(broadcastState.actions.setConnID(connID["connid"])); + dispatch(REGISTERED({ connID })); } } catch (e) { if (e instanceof ApiException) { @@ -160,9 +158,9 @@ export const cancelTimeslot = (): AppThunk => async (dispatch, getState) => { console.log("Attempting to Cancel Broadcast."); try { var response = await sendBroadcastCancel(getState().broadcast.connID); - dispatch(stopStreaming()); + await stopStreaming(); if (response != null) { - dispatch(broadcastState.actions.setConnID(null)); + dispatch(DISCONNECTED()); } } catch (e) { if (e instanceof ApiException) { @@ -181,7 +179,7 @@ export const cancelTimeslot = (): AppThunk => async (dispatch, getState) => { } }; -export const changeTimeslot = (): AppThunk => async (dispatch, getState) => { +export const changeTimeslot = (): AppThunk => async (_, getState) => { var state = getState().broadcast; if (state.stage === "REGISTERED") { console.log("Attempting to Change Broadcast Options."); @@ -309,21 +307,21 @@ export const goOnAir = (): AppThunk => async (dispatch, getState) => { dispatch ); streamer.addConnectionStateListener((state) => { - dispatch(broadcastState.actions.setConnectionState(state)); + dispatch(CONNECTED); if (state === "CONNECTION_LOST") { // un-register if we drop, let the user manually reconnect - dispatch(broadcastState.actions.setConnID(null)); + dispatch(DISCONNECTED); } else if (state === "CONNECTED") { // okay, we've connected dispatch(registerForShow()); } else if (state === "LIVE") { - dispatch(setItemPlayed({ itemId: "all", played: false })); + dispatch(GONE_LIVE); } }); await streamer.start(); }; -export const stopStreaming = (): AppThunk => async (dispatch) => { +export const stopStreaming = async () => { if (streamer) { await streamer.stop("stopStreaming call"); streamer = null; diff --git a/src/showplanner/state.ts b/src/showplanner/state.ts index 8b9b8fb6..6a8a404f 100644 --- a/src/showplanner/state.ts +++ b/src/showplanner/state.ts @@ -4,6 +4,7 @@ import { createSlice, PayloadAction } from "@reduxjs/toolkit"; import { AppThunk } from "../store"; import { cloneDeep } from "lodash"; import raygun from "raygun4js"; +import { GONE_LIVE } from "../broadcast/actions"; export interface ItemGhost { type: "ghost"; @@ -216,6 +217,12 @@ const showplan = createSlice({ state.auxPlaylists = state.auxPlaylists.concat(action.payload); }, }, + extraReducers: (builder) => + builder.addCase(GONE_LIVE, (state, action) => { + state.plan!.forEach((x) => { + x.played = true; + }); + }), }); export default showplan.reducer;