Add flow types to Sync. No functional change.

This commit is contained in:
infinite-persistence 2021-12-17 12:08:16 +08:00
parent 3bce2e656f
commit 3040b9ea12
No known key found for this signature in database
GPG key ID: B9C3252EDC3D0AA0
2 changed files with 36 additions and 16 deletions

16
flow-typed/sync.js vendored Normal file
View file

@ -0,0 +1,16 @@
declare type SyncState = {
hasSyncedWallet: boolean,
syncHash: ?string,
syncData: ?string,
setSyncErrorMessage: ?string,
getSyncErrorMessage: ?string,
syncApplyErrorMessage: string,
syncApplyIsPending: boolean,
syncApplyPasswordError: boolean,
getSyncIsPending: boolean,
setSyncIsPending: boolean,
prefsReady: boolean,
syncLocked: boolean,
hashChanged: boolean,
fatalError: boolean,
};

View file

@ -1,7 +1,9 @@
// @flow
import * as ACTIONS from 'constants/action_types'; import * as ACTIONS from 'constants/action_types';
const reducers = {}; const reducers = {};
const defaultState = {
const defaultState: SyncState = {
hasSyncedWallet: false, hasSyncedWallet: false,
syncHash: null, syncHash: null,
syncData: null, syncData: null,
@ -18,7 +20,8 @@ const defaultState = {
fatalError: false, fatalError: false,
}; };
reducers[ACTIONS.USER_STATE_POPULATE] = (state) => { reducers[ACTIONS.USER_STATE_POPULATE] = (state: SyncState) => {
// $FlowFixMe - 'syncReady' doesn't exist. A bug?
const { syncReady } = state; const { syncReady } = state;
if (!syncReady) { if (!syncReady) {
return Object.assign({}, state, { return Object.assign({}, state, {
@ -29,20 +32,21 @@ reducers[ACTIONS.USER_STATE_POPULATE] = (state) => {
} }
}; };
reducers[ACTIONS.SET_PREFS_READY] = (state, action) => Object.assign({}, state, { prefsReady: action.data }); reducers[ACTIONS.SET_PREFS_READY] = (state: SyncState, action: any) =>
Object.assign({}, state, { prefsReady: action.data });
reducers[ACTIONS.GET_SYNC_STARTED] = (state) => reducers[ACTIONS.GET_SYNC_STARTED] = (state: SyncState) =>
Object.assign({}, state, { Object.assign({}, state, {
getSyncIsPending: true, getSyncIsPending: true,
getSyncErrorMessage: null, getSyncErrorMessage: null,
}); });
reducers[ACTIONS.SET_SYNC_LOCK] = (state, action) => reducers[ACTIONS.SET_SYNC_LOCK] = (state: SyncState, action: any) =>
Object.assign({}, state, { Object.assign({}, state, {
syncLocked: action.data, syncLocked: action.data,
}); });
reducers[ACTIONS.GET_SYNC_COMPLETED] = (state, action) => reducers[ACTIONS.GET_SYNC_COMPLETED] = (state: SyncState, action: any) =>
Object.assign({}, state, { Object.assign({}, state, {
syncHash: action.data.syncHash, syncHash: action.data.syncHash,
syncData: action.data.syncData, syncData: action.data.syncData,
@ -52,25 +56,25 @@ reducers[ACTIONS.GET_SYNC_COMPLETED] = (state, action) =>
fatalError: action.data.fatalError, fatalError: action.data.fatalError,
}); });
reducers[ACTIONS.GET_SYNC_FAILED] = (state, action) => reducers[ACTIONS.GET_SYNC_FAILED] = (state: SyncState, action: any) =>
Object.assign({}, state, { Object.assign({}, state, {
getSyncIsPending: false, getSyncIsPending: false,
getSyncErrorMessage: action.data.error, getSyncErrorMessage: action.data.error,
}); });
reducers[ACTIONS.SET_SYNC_STARTED] = (state) => reducers[ACTIONS.SET_SYNC_STARTED] = (state: SyncState) =>
Object.assign({}, state, { Object.assign({}, state, {
setSyncIsPending: true, setSyncIsPending: true,
setSyncErrorMessage: null, setSyncErrorMessage: null,
}); });
reducers[ACTIONS.SET_SYNC_FAILED] = (state, action) => reducers[ACTIONS.SET_SYNC_FAILED] = (state: SyncState, action: any) =>
Object.assign({}, state, { Object.assign({}, state, {
setSyncIsPending: false, setSyncIsPending: false,
setSyncErrorMessage: action.data.error, setSyncErrorMessage: action.data.error,
}); });
reducers[ACTIONS.SET_SYNC_COMPLETED] = (state, action) => reducers[ACTIONS.SET_SYNC_COMPLETED] = (state: SyncState, action: any) =>
Object.assign({}, state, { Object.assign({}, state, {
setSyncIsPending: false, setSyncIsPending: false,
setSyncErrorMessage: null, setSyncErrorMessage: null,
@ -78,31 +82,31 @@ reducers[ACTIONS.SET_SYNC_COMPLETED] = (state, action) =>
syncHash: action.data.syncHash, syncHash: action.data.syncHash,
}); });
reducers[ACTIONS.SYNC_APPLY_STARTED] = (state) => reducers[ACTIONS.SYNC_APPLY_STARTED] = (state: SyncState) =>
Object.assign({}, state, { Object.assign({}, state, {
syncApplyPasswordError: false, syncApplyPasswordError: false,
syncApplyIsPending: true, syncApplyIsPending: true,
syncApplyErrorMessage: '', syncApplyErrorMessage: '',
}); });
reducers[ACTIONS.SYNC_APPLY_COMPLETED] = (state) => reducers[ACTIONS.SYNC_APPLY_COMPLETED] = (state: SyncState) =>
Object.assign({}, state, { Object.assign({}, state, {
syncApplyIsPending: false, syncApplyIsPending: false,
syncApplyErrorMessage: '', syncApplyErrorMessage: '',
}); });
reducers[ACTIONS.SYNC_APPLY_FAILED] = (state, action) => reducers[ACTIONS.SYNC_APPLY_FAILED] = (state: SyncState, action: any) =>
Object.assign({}, state, { Object.assign({}, state, {
syncApplyIsPending: false, syncApplyIsPending: false,
syncApplyErrorMessage: action.data.error, syncApplyErrorMessage: action.data.error,
}); });
reducers[ACTIONS.SYNC_APPLY_BAD_PASSWORD] = (state) => reducers[ACTIONS.SYNC_APPLY_BAD_PASSWORD] = (state: SyncState) =>
Object.assign({}, state, { Object.assign({}, state, {
syncApplyPasswordError: true, syncApplyPasswordError: true,
}); });
reducers[ACTIONS.SYNC_FATAL_ERROR] = (state) => { reducers[ACTIONS.SYNC_FATAL_ERROR] = (state: SyncState) => {
return Object.assign({}, state, { return Object.assign({}, state, {
fatalError: true, fatalError: true,
}); });
@ -110,7 +114,7 @@ reducers[ACTIONS.SYNC_FATAL_ERROR] = (state) => {
reducers[ACTIONS.SYNC_RESET] = () => defaultState; reducers[ACTIONS.SYNC_RESET] = () => defaultState;
export default function syncReducer(state = defaultState, action) { export default function syncReducer(state: SyncState = defaultState, action: any) {
const handler = reducers[action.type]; const handler = reducers[action.type];
if (handler) return handler(state, action); if (handler) return handler(state, action);
return state; return state;