mirror of
https://github.com/captbaritone/webamp.git
synced 2026-07-28 12:36:35 +00:00
Allow overriding partial state via hash
This commit is contained in:
parent
894fb26a50
commit
e1ed4d3438
2 changed files with 24 additions and 2 deletions
13
js/store.js
13
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;
|
||||
|
|
|
|||
13
js/utils.js
13
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;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue