This commit is contained in:
Nathan Bierema 2021-08-30 17:39:54 -04:00
parent e9be680533
commit 720ab4fbfb
4 changed files with 48 additions and 73 deletions

View File

@ -63,17 +63,14 @@ export default function configureStore(
} }
const store = createStore(rootReducer, preloadedState, enhancer); const store = createStore(rootReducer, preloadedState, enhancer);
chrome.storage.local.get(['s:hostname', 's:port', 's:secure'], (options) => { if (
if (!options['s:hostname'] || !options['s:port']) return; store.getState().connection.options.hostname &&
store.getState().connection.options.port
) {
store.dispatch({ store.dispatch({
type: CONNECT_REQUEST, type: CONNECT_REQUEST,
options: {
hostname: options['s:hostname'],
port: options['s:port'],
secure: options['s:secure'],
} as any,
}); });
}); }
return store; return store;
} }

View File

@ -446,7 +446,6 @@ export interface RemoveInstanceAction {
export interface ConnectRequestAction { export interface ConnectRequestAction {
type: typeof CONNECT_REQUEST; type: typeof CONNECT_REQUEST;
options: ConnectionOptions;
} }
interface ConnectSuccessPayload { interface ConnectSuccessPayload {

View File

@ -16,15 +16,11 @@ class Root extends Component<Props> {
store?: Store<StoreState, StoreAction>; store?: Store<StoreState, StoreAction>;
UNSAFE_componentWillMount() { UNSAFE_componentWillMount() {
configureStore((store, preloadedState) => { this.store = configureStore();
this.store = store; this.store.dispatch({
store.dispatch({ type: CONNECT_REQUEST,
type: CONNECT_REQUEST,
options: (preloadedState!.connection ||
this.props.socketOptions) as ConnectionOptions,
});
this.forceUpdate();
}); });
this.forceUpdate();
} }
render() { render() {

View File

@ -1,66 +1,49 @@
import { createStore, compose, applyMiddleware, Store } from 'redux'; import { createStore, compose, applyMiddleware } from 'redux';
import localForage from 'localforage'; import localForage from 'localforage';
import { import { persistReducer, persistStore } from 'redux-persist';
getStoredState,
createPersistor,
PersistorConfig,
} from 'redux-persist';
import api from '../middlewares/api'; import api from '../middlewares/api';
import exportState from '../middlewares/exportState'; import exportState from '../middlewares/exportState';
import rootReducer, { StoreState } from '../reducers'; import rootReducer, { StoreState } from '../reducers';
import { StoreAction } from '../actions'; import { StoreAction } from '../actions';
export default function configureStore( const persistConfig = {
callback: ( key: 'redux-devtools',
store: Store<StoreState, StoreAction>, blacklist: ['instances', 'socket'],
restoredState: Partial<StoreState> | undefined storage: localForage,
) => void, };
key?: string
) {
const persistConfig: PersistorConfig = {
keyPrefix: `redux-devtools${key || ''}:`,
blacklist: ['instances', 'socket'],
storage: localForage,
serialize: (data: unknown) => data,
deserialize: (data: unknown) => data,
} as unknown as PersistorConfig;
// eslint-disable-next-line @typescript-eslint/no-floating-promises const persistedReducer = persistReducer(persistConfig, rootReducer);
getStoredState<StoreState>(persistConfig, (err, restoredState) => {
let composeEnhancers = compose; export default function configureStore() {
if (process.env.NODE_ENV !== 'production') { let composeEnhancers = compose;
if ( if (process.env.NODE_ENV !== 'production') {
( if (
window as unknown as { (
__REDUX_DEVTOOLS_EXTENSION_COMPOSE__?: typeof compose; window as unknown as {
} __REDUX_DEVTOOLS_EXTENSION_COMPOSE__?: typeof compose;
).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ }
) { ).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__
composeEnhancers = ( ) {
window as unknown as { composeEnhancers = (
__REDUX_DEVTOOLS_EXTENSION_COMPOSE__: typeof compose; window as unknown as {
} __REDUX_DEVTOOLS_EXTENSION_COMPOSE__: typeof compose;
).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__; }
} ).__REDUX_DEVTOOLS_EXTENSION_COMPOSE__;
if (module.hot) {
// Enable Webpack hot module replacement for reducers
module.hot.accept('../reducers', () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const nextReducer = require('../reducers'); // eslint-disable-line global-require
store.replaceReducer(nextReducer);
});
}
} }
if (module.hot) {
// Enable Webpack hot module replacement for reducers
module.hot.accept('../reducers', () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const nextReducer = require('../reducers'); // eslint-disable-line global-require
store.replaceReducer(nextReducer);
});
}
}
const store = createStore( const store = createStore<StoreState, StoreAction, unknown, unknown>(
rootReducer, persistedReducer as any,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment composeEnhancers(applyMiddleware(exportState, api))
// @ts-ignore );
restoredState, persistStore(store);
composeEnhancers(applyMiddleware(exportState, api)) return store;
);
const persistor = createPersistor(store, persistConfig);
callback(store, restoredState);
if (err) persistor.purge();
});
} }