import moment from 'moment/min/moment.min' import FileSaver from 'file-saver' import types from '../types' const liveInitialState = { loading: false, error: null, opt: { hue: 0, saturation: 0, luminosity: 0, sequence_playing: true, sequence_step: 1, recurse_roll: 0, rotate: 0, scale: 0, process_frac: 0.5, view_mode: 'b', }, all_checkpoints: [], checkpoints: [], epochs: ['latest'], sequences: [], fps: 0, playing: false, last_message: '', fullscreen: false, frame: { i: 0, sequence_i: 0, sequence_len: '1' } } const liveReducer = (state = liveInitialState, action) => { let results; switch(action.type) { case types.socket.load_params: if (! action.opt || ! Object.keys(action.opt).length) { return state } return { ...state, loading: false, error: null, opt: action.opt, } case types.player.set_param: return { ...state, opt: { ...state.opt, [action.key]: action.value, } } case types.player.set_fullscreen: return { ...state, fullscreen: action.value, } case types.socket.list_checkpoints: return { ...state, checkpoints: action.checkpoints, epochs: [], } case types.socket.list_all_checkpoints: return { ...state, all_checkpoints: action.all_checkpoints, epochs: [], } case types.socket.list_epochs: console.log(action) if (action.epochs === "not found") return { ...state, 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 types.socket.list_sequences: return { ...state, sequences: action.sequences, } case types.player.loading_sequence: return { ...state, opt: { ...state.opt, sequence_name: action.sequence_name, } } case types.player.loading_checkpoint: return { ...state, opt: { ...state.opt, checkpoint_name: action.checkpoint_name, epoch: action.epoch, } } case types.player.set_fps: return { ...state, fps: action.fps, } case types.system.stdout: if (action.data.processor === 'gpu') { return { ...state, last_message: action.data.data, } } return state case types.player.current_frame: return action.meta ? { ...state, frame: action.meta } : state case types.player.pausing: return { ...state, playing: false, } case types.player.playing: return { ...state, playing: true, } case types.player.start_recording: return { ...state, opt: { ...state.opt, recording: true, } } case types.player.add_record_frame: return { ...state, opt: { ...state.opt, recordFrames: (state.opt.recordFrames || 0) + 1, } } case types.player.save_frame: FileSaver.saveAs( action.blob, state.opt.checkpoint_name + "_" + state.opt.sequence + "_" + moment().format("YYYYMMDD_HHmm") + ".png" ) return state case types.player.saving_video: return { ...state, opt: { ...state.opt, savingVideo: true, } } case types.player.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, savingVideo: false, recordFrames: 0, } } default: return state } } export default liveReducer