import { combineReducers } from 'redux' import moment from 'moment' let FileSaver = require('file-saver') const liveInitialState = { loading: false, error: null, opt: {}, checkpoints: [], epochs: ['latest'], sequences: [], fps: 0, frame: { i: 0, sequence_i: 0, sequence_len: '1' } } const liveReducer = (state = liveInitialState, action) => { let results; switch(action.type) { case 'LOAD_PARAMS': if (! action.opt || ! Object.keys(action.opt).length) { return state } return { ...state, loading: false, error: null, opt: action.opt, } case 'SET_PARAM': return { ...state, opt: { ...state.opt, [action.key]: action.value, } } case 'LIST_CHECKPOINTS': return { ...state, checkpoints: action.checkpoints, epochs: [], } case 'LIST_EPOCHS': return { ...state, epochs: (action.epochs || []).map(a => [ a == 'latest' ? Infinity : a, a ]) .sort((a,b) => a[0] - b[0]) .map(a => a[1]) } case 'LIST_SEQUENCES': return { ...state, sequences: action.sequences, } case 'SET_FPS': return { ...state, fps: action.fps, } case 'CURRENT_FRAME': return action.meta ? { ...state, frame: action.meta } : state case 'START_RECORDING': return { ...state, opt: { ...state.opt, recording: true, } } case 'ADD_RECORD_FRAME': return { ...state, opt: { ...state.opt, recordFrames: (state.opt.recordFrames || 0) + 1, } } case 'SAVE_FRAME': FileSaver.saveAs( action.blob, state.opt.checkpoint_name + "_" + state.opt.sequence + "_" + moment().format("YYYYMMDD_HHmm") + ".png" ) return state case 'SAVE_VIDEO': FileSaver.saveAs( action.blob, state.opt.checkpoint_name + "_" + state.opt.sequence + "_" + moment().format("YYYYMMDD_HHmm") + ".webm" ) return { ...state, opt: { ...state.opt, recording: false, } } default: return state } } export default liveReducer