initial commit

This commit is contained in:
Franz Heinzmann (Frando) 2021-06-07 16:22:13 +02:00
commit 0024f44c80
34 changed files with 1991 additions and 0 deletions

70
common/state.mjs Normal file
View file

@ -0,0 +1,70 @@
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 } = data
return {
...state,
streams: {
...(state.streams || {}),
[id]: {
...state.streams[id],
status
}
}
}
}
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
}