From 397cba3c2655d91962952168bda892e1585f5635 Mon Sep 17 00:00:00 2001 From: Marks Polakovs Date: Tue, 27 Oct 2020 12:45:11 +0000 Subject: [PATCH 1/2] Trial run of redux refactoring --- src/broadcast/actions.ts | 8 ++++++++ src/broadcast/state.ts | 22 ++++++++++++++-------- src/showplanner/state.ts | 7 +++++++ 3 files changed, 29 insertions(+), 8 deletions(-) create mode 100644 src/broadcast/actions.ts 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..f007b6ef 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; @@ -80,6 +81,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 +139,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 +166,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 +187,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 +315,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; From 12f55a5f9a10046e64d7c56ffdf6fad12d15a814 Mon Sep 17 00:00:00 2001 From: Marks Polakovs Date: Tue, 27 Oct 2020 12:47:49 +0000 Subject: [PATCH 2/2] Remove unused action --- src/broadcast/state.ts | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/broadcast/state.ts b/src/broadcast/state.ts index f007b6ef..bdb23635 100644 --- a/src/broadcast/state.ts +++ b/src/broadcast/state.ts @@ -63,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; },