2017-12-21 18:32:51 +01:00
|
|
|
import appReducer from 'redux/reducers/app';
|
|
|
|
import availabilityReducer from 'redux/reducers/availability';
|
|
|
|
import contentReducer from 'redux/reducers/content';
|
2018-04-18 06:03:01 +02:00
|
|
|
import {
|
|
|
|
claimsReducer,
|
|
|
|
fileInfoReducer,
|
|
|
|
searchReducer,
|
|
|
|
walletReducer,
|
2018-04-19 18:51:18 +02:00
|
|
|
notificationsReducer,
|
2018-04-18 06:03:01 +02:00
|
|
|
} from 'lbry-redux';
|
2019-03-12 20:53:55 +01:00
|
|
|
import {
|
|
|
|
userReducer,
|
|
|
|
rewardsReducer,
|
|
|
|
costInfoReducer,
|
|
|
|
blacklistReducer,
|
|
|
|
homepageReducer,
|
2019-03-14 19:40:26 +01:00
|
|
|
statsReducer,
|
2019-03-12 20:53:55 +01:00
|
|
|
} from 'lbryinc';
|
2017-12-21 18:32:51 +01:00
|
|
|
import navigationReducer from 'redux/reducers/navigation';
|
|
|
|
import settingsReducer from 'redux/reducers/settings';
|
|
|
|
import subscriptionsReducer from 'redux/reducers/subscriptions';
|
2018-03-26 23:32:43 +02:00
|
|
|
import publishReducer from 'redux/reducers/publish';
|
2017-12-21 18:32:51 +01:00
|
|
|
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) {
|
2017-12-21 18:32:51 +01:00
|
|
|
return typeof object === 'function';
|
2017-04-07 07:15:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function isNotFunction(object) {
|
2017-06-15 06:27:23 +02:00
|
|
|
return !isFunction(object);
|
2017-04-07 07:15:22 +02:00
|
|
|
}
|
|
|
|
|
2017-04-26 19:08:26 +02:00
|
|
|
function createBulkThunkMiddleware() {
|
2017-06-15 06:27:23 +02:00
|
|
|
return ({ dispatch, getState }) => next => action => {
|
2017-12-21 18:32:51 +01:00
|
|
|
if (action.type === 'BATCH_ACTIONS') {
|
|
|
|
action.actions.filter(isFunction).map(actionFn => actionFn(dispatch, getState));
|
2017-06-15 06:27:23 +02:00
|
|
|
}
|
|
|
|
return next(action);
|
|
|
|
};
|
2017-04-26 19:08:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function enableBatching(reducer) {
|
2017-06-15 06:27:23 +02:00
|
|
|
return function batchingReducer(state, action) {
|
|
|
|
switch (action.type) {
|
2017-12-21 18:32:51 +01:00
|
|
|
case 'BATCH_ACTIONS':
|
|
|
|
return action.actions.filter(isNotFunction).reduce(batchingReducer, state);
|
2017-06-15 06:27:23 +02:00
|
|
|
default:
|
|
|
|
return reducer(state, action);
|
|
|
|
}
|
|
|
|
};
|
2017-04-26 19:08:26 +02:00
|
|
|
}
|
|
|
|
|
2017-12-08 21:14:35 +01:00
|
|
|
const reducers = combineReducers({
|
2017-06-01 18:20:12 +02:00
|
|
|
app: appReducer,
|
2017-08-30 14:48:32 +02:00
|
|
|
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-08 21:14:35 +01:00
|
|
|
subscriptions: subscriptionsReducer,
|
2018-03-26 23:32:43 +02:00
|
|
|
publish: publishReducer,
|
2018-04-19 18:51:18 +02:00
|
|
|
notifications: notificationsReducer,
|
2018-04-24 20:17:11 +02:00
|
|
|
blacklist: blacklistReducer,
|
2019-03-12 20:53:55 +01:00
|
|
|
homepage: homepageReducer,
|
2019-03-14 19:40:26 +01:00
|
|
|
stats: statsReducer,
|
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-12-08 21:14:35 +01:00
|
|
|
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
|
|
|
|
|
|
|
|
const store = createStore(
|
|
|
|
enableBatching(reducers),
|
|
|
|
{}, // initial state
|
|
|
|
composeEnhancers(
|
|
|
|
autoRehydrate({
|
2017-12-21 18:32:51 +01:00
|
|
|
log: app.env === 'development',
|
2017-12-08 21:14:35 +01:00
|
|
|
}),
|
|
|
|
applyMiddleware(...middleware)
|
|
|
|
)
|
|
|
|
);
|
2017-04-07 07:15:22 +02:00
|
|
|
|
2017-06-15 06:27:23 +02:00
|
|
|
const compressor = createCompressor();
|
2018-10-03 18:49:48 +02:00
|
|
|
// Removing claims from redux-persist to see if it solves https://github.com/lbryio/lbry-desktop/issues/1983
|
|
|
|
// We were caching so much data the app was locking up
|
|
|
|
// We can't add this back until we can perform this in a non-blocking way
|
|
|
|
// const saveClaimsFilter = createFilter('claims', ['byId', 'claimsByUri']);
|
2018-07-31 20:07:45 +02:00
|
|
|
const contentFilter = createFilter('content', ['positions', 'history']);
|
2018-10-25 08:01:32 +02:00
|
|
|
const fileInfoFilter = createFilter('fileInfo', [
|
|
|
|
'fileListPublishedSort',
|
|
|
|
'fileListDownloadedSort',
|
|
|
|
'fileListSubscriptionSort',
|
|
|
|
]);
|
2019-02-21 23:45:17 +01:00
|
|
|
const appFilter = createFilter('app', ['hasClickedComment', 'searchOptionsExpanded']);
|
2018-04-18 06:03:01 +02:00
|
|
|
// We only need to persist the receiveAddress for the wallet
|
|
|
|
const walletFilter = createFilter('wallet', ['receiveAddress']);
|
2019-02-18 18:24:56 +01:00
|
|
|
const searchFilter = createFilter('search', ['options']);
|
2017-06-15 06:27:23 +02:00
|
|
|
|
|
|
|
const persistOptions = {
|
2019-02-18 18:24:56 +01:00
|
|
|
whitelist: ['subscriptions', 'publish', 'wallet', 'content', 'fileInfo', 'app', 'search'],
|
2017-06-15 06:27:23 +02:00
|
|
|
// Order is important. Needs to be compressed last or other transforms can't
|
|
|
|
// read the data
|
2019-02-18 18:24:56 +01:00
|
|
|
transforms: [walletFilter, contentFilter, fileInfoFilter, appFilter, searchFilter, compressor],
|
2017-07-01 13:03:51 +02:00
|
|
|
debounce: 10000,
|
2017-06-15 06:27:23 +02:00
|
|
|
storage: localForage,
|
|
|
|
};
|
2017-04-07 07:15:22 +02:00
|
|
|
|
2019-03-13 20:17:17 +01:00
|
|
|
// Dont' persist anything on web (for now)
|
|
|
|
// @if TARGET='app'
|
2017-12-08 21:14:35 +01:00
|
|
|
window.cacheStore = persistStore(store, persistOptions, err => {
|
|
|
|
if (err) {
|
2019-03-18 06:09:50 +01:00
|
|
|
console.error('Unable to load saved settings');
|
2017-12-08 21:14:35 +01:00
|
|
|
}
|
|
|
|
});
|
2019-03-13 20:17:17 +01:00
|
|
|
// @endif
|
2017-12-08 21:14:35 +01:00
|
|
|
|
|
|
|
export default store;
|