From 5b6603cce95c7a8df37e14f103e5ef1d0f3ac2e4 Mon Sep 17 00:00:00 2001 From: Jordan Eldredge Date: Tue, 11 Sep 2018 07:06:25 -0700 Subject: [PATCH] Stub out ability to serialize state --- js/actionCreators/index.js | 22 +++++++++++++++++++++- js/actionTypes.js | 1 + js/constants.js | 2 ++ js/selectors.js | 9 ++++++++- js/webampLazy.js | 14 +++++++++++--- 5 files changed, 43 insertions(+), 5 deletions(-) diff --git a/js/actionCreators/index.js b/js/actionCreators/index.js index 333a7ea4cb..1918135b50 100644 --- a/js/actionCreators/index.js +++ b/js/actionCreators/index.js @@ -1,4 +1,10 @@ -import { CLOSE_WINAMP, STOP, TOGGLE_VISUALIZER_STYLE } from "../actionTypes"; +import { + CLOSE_WINAMP, + STOP, + TOGGLE_VISUALIZER_STYLE, + RESTORE_SERIALIZED_STATE +} from "../actionTypes"; +import { SERIALIZATION_VERSION } from "../constants"; export { toggleDoubleSizeMode, @@ -78,3 +84,17 @@ export function close() { export function toggleVisualizerStyle() { return { type: TOGGLE_VISUALIZER_STYLE }; } + +export function restoreSerializedState(serializedState) { + if (serializedState.version !== SERIALIZATION_VERSION) { + // When SERIALIZATION_VERSION changes, will need to provide mapper functions + // to transform old versions to new versions here. + // + // Having types will make doing this translation much easier. + // + // For now we will throw as reminder of how important it is to remember to + // do this translation. + throw new Error("Invalid serializaiton version"); + } + return { type: RESTORE_SERIALIZED_STATE, serializedState }; +} diff --git a/js/actionTypes.js b/js/actionTypes.js index 98243293cb..82823e8f30 100644 --- a/js/actionTypes.js +++ b/js/actionTypes.js @@ -70,3 +70,4 @@ export const DISABLE_MARQUEE = "DISABLE_MARQUEE"; export const SET_DUMMY_VIZ_DATA = "SET_DUMMY_VIZ_DATA"; export const SET_WINDOW_VISIBILITY = "SET_WINDOW_VISIBILITY"; export const LOADING = "LOADING"; +export const RESTORE_SERIALIZED_STATE = "RESTORE_SERIALIZED_STATE"; diff --git a/js/constants.js b/js/constants.js index ff2ffcbb33..42721bca94 100644 --- a/js/constants.js +++ b/js/constants.js @@ -52,3 +52,5 @@ export const MEDIA_STATUS = { STOPPED: "STOPPED", PAUSED: "PAUSED" }; + +export const SERIALIZATION_VERSION = 1; diff --git a/js/selectors.js b/js/selectors.js index 4adf00d165..e7c0bc2ec4 100644 --- a/js/selectors.js +++ b/js/selectors.js @@ -11,7 +11,8 @@ import { TRACK_HEIGHT, WINDOW_RESIZE_SEGMENT_WIDTH, WINDOW_RESIZE_SEGMENT_HEIGHT, - WINDOW_WIDTH + WINDOW_WIDTH, + SERIALIZATION_VERSION } from "./constants"; import { createPlaylistURL } from "./playlistHtml"; import * as fromPlaylist from "./reducers/playlist"; @@ -329,3 +330,9 @@ export const getSkinPlaylistStyle = state => { export const getVisualizerStyle = state => fromDisplay.getVisualizerStyle(state.display); + +export const serialize = state => { + return { + version: SERIALIZATION_VERSION + }; +}; diff --git a/js/webampLazy.js b/js/webampLazy.js index 0fb88f085e..1eeb0ee1b7 100644 --- a/js/webampLazy.js +++ b/js/webampLazy.js @@ -6,7 +6,7 @@ import getStore from "./store"; import App from "./components/App"; import Hotkeys from "./hotkeys"; import Media from "./media"; -import { getTrackCount, getTracks } from "./selectors"; +import * as Selectors from "./selectors"; import { setSkinFromUrl, loadMediaFiles, @@ -173,7 +173,7 @@ class Winamp { // Append this array of tracks to the end of the current playlist. appendTracks(tracks) { - const nextIndex = getTrackCount(this.store.getState()); + const nextIndex = Selectors.getTrackCount(this.store.getState()); this.store.dispatch(loadMediaFiles(tracks, LOAD_STYLE.BUFFER, nextIndex)); } @@ -186,9 +186,17 @@ class Winamp { return this._actionEmitter.on(CLOSE_WINAMP, cb); } + serialize() { + return Selectors.serialize(this.store.getState()); + } + + restoreSerializedState(serializedState) { + this.store.dispatch(this.restoreSerializedState(serializedState)); + } + onTrackDidChange(cb) { return this._actionEmitter.on(SET_MEDIA, action => { - const tracks = getTracks(this.store.getState()); + const tracks = Selectors.getTracks(this.store.getState()); const track = tracks[action.id]; if (track == null) { return;