Farmbot-Web-App/frontend/redux/middlewares.ts

52 lines
1.8 KiB
TypeScript
Raw Normal View History

2017-06-29 12:54:02 -06:00
import thunk from "redux-thunk";
2018-03-19 14:01:05 -06:00
import { applyMiddleware, compose } from "redux";
import { EnvName, ReduxAction } from "./interfaces";
import { Actions } from "../constants";
2018-01-15 10:50:44 -07:00
import { stateFetchMiddlewareConfig } from "./state_fetch_middleware";
2018-01-23 13:57:37 -07:00
import { revertToEnglishMiddleware } from "./revert_to_english_middleware";
2018-03-19 13:06:31 -06:00
import { versionChangeMiddleware } from "./version_tracker_middleware";
2018-03-19 14:01:05 -06:00
import { Everything } from "../interfaces";
import { Middleware } from "redux";
import { Store } from "redux";
2018-04-06 13:26:55 -06:00
import { refilterLogsMiddleware } from "./refilter_logs_middleware";
2018-03-19 14:01:05 -06:00
export interface MW extends Middleware {
(store: Store<Everything>):
(dispatch: Function) =>
(action: ReduxAction<object>) => void;
}
2017-06-29 12:54:02 -06:00
2018-03-23 09:44:06 -06:00
export interface MiddlewareConfig { fn: MW; env: EnvName; }
2017-06-29 12:54:02 -06:00
/** To make it easier to manage all things watching the state tree,
* we keep subscriber functions in this array. */
export const mwConfig: MiddlewareConfig[] = [
{ env: "*", fn: thunk },
{ env: "development", fn: require("redux-immutable-state-invariant").default() },
stateFetchMiddlewareConfig,
2018-03-19 13:06:31 -06:00
revertToEnglishMiddleware,
2018-04-06 13:26:55 -06:00
versionChangeMiddleware,
refilterLogsMiddleware
2017-06-29 12:54:02 -06:00
];
export function getMiddleware(env: EnvName) {
2017-08-28 05:49:13 -06:00
const middlewareFns = mwConfig
2017-06-29 12:54:02 -06:00
.filter(function (mwc) { return (mwc.env === env) || (mwc.env === "*"); })
.map((mwc) => mwc.fn);
// tslint:disable-next-line:no-any
const wow = (window as any).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__;
2020-01-03 13:04:45 -07:00
const dtCompose = wow?.({
actionsBlacklist: [
Actions.NETWORK_EDGE_CHANGE,
Actions.PING_NO,
Actions.PING_OK,
Actions.PING_START,
Actions.RESOURCE_READY
]
});
2017-06-29 12:54:02 -06:00
const composeEnhancers = dtCompose || compose;
const middleware = applyMiddleware(...middlewareFns);
2017-06-29 12:54:02 -06:00
return composeEnhancers(middleware);
2017-06-29 12:54:02 -06:00
}