lbry-desktop/src/renderer/store.js

116 lines
3.6 KiB
JavaScript
Raw Normal View History

import { createLogger } from 'redux-logger';
import appReducer from 'redux/reducers/app';
import availabilityReducer from 'redux/reducers/availability';
import claimsReducer from 'redux/reducers/claims';
import contentReducer from 'redux/reducers/content';
import costInfoReducer from 'redux/reducers/cost_info';
import fileInfoReducer from 'redux/reducers/file_info';
import navigationReducer from 'redux/reducers/navigation';
import rewardsReducer from 'redux/reducers/rewards';
import searchReducer from 'redux/reducers/search';
import settingsReducer from 'redux/reducers/settings';
import userReducer from 'redux/reducers/user';
import walletReducer from 'redux/reducers/wallet';
import shapeShiftReducer from 'redux/reducers/shape_shift';
import subscriptionsReducer from 'redux/reducers/subscriptions';
2018-01-06 00:57:24 +01:00
import mediaReducer from 'redux/reducers/media';
2018-03-26 23:32:43 +02:00
import publishReducer from 'redux/reducers/publish';
import { persistStore, autoRehydrate } from 'redux-persist';
import createCompressor from 'redux-persist-transform-compress';
import createFilter from 'redux-persist-transform-filter';
import localForage from 'localforage';
import { createStore, applyMiddleware, compose, combineReducers } from 'redux';
import thunk from 'redux-thunk';
2017-04-07 07:15:22 +02:00
function isFunction(object) {
return typeof object === 'function';
2017-04-07 07:15:22 +02:00
}
function isNotFunction(object) {
return !isFunction(object);
2017-04-07 07:15:22 +02:00
}
function createBulkThunkMiddleware() {
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) {
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-12-08 21:14:35 +01:00
const reducers = combineReducers({
2017-06-01 18:20:12 +02:00
app: appReducer,
navigation: navigationReducer,
2017-06-01 18:20:12 +02:00
availability: availabilityReducer,
claims: claimsReducer,
fileInfo: fileInfoReducer,
content: contentReducer,
costInfo: costInfoReducer,
rewards: rewardsReducer,
search: searchReducer,
settings: settingsReducer,
wallet: walletReducer,
user: userReducer,
2017-12-01 04:51:55 +01:00
shapeShift: shapeShiftReducer,
2017-12-08 21:14:35 +01:00
subscriptions: subscriptionsReducer,
2017-12-21 00:38:11 +01:00
media: mediaReducer,
2018-03-26 23:32:43 +02:00
publish: publishReducer,
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
if (app.env === 'development') {
const logger = createLogger({
collapsed: true,
});
middleware.push(logger);
2017-05-12 01:28:43 +02:00
}
2017-04-07 07:15:22 +02:00
// eslint-disable-next-line no-underscore-dangle
2017-12-08 21:14:35 +01:00
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(
enableBatching(reducers),
{}, // initial state
composeEnhancers(
autoRehydrate({
log: app.env === 'development',
2017-12-08 21:14:35 +01:00
}),
applyMiddleware(...middleware)
)
);
2017-04-07 07:15:22 +02:00
const compressor = createCompressor();
const saveClaimsFilter = createFilter('claims', ['byId', 'claimsByUri']);
const subscriptionsFilter = createFilter('subscriptions', ['subscriptions']);
const persistOptions = {
whitelist: ['claims', 'subscriptions', 'publish'],
// Order is important. Needs to be compressed last or other transforms can't
// read the data
2017-12-08 21:14:35 +01:00
transforms: [saveClaimsFilter, subscriptionsFilter, compressor],
debounce: 10000,
storage: localForage,
};
2017-04-07 07:15:22 +02:00
2017-12-08 21:14:35 +01:00
window.cacheStore = persistStore(store, persistOptions, err => {
if (err) {
2018-03-26 23:32:43 +02:00
console.error('Unable to load saved settings'); // eslint-disable-line no-console
2017-12-08 21:14:35 +01:00
}
});
export default store;