diff --git a/web/redux/redux-setup.js b/web/redux/redux-setup.js --- a/web/redux/redux-setup.js +++ b/web/redux/redux-setup.js @@ -7,6 +7,10 @@ logOutActionTypes, deleteAccountActionTypes, } from 'lib/actions/user-actions.js'; +import { + type ThreadStoreOperation, + threadStoreOpsHandlers, +} from 'lib/ops/thread-store-ops.js'; import baseReducer from 'lib/reducers/master-reducer.js'; import { mostRecentlyReadThreadSelector } from 'lib/selectors/thread-selectors.js'; import { isLoggedIn } from 'lib/selectors/user-selectors.js'; @@ -27,6 +31,7 @@ import type { UserPolicies } from 'lib/types/policy-types.js'; import type { BaseAction } from 'lib/types/redux-types.js'; import type { ReportStore } from 'lib/types/report-types.js'; +import type { StoreOperations } from 'lib/types/store-ops-types.js'; import type { GlobalThemeInfo } from 'lib/types/theme-types.js'; import type { ThreadActivityStore } from 'lib/types/thread-activity-types'; import type { ThreadStore } from 'lib/types/thread-types.js'; @@ -109,6 +114,13 @@ export function reducer(oldState: AppState | void, action: Action): AppState { invariant(oldState, 'should be set'); let state = oldState; + let storeOperations: StoreOperations = { + draftStoreOperations: [], + threadStoreOperations: [], + messageStoreOperations: [], + reportStoreOperations: [], + userStoreOperations: [], + }; if (action.type === setInitialReduxState) { const { userInfos, keyserverInfos, ...rest } = action.payload; @@ -123,26 +135,38 @@ updatesCurrentAsOf: newUpdatesCurrentAsOf, }; } - return validateState(oldState, { - ...state, - ...rest, - userStore: { userInfos }, - keyserverStore: { - ...state.keyserverStore, - keyserverInfos: newKeyserverInfos, + return validateStateAndProcessDBOperations( + oldState, + { + ...state, + ...rest, + userStore: { userInfos }, + keyserverStore: { + ...state.keyserverStore, + keyserverInfos: newKeyserverInfos, + }, + initialStateLoaded: true, }, - initialStateLoaded: true, - }); + storeOperations, + ); } else if (action.type === updateWindowDimensionsActionType) { - return validateState(oldState, { - ...state, - windowDimensions: action.payload, - }); + return validateStateAndProcessDBOperations( + oldState, + { + ...state, + windowDimensions: action.payload, + }, + storeOperations, + ); } else if (action.type === updateWindowActiveActionType) { - return validateState(oldState, { - ...state, - windowActive: action.payload, - }); + return validateStateAndProcessDBOperations( + oldState, + { + ...state, + windowActive: action.payload, + }, + storeOperations, + ); } else if (action.type === setNewSessionActionType) { if ( invalidSessionDowngrade( @@ -190,11 +214,7 @@ ) { const baseReducerResult = baseReducer(state, action, onStateDifference); state = baseReducerResult.state; - - processDBStoreOperations( - baseReducerResult.storeOperations, - state.currentUserInfo?.id, - ); + storeOperations = baseReducerResult.storeOperations; } const communityPickerStore = reduceCommunityPickerStore( @@ -213,10 +233,15 @@ communityPickerStore, }; - return validateState(oldState, state); + return validateStateAndProcessDBOperations(oldState, state, storeOperations); } -function validateState(oldState: AppState, state: AppState): AppState { +function validateStateAndProcessDBOperations( + oldState: AppState, + state: AppState, + storeOperations: StoreOperations, +): AppState { + const updateActiveThreadOps: ThreadStoreOperation[] = []; if ( (state.navInfo.activeChatThreadID && !state.navInfo.pendingThread && @@ -270,22 +295,19 @@ state.threadStore.threadInfos[activeThread].currentUser.unread ) { // Makes sure a currently focused thread is never unread - state = { - ...state, - threadStore: { - ...state.threadStore, - threadInfos: { - ...state.threadStore.threadInfos, - [activeThread]: { - ...state.threadStore.threadInfos[activeThread], - currentUser: { - ...state.threadStore.threadInfos[activeThread].currentUser, - unread: false, - }, + updateActiveThreadOps.push({ + type: 'replace', + payload: { + id: activeThread, + threadInfo: { + ...state.threadStore.threadInfos[activeThread], + currentUser: { + ...state.threadStore.threadInfos[activeThread].currentUser, + unread: false, }, }, }, - }; + }); } const oldActiveThread = activeThreadSelector(oldState); @@ -307,5 +329,24 @@ }; } + if (updateActiveThreadOps.length > 0) { + state = { + ...state, + threadStore: threadStoreOpsHandlers.processStoreOperations( + state.threadStore, + updateActiveThreadOps, + ), + }; + storeOperations = { + ...storeOperations, + threadStoreOperations: [ + ...storeOperations.threadStoreOperations, + ...updateActiveThreadOps, + ], + }; + } + + processDBStoreOperations(storeOperations, state.currentUserInfo?.id); + return state; }