Allow overriding partial state via hash

This commit is contained in:
Jordan Eldredge 2017-08-28 08:39:42 -07:00
parent 894fb26a50
commit e1ed4d3438
2 changed files with 24 additions and 2 deletions

View file

@ -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;

View file

@ -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;
};