diff --git a/lib/reducers/user-reducer.js b/lib/reducers/user-reducer.js --- a/lib/reducers/user-reducer.js +++ b/lib/reducers/user-reducer.js @@ -17,6 +17,7 @@ updateUserAvatarActionTypes, resetUserStateActionType, } from '../actions/user-actions.js'; +import { updateSpecs } from '../shared/updates/update-specs.js'; import type { BaseAction } from '../types/redux-types.js'; import { type UserInconsistencyReportCreationRequest, @@ -74,14 +75,15 @@ action.type === incrementalStateSyncActionType || action.type === processUpdatesActionType ) { - for (const update of action.payload.updatesResult.newUpdates) { - if ( - update.type === updateTypes.UPDATE_CURRENT_USER && - !_isEqual(update.currentUserInfo)(state) - ) { - return update.currentUserInfo; - } - } + return action.payload.updatesResult.newUpdates.reduce( + (reducedState, update) => { + const { reduceCurrentUser } = updateSpecs[update.type]; + return reduceCurrentUser + ? reduceCurrentUser(reducedState, update) + : reducedState; + }, + state, + ); } else if (action.type === processServerRequestsActionType) { const checkStateRequest = action.payload.serverRequests.find( candidate => candidate.type === serverRequestTypes.CHECK_STATE, diff --git a/lib/shared/updates/update-current-user-spec.js b/lib/shared/updates/update-current-user-spec.js --- a/lib/shared/updates/update-current-user-spec.js +++ b/lib/shared/updates/update-current-user-spec.js @@ -1,7 +1,17 @@ // @flow +import _isEqual from 'lodash/fp/isEqual.js'; + import type { UpdateSpec } from './update-spec.js'; import type { CurrentUserUpdateInfo } from '../../types/update-types.js'; +import type { CurrentUserInfo } from '../../types/user-types.js'; export const updateCurrentUserSpec: UpdateSpec = - Object.freeze({}); + Object.freeze({ + reduceCurrentUser(state: ?CurrentUserInfo, update: CurrentUserUpdateInfo) { + if (!_isEqual(update.currentUserInfo)(state)) { + return update.currentUserInfo; + } + return state; + }, + }); diff --git a/lib/shared/updates/update-spec.js b/lib/shared/updates/update-spec.js --- a/lib/shared/updates/update-spec.js +++ b/lib/shared/updates/update-spec.js @@ -4,6 +4,7 @@ import type { RawEntryInfo } from '../../types/entry-types.js'; import type { RawThreadInfos } from '../../types/thread-types.js'; import type { ClientUpdateInfo } from '../../types/update-types.js'; +import type { CurrentUserInfo } from '../../types/user-types.js'; export type UpdateSpec = { +generateOpsForThreadUpdates?: ( @@ -15,4 +16,8 @@ mergedEntryInfos: Array, update: UpdateInfo, ) => void, + +reduceCurrentUser?: ( + state: ?CurrentUserInfo, + update: UpdateInfo, + ) => ?CurrentUserInfo, };