| 1234567891011121314151617181920212223242526272829303132333435363738394041 |
- 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<GlobalState, 'player' | 'myClientName'>): boolean =>
- state.player.master === state.myClientName;
- export const isActiveClient = (state: Pick<GlobalState, 'player' | 'myClientName'>): boolean =>
- isMaster(state) || state.player.activeClients.includes(state.myClientName);
- export const isFromOurselves = (
- state: Pick<GlobalState, 'myClientName'>,
- action: ActionRemote,
- ): boolean => state.myClientName === action.fromClient;
- export const willBeMaster = (
- state: Partial<GlobalState> & Pick<GlobalState, 'myClientName'>,
- 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<GlobalState, 'player'>): number | null => state.player.songId;
|