import { ActionRemote, ActionStateSetLocal, ActionStateSetRemote } from './actions'; import { GlobalState } from './reducer/types'; import { MusicPlayer } from './types'; export function getNextPlayerStateFromAction( player: MusicPlayer | undefined, action: ActionStateSetLocal | ActionStateSetRemote | null, ): MusicPlayer | null { if (!(action && player)) { return null; } if (typeof action.payload === 'function') { return { ...player, ...action.payload(player) }; } return { ...player, ...action.payload }; } export const isMaster = (state: Pick): boolean => state.player.master === state.myClientName; export const isActiveClient = (state: Pick): boolean => isMaster(state) || state.player.activeClients.includes(state.myClientName); export const isFromOurselves = ( state: Pick, action: ActionRemote, ): boolean => state.myClientName === action.fromClient; export const willBeMaster = ( state: Partial & Pick, action: ActionStateSetLocal | ActionStateSetRemote, ): boolean => { const actionHasMaster = typeof action.payload === 'function' ? !!action.payload({}).master : !!action.payload?.master; return ( actionHasMaster && state.myClientName === getNextPlayerStateFromAction(state.player, action)?.master ); }; export const getSongId = (state: Pick): number | null => state.player.songId;