mirror of
https://github.com/captbaritone/webamp.git
synced 2026-07-23 01:57:29 +00:00
Type the Redux store
This commit is contained in:
parent
08f415c829
commit
2d1cddedb6
2 changed files with 56 additions and 48 deletions
48
js/store.js
48
js/store.js
|
|
@ -1,48 +0,0 @@
|
|||
import { createStore, applyMiddleware } from "redux";
|
||||
import thunk from "redux-thunk";
|
||||
import { composeWithDevTools } from "redux-devtools-extension";
|
||||
import reducer from "./reducers";
|
||||
import mediaMiddleware from "./mediaMiddleware";
|
||||
import { merge } from "./utils";
|
||||
import { UPDATE_TIME_ELAPSED, STEP_MARQUEE } from "./actionTypes";
|
||||
|
||||
const compose = composeWithDevTools({
|
||||
actionsBlacklist: [UPDATE_TIME_ELAPSED, STEP_MARQUEE]
|
||||
});
|
||||
|
||||
export default function(
|
||||
media,
|
||||
actionEmitter,
|
||||
customMiddlewares = [],
|
||||
stateOverrides,
|
||||
extras
|
||||
) {
|
||||
let initialState;
|
||||
if (stateOverrides) {
|
||||
initialState = merge(
|
||||
reducer(undefined, { type: "@@init" }),
|
||||
stateOverrides
|
||||
);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
const emitterMiddleware = store => next => action => {
|
||||
actionEmitter.trigger(action.type, action);
|
||||
return next(action);
|
||||
};
|
||||
|
||||
return createStore(
|
||||
reducer,
|
||||
initialState,
|
||||
compose(
|
||||
applyMiddleware(
|
||||
...[
|
||||
thunk.withExtraArgument(extras),
|
||||
mediaMiddleware(media),
|
||||
emitterMiddleware,
|
||||
...customMiddlewares
|
||||
].filter(Boolean)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
56
js/store.ts
Normal file
56
js/store.ts
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
import { createStore, applyMiddleware, DeepPartial } from "redux";
|
||||
import thunk from "redux-thunk";
|
||||
import { composeWithDevTools } from "redux-devtools-extension";
|
||||
import reducer from "./reducers";
|
||||
import mediaMiddleware from "./mediaMiddleware";
|
||||
import { merge } from "./utils";
|
||||
import { UPDATE_TIME_ELAPSED, STEP_MARQUEE } from "./actionTypes";
|
||||
import Media from "./media";
|
||||
import Emitter from "./emitter";
|
||||
import { Extras, MiddlewareStore, Dispatch, Action, AppState } from "./types";
|
||||
|
||||
const compose = composeWithDevTools({
|
||||
actionsBlacklist: [UPDATE_TIME_ELAPSED, STEP_MARQUEE]
|
||||
});
|
||||
|
||||
export default function(
|
||||
media: Media,
|
||||
actionEmitter: Emitter,
|
||||
customMiddlewares = [],
|
||||
stateOverrides: DeepPartial<AppState>,
|
||||
extras: Extras
|
||||
) {
|
||||
let initialState;
|
||||
if (stateOverrides) {
|
||||
initialState = merge(
|
||||
reducer(undefined, { type: "@@init" }),
|
||||
stateOverrides
|
||||
);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
const emitterMiddleware = (store: MiddlewareStore) => (next: Dispatch) => (
|
||||
action: Action
|
||||
) => {
|
||||
actionEmitter.trigger(action.type, action);
|
||||
return next(action);
|
||||
};
|
||||
|
||||
const enhancer = compose(
|
||||
applyMiddleware(
|
||||
...[
|
||||
thunk.withExtraArgument(extras),
|
||||
mediaMiddleware(media),
|
||||
emitterMiddleware,
|
||||
...customMiddlewares
|
||||
].filter(Boolean)
|
||||
)
|
||||
);
|
||||
|
||||
// The Redux types are a bit confused, and don't realize that passing an
|
||||
// undefined initialState is allowed.
|
||||
const store = initialState
|
||||
? createStore(reducer, initialState, enhancer)
|
||||
: createStore(reducer, enhancer);
|
||||
return store;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue