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';
|
2019-04-04 23:05:23 +02:00
|
|
|
import { createStore, applyMiddleware, compose } from 'redux';
|
2017-12-21 18:32:51 +01:00
|
|
|
import thunk from 'redux-thunk';
|
2019-04-04 23:05:23 +02:00
|
|
|
import { createHashHistory, createBrowserHistory } from 'history';
|
2019-06-11 20:10:58 +02:00
|
|
|
import { routerMiddleware } from 'connected-react-router';
|
2019-04-04 23:05:23 +02:00
|
|
|
import createRootReducer from './reducers';
|
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
|
|
|
}
|
|
|
|
|
2019-04-18 18:51:15 +02:00
|
|
|
let history;
|
2019-04-04 23:05:23 +02:00
|
|
|
// @if TARGET='app'
|
2019-04-18 18:51:15 +02:00
|
|
|
history = createHashHistory();
|
2019-04-04 23:05:23 +02:00
|
|
|
// @endif
|
|
|
|
// @if TARGET='web'
|
2019-04-18 18:51:15 +02:00
|
|
|
history = createBrowserHistory();
|
2019-04-04 23:05:23 +02:00
|
|
|
// @endif
|
2019-04-18 18:51:15 +02:00
|
|
|
|
2017-06-06 06:21:55 +02:00
|
|
|
const bulkThunk = createBulkThunkMiddleware();
|
2019-04-04 23:05:23 +02:00
|
|
|
const middleware = [routerMiddleware(history), thunk, bulkThunk];
|
2017-12-08 21:14:35 +01:00
|
|
|
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
|
|
|
|
|
|
|
|
const store = createStore(
|
2019-04-04 23:05:23 +02:00
|
|
|
enableBatching(createRootReducer(history)),
|
2017-12-08 21:14:35 +01:00
|
|
|
{}, // 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-04-11 21:18:52 +02:00
|
|
|
whitelist: [
|
|
|
|
// @if TARGET='app'
|
|
|
|
'publish',
|
|
|
|
'wallet',
|
|
|
|
'fileInfo',
|
|
|
|
// @endif
|
|
|
|
'content',
|
|
|
|
'subscriptions',
|
|
|
|
'app',
|
|
|
|
'search',
|
2019-06-11 20:10:58 +02:00
|
|
|
'tags',
|
2019-04-11 21:18:52 +02:00
|
|
|
],
|
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-04-11 21:18:52 +02:00
|
|
|
transforms: [
|
|
|
|
// @if TARGET='app'
|
|
|
|
walletFilter,
|
|
|
|
contentFilter,
|
|
|
|
fileInfoFilter,
|
|
|
|
// @endif
|
|
|
|
appFilter,
|
|
|
|
searchFilter,
|
|
|
|
compressor,
|
|
|
|
],
|
2019-06-11 20:10:58 +02:00
|
|
|
debounce: 5000,
|
2017-06-15 06:27:23 +02:00
|
|
|
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) {
|
2019-03-18 06:09:50 +01:00
|
|
|
console.error('Unable to load saved settings');
|
2017-12-08 21:14:35 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-04-04 23:05:23 +02:00
|
|
|
export { store, history };
|