Reinitialize redux after importing a backup

This commit is contained in:
Scott Nonnenberg 2024-08-27 00:26:21 +10:00 committed by GitHub
parent 19e0eb4444
commit abdef4847a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 437 additions and 264 deletions

View file

@ -0,0 +1,57 @@
// Copyright 2023 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { AnyAction } from 'redux';
import * as log from '../logging/log';
import { getInitialState } from './getInitialState';
import { reducer as normalReducer } from './reducer';
import type { StateType } from './reducer';
import type { ReduxInitData } from './initializeRedux';
const REPLACE_STATE = 'resetReducer/REPLACE';
export function reinitializeRedux(options: ReduxInitData): void {
const logId = 'initializeRedux';
const existingState = window.reduxStore.getState();
const newInitialState = getInitialState(options, existingState);
const resetReducer = (
state: StateType | undefined,
action: AnyAction
): StateType => {
if (state == null) {
log.info(
`${logId}/resetReducer: Got null incoming state, returning newInitialState`
);
return newInitialState;
}
const { type } = action;
if (type === REPLACE_STATE) {
log.info(
`${logId}/resetReducer: Got REPLACE_STATE action, returning newInitialState`
);
return newInitialState;
}
log.info(
`${logId}/resetReducer: Got action with type ${type}, returning original state`
);
return state;
};
log.info(`${logId}: installing resetReducer`);
window.reduxStore.replaceReducer(resetReducer);
log.info(`${logId}: dispatching REPLACE_STATE event`);
window.reduxStore.dispatch({
type: REPLACE_STATE,
});
log.info(`${logId}: restoring original reducer`);
window.reduxStore.replaceReducer(normalReducer);
log.info(`${logId}: complete!`);
}