2019-07-23 10:05:51 +02:00
|
|
|
import { persistStore, persistReducer } from 'redux-persist';
|
|
|
|
import autoMergeLevel2 from 'redux-persist/lib/stateReconciler/autoMergeLevel2';
|
2017-12-21 18:32:51 +01:00
|
|
|
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';
|
2019-09-26 18:07:11 +02:00
|
|
|
import { Lbryio } from 'lbryinc';
|
|
|
|
import isEqual from 'util/deep-equal';
|
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
|
|
|
}
|
|
|
|
|
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-08-13 07:35:13 +02:00
|
|
|
const appFilter = createFilter('app', ['hasClickedComment', 'searchOptionsExpanded', 'volume', 'muted']);
|
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']);
|
2019-07-02 23:00:05 +02:00
|
|
|
const tagsFilter = createFilter('tags', ['followedTags']);
|
2019-08-02 17:11:31 +02:00
|
|
|
const blockedFilter = createFilter('blocked', ['blockedChannels']);
|
2019-07-01 06:35:36 +02:00
|
|
|
const whiteListedReducers = [
|
|
|
|
// @if TARGET='app'
|
|
|
|
'publish',
|
|
|
|
'wallet',
|
2019-09-26 18:07:11 +02:00
|
|
|
'tags',
|
2019-07-23 10:05:51 +02:00
|
|
|
// 'fileInfo',
|
2019-07-01 06:35:36 +02:00
|
|
|
// @endif
|
|
|
|
'content',
|
|
|
|
'app',
|
|
|
|
'search',
|
2019-08-02 17:11:31 +02:00
|
|
|
'blocked',
|
2019-09-17 20:49:03 +02:00
|
|
|
'settings',
|
2019-07-01 06:35:36 +02:00
|
|
|
];
|
2019-07-02 23:00:05 +02:00
|
|
|
|
2019-07-23 10:05:51 +02:00
|
|
|
const transforms = [
|
|
|
|
// @if TARGET='app'
|
|
|
|
walletFilter,
|
|
|
|
fileInfoFilter,
|
2019-07-08 22:54:58 +02:00
|
|
|
blockedFilter,
|
2019-09-26 18:07:11 +02:00
|
|
|
tagsFilter,
|
2019-07-23 10:05:51 +02:00
|
|
|
// @endif
|
|
|
|
appFilter,
|
|
|
|
searchFilter,
|
|
|
|
tagsFilter,
|
2019-09-17 20:57:22 +02:00
|
|
|
contentFilter,
|
2019-07-23 10:05:51 +02:00
|
|
|
createCompressor(),
|
|
|
|
];
|
|
|
|
|
2017-06-15 06:27:23 +02:00
|
|
|
const persistOptions = {
|
2019-07-23 10:05:51 +02:00
|
|
|
key: 'v0',
|
|
|
|
storage: localForage,
|
|
|
|
stateReconciler: autoMergeLevel2,
|
2019-07-01 06:35:36 +02:00
|
|
|
whitelist: whiteListedReducers,
|
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-07-23 10:05:51 +02:00
|
|
|
transforms,
|
2017-06-15 06:27:23 +02:00
|
|
|
};
|
2017-04-07 07:15:22 +02:00
|
|
|
|
2019-07-23 10:05:51 +02:00
|
|
|
let history;
|
|
|
|
// @if TARGET='app'
|
|
|
|
history = createHashHistory();
|
|
|
|
// @endif
|
|
|
|
// @if TARGET='web'
|
|
|
|
history = createBrowserHistory();
|
|
|
|
// @endif
|
|
|
|
|
|
|
|
const rootReducer = createRootReducer(history);
|
|
|
|
const persistedReducer = persistReducer(persistOptions, rootReducer);
|
|
|
|
const bulkThunk = createBulkThunkMiddleware();
|
|
|
|
const middleware = [routerMiddleware(history), thunk, bulkThunk];
|
|
|
|
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
|
|
|
|
const store = createStore(
|
|
|
|
enableBatching(persistedReducer),
|
|
|
|
{}, // initial state
|
|
|
|
composeEnhancers(applyMiddleware(...middleware))
|
|
|
|
);
|
|
|
|
|
2019-09-26 18:07:11 +02:00
|
|
|
let currentPayload;
|
|
|
|
store.subscribe(() => {
|
|
|
|
const state = store.getState();
|
|
|
|
const subscriptions = state.subscriptions.subscriptions.map(({ uri }) => uri);
|
|
|
|
const tags = state.tags.followedTags;
|
|
|
|
const authToken = state.user.accessToken;
|
|
|
|
|
|
|
|
const newPayload = {
|
|
|
|
version: '0.1',
|
|
|
|
shared: {
|
|
|
|
subscriptions,
|
|
|
|
tags,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!isEqual(newPayload, currentPayload)) {
|
|
|
|
currentPayload = newPayload;
|
|
|
|
if (authToken) {
|
|
|
|
Lbryio.call('user_settings', 'set', { settings: newPayload });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-07-23 10:05:51 +02:00
|
|
|
const persistor = persistStore(store);
|
|
|
|
window.persistor = persistor;
|
2017-12-08 21:14:35 +01:00
|
|
|
|
2019-07-23 10:05:51 +02:00
|
|
|
export { store, persistor, history, whiteListedReducers };
|