From e1ed4d34387d34a06a7c92125d0e2d4acc31cbae Mon Sep 17 00:00:00 2001 From: Jordan Eldredge Date: Mon, 28 Aug 2017 08:39:42 -0700 Subject: [PATCH] Allow overriding partial state via hash --- js/store.js | 13 +++++++++++-- js/utils.js | 13 +++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/js/store.js b/js/store.js index efe97f60..ee3b33a7 100644 --- a/js/store.js +++ b/js/store.js @@ -3,12 +3,21 @@ import reducer from "./reducers"; import thunk from "redux-thunk"; import { composeWithDevTools } from "redux-devtools-extension"; import mediaMiddleware from "./mediaMiddleware"; +import { merge } from "./utils"; -const getStore = (winamp, initialState) => - createStore( +const getStore = (winamp, stateOverrides) => { + let initialState; + if (stateOverrides) { + initialState = merge( + reducer(undefined, { type: "@@init" }), + stateOverrides + ); + } + return createStore( reducer, initialState, composeWithDevTools(applyMiddleware(thunk, mediaMiddleware(winamp.media))) ); +}; export default getStore; diff --git a/js/utils.js b/js/utils.js index f94c763e..a704f37a 100644 --- a/js/utils.js +++ b/js/utils.js @@ -78,3 +78,16 @@ export const normalize = rebound(1, 64, 1, 100); // Convert a 0-100 to an .eqf value export const denormalize = rebound(1, 100, 1, 64); + +// Merge a `source` object to a `target` recursively +export const merge = (target, source) => { + // Iterate through `source` properties and if an `Object` set property to merge of `target` and `source` properties + for (const key of Object.keys(source)) { + if (source[key] instanceof Object) + Object.assign(source[key], merge(target[key], source[key])); + } + + // Join `target` and modified `source` + Object.assign(target || {}, source); + return target; +};