studiox-streamer/common/state.mjs

71 lines
1.5 KiB
JavaScript

export const STATE_RESET = 'state-reset'
export const STREAM_STATUS = 'stream-status'
export const LOG_MESSAGE = 'log-message'
export const PING = 'ping'
export const METER = 'meter'
export const CONNECTION_STATE = 'connection-state'
export const CONNECTIVITY = 'connectivity'
export function action (action, data = {}) {
return { action, data }
}
export const INITIAL_STATE = {
connected: false,
streams: {},
log: {},
pings: 0,
meter: {},
connectionState: { connected: false, error: null },
connectivity: { internet: false },
}
export function reducer (state, event) {
const { action, data } = event
if (action === STATE_RESET) {
return { ...INITIAL_STATE, ...data }
}
if (action === PING) {
return {
...state,
ping: Date.now()
}
}
if (action === STREAM_STATUS) {
const { id, status, url } = data
return {
...state,
streams: {
...(state.streams || {}),
[id]: {
...state.streams[id],
status,
url
}
}
}
}
if (action === LOG_MESSAGE) {
const { id, type, message } = data
return {
...state,
log: {
...state.log,
[id]: [
...(state.log[id] || []),
{ type, message }
]
}
}
}
if (action === METER) {
return { ...state, meter: data }
}
if (action === CONNECTION_STATE) {
return { ...state, connectionState: data }
}
if (action === CONNECTIVITY) {
return { ...state, connectivity: data }
}
return state
}