mirror of
https://github.com/captbaritone/webamp.git
synced 2026-07-23 01:57:29 +00:00
I don't know how we'll feasibly solve the multide of potential race conditons and async clean up memory leaks without something like rxjs. So, this is a trial to see the following: 1. How much does it increase bundle size? 2. How effective is typechecking? 3. Does it solve some of the difficult cancelation issues we have?
63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
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, Dispatch, Action, AppState, Middleware } from "./types";
|
|
import { createEpicMiddleware, combineEpics } from "redux-observable";
|
|
import * as Epics from "./epics";
|
|
|
|
// TODO: Move to demo
|
|
const compose = composeWithDevTools({
|
|
actionsBlacklist: [UPDATE_TIME_ELAPSED, STEP_MARQUEE],
|
|
});
|
|
|
|
export default function(
|
|
media: Media,
|
|
actionEmitter: Emitter,
|
|
customMiddlewares: Middleware[] = [],
|
|
stateOverrides: DeepPartial<AppState> | undefined,
|
|
extras: Extras
|
|
) {
|
|
let initialState;
|
|
if (stateOverrides) {
|
|
initialState = merge(
|
|
reducer(undefined, { type: "@@init" }),
|
|
stateOverrides
|
|
);
|
|
}
|
|
|
|
const emitterMiddleware = () => (next: Dispatch) => (action: Action) => {
|
|
actionEmitter.trigger(action.type, action);
|
|
return next(action);
|
|
};
|
|
|
|
const epicMiddleware = createEpicMiddleware({
|
|
dependencies: extras,
|
|
});
|
|
|
|
const enhancer = compose(
|
|
applyMiddleware(
|
|
...[
|
|
thunk.withExtraArgument(extras),
|
|
epicMiddleware,
|
|
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);
|
|
|
|
epicMiddleware.run(combineEpics(...Object.values(Epics)));
|
|
return store;
|
|
}
|