2017-06-15 11:27:23 +07:00
|
|
|
import { createLogger } from "redux-logger";
|
|
|
|
import appReducer from "reducers/app";
|
|
|
|
import availabilityReducer from "reducers/availability";
|
|
|
|
import claimsReducer from "reducers/claims";
|
|
|
|
import contentReducer from "reducers/content";
|
|
|
|
import costInfoReducer from "reducers/cost_info";
|
|
|
|
import fileInfoReducer from "reducers/file_info";
|
2017-08-30 08:48:32 -04:00
|
|
|
import navigationReducer from "reducers/navigation";
|
2017-06-15 11:27:23 +07:00
|
|
|
import rewardsReducer from "reducers/rewards";
|
|
|
|
import searchReducer from "reducers/search";
|
|
|
|
import settingsReducer from "reducers/settings";
|
|
|
|
import userReducer from "reducers/user";
|
|
|
|
import walletReducer from "reducers/wallet";
|
|
|
|
import { persistStore, autoRehydrate } from "redux-persist";
|
|
|
|
import createCompressor from "redux-persist-transform-compress";
|
|
|
|
import createFilter from "redux-persist-transform-filter";
|
2017-08-30 08:48:32 -04:00
|
|
|
//import { REHYDRATE } from "redux-persist/constants";
|
|
|
|
//import createActionBuffer from "redux-action-buffer";
|
2017-04-07 12:15:22 +07:00
|
|
|
|
2017-06-15 11:27:23 +07:00
|
|
|
const localForage = require("localforage");
|
|
|
|
const redux = require("redux");
|
|
|
|
const thunk = require("redux-thunk").default;
|
|
|
|
const env = ENV;
|
2017-04-07 12:15:22 +07:00
|
|
|
|
|
|
|
function isFunction(object) {
|
2017-06-15 11:27:23 +07:00
|
|
|
return typeof object === "function";
|
2017-04-07 12:15:22 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
function isNotFunction(object) {
|
2017-06-15 11:27:23 +07:00
|
|
|
return !isFunction(object);
|
2017-04-07 12:15:22 +07:00
|
|
|
}
|
|
|
|
|
2017-04-27 00:08:26 +07:00
|
|
|
function createBulkThunkMiddleware() {
|
2017-06-15 11:27:23 +07:00
|
|
|
return ({ dispatch, getState }) => next => action => {
|
|
|
|
if (action.type === "BATCH_ACTIONS") {
|
|
|
|
action.actions
|
|
|
|
.filter(isFunction)
|
|
|
|
.map(actionFn => actionFn(dispatch, getState));
|
|
|
|
}
|
|
|
|
return next(action);
|
|
|
|
};
|
2017-04-27 00:08:26 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
function enableBatching(reducer) {
|
2017-06-15 11:27:23 +07:00
|
|
|
return function batchingReducer(state, action) {
|
|
|
|
switch (action.type) {
|
|
|
|
case "BATCH_ACTIONS":
|
|
|
|
return action.actions
|
|
|
|
.filter(isNotFunction)
|
|
|
|
.reduce(batchingReducer, state);
|
|
|
|
default:
|
|
|
|
return reducer(state, action);
|
|
|
|
}
|
|
|
|
};
|
2017-04-27 00:08:26 +07:00
|
|
|
}
|
|
|
|
|
2017-04-07 12:15:22 +07:00
|
|
|
const reducers = redux.combineReducers({
|
2017-06-01 12:20:12 -04:00
|
|
|
app: appReducer,
|
2017-08-30 08:48:32 -04:00
|
|
|
navigation: navigationReducer,
|
2017-06-01 12:20:12 -04:00
|
|
|
availability: availabilityReducer,
|
|
|
|
claims: claimsReducer,
|
|
|
|
fileInfo: fileInfoReducer,
|
|
|
|
content: contentReducer,
|
|
|
|
costInfo: costInfoReducer,
|
|
|
|
rewards: rewardsReducer,
|
|
|
|
search: searchReducer,
|
|
|
|
settings: settingsReducer,
|
|
|
|
wallet: walletReducer,
|
|
|
|
user: userReducer,
|
2017-04-07 12:15:22 +07:00
|
|
|
});
|
|
|
|
|
2017-06-05 21:21:55 -07:00
|
|
|
const bulkThunk = createBulkThunkMiddleware();
|
|
|
|
const middleware = [thunk, bulkThunk];
|
2017-04-07 12:15:22 +07:00
|
|
|
|
2017-06-15 11:27:23 +07:00
|
|
|
if (env === "development") {
|
|
|
|
const logger = createLogger({
|
|
|
|
collapsed: true,
|
|
|
|
});
|
|
|
|
middleware.push(logger);
|
2017-05-11 19:28:43 -04:00
|
|
|
}
|
2017-04-07 12:15:22 +07:00
|
|
|
|
2017-06-21 20:09:30 -04:00
|
|
|
// middleware.push(createActionBuffer(REHYDRATE)); // was causing issues with authentication reducers not firing
|
2017-06-15 11:27:23 +07:00
|
|
|
|
2017-04-24 14:25:27 +07:00
|
|
|
const createStoreWithMiddleware = redux.compose(
|
2017-06-15 11:27:23 +07:00
|
|
|
autoRehydrate(),
|
|
|
|
redux.applyMiddleware(...middleware)
|
2017-04-07 12:15:22 +07:00
|
|
|
)(redux.createStore);
|
|
|
|
|
2017-04-27 00:08:26 +07:00
|
|
|
const reduxStore = createStoreWithMiddleware(enableBatching(reducers));
|
2017-06-15 11:27:23 +07:00
|
|
|
const compressor = createCompressor();
|
2017-07-10 21:44:49 +07:00
|
|
|
const saveClaimsFilter = createFilter("claims", ["byId", "claimsByUri"]);
|
2017-06-15 11:27:23 +07:00
|
|
|
|
|
|
|
const persistOptions = {
|
2017-07-08 15:03:12 +07:00
|
|
|
whitelist: ["claims"],
|
2017-06-15 11:27:23 +07:00
|
|
|
// Order is important. Needs to be compressed last or other transforms can't
|
|
|
|
// read the data
|
2017-07-08 15:03:12 +07:00
|
|
|
transforms: [saveClaimsFilter, compressor],
|
2017-07-01 18:03:51 +07:00
|
|
|
debounce: 10000,
|
2017-06-15 11:27:23 +07:00
|
|
|
storage: localForage,
|
|
|
|
};
|
2017-06-16 12:43:43 +07:00
|
|
|
window.cacheStore = persistStore(reduxStore, persistOptions);
|
2017-04-07 12:15:22 +07:00
|
|
|
|
|
|
|
export default reduxStore;
|