lbry-desktop/ui/js/store.js

81 lines
2 KiB
JavaScript
Raw Normal View History

2017-04-07 07:15:22 +02:00
const redux = require('redux');
2017-06-06 06:21:55 +02:00
const thunk = require('redux-thunk').default;
const env = ENV;
2017-04-07 07:15:22 +02:00
2017-06-06 06:21:55 +02:00
import { createLogger } from 'redux-logger';
2017-04-07 07:15:22 +02:00
import appReducer from 'reducers/app';
2017-06-06 06:21:55 +02:00
import availabilityReducer from 'reducers/availability';
import claimsReducer from 'reducers/claims';
2017-04-23 11:56:50 +02:00
import contentReducer from 'reducers/content';
2017-06-06 06:21:55 +02:00
import costInfoReducer from 'reducers/cost_info';
import fileInfoReducer from 'reducers/file_info';
import rewardsReducer from 'reducers/rewards';
import searchReducer from 'reducers/search';
import settingsReducer from 'reducers/settings';
import walletReducer from 'reducers/wallet';
2017-06-01 18:20:12 +02:00
import userReducer from 'reducers/user';
2017-04-07 07:15:22 +02:00
function isFunction(object) {
2017-06-06 06:21:55 +02:00
return typeof object === 'function';
2017-04-07 07:15:22 +02:00
}
function isNotFunction(object) {
2017-06-06 06:21:55 +02:00
return !isFunction(object);
2017-04-07 07:15:22 +02:00
}
function createBulkThunkMiddleware() {
2017-06-06 06:21:55 +02:00
return ({ dispatch, getState }) => next => action => {
if (action.type === 'BATCH_ACTIONS') {
action.actions
.filter(isFunction)
.map(actionFn => actionFn(dispatch, getState));
}
return next(action);
};
}
function enableBatching(reducer) {
2017-06-06 06:21:55 +02: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-07 07:15:22 +02:00
const reducers = redux.combineReducers({
2017-06-01 18:20:12 +02:00
app: appReducer,
availability: availabilityReducer,
claims: claimsReducer,
fileInfo: fileInfoReducer,
content: contentReducer,
costInfo: costInfoReducer,
rewards: rewardsReducer,
search: searchReducer,
settings: settingsReducer,
wallet: walletReducer,
user: userReducer,
2017-04-07 07:15:22 +02:00
});
2017-06-06 06:21:55 +02:00
const bulkThunk = createBulkThunkMiddleware();
const middleware = [thunk, bulkThunk];
2017-04-07 07:15:22 +02:00
2017-05-12 01:28:43 +02:00
if (env === 'development') {
2017-06-06 06:21:55 +02:00
const logger = createLogger({
collapsed: true
});
middleware.push(logger);
2017-05-12 01:28:43 +02:00
}
2017-04-07 07:15:22 +02:00
const createStoreWithMiddleware = redux.compose(
2017-06-06 06:21:55 +02:00
redux.applyMiddleware(...middleware)
2017-04-07 07:15:22 +02:00
)(redux.createStore);
const reduxStore = createStoreWithMiddleware(enableBatching(reducers));
2017-04-07 07:15:22 +02:00
export default reduxStore;