Skip to content

[WIP] Ability to serialize state #656

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion js/actionCreators/index.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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 };
}
1 change: 1 addition & 0 deletions js/actionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
2 changes: 2 additions & 0 deletions js/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,5 @@ export const MEDIA_STATUS = {
STOPPED: "STOPPED",
PAUSED: "PAUSED"
};

export const SERIALIZATION_VERSION = 1;
9 changes: 8 additions & 1 deletion js/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -329,3 +330,9 @@ export const getSkinPlaylistStyle = state => {

export const getVisualizerStyle = state =>
fromDisplay.getVisualizerStyle(state.display);

export const serialize = state => {
return {
version: SERIALIZATION_VERSION
};
};
14 changes: 11 additions & 3 deletions js/webampLazy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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));
}

Expand All @@ -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;
Expand Down