diff --git a/lib/shared/state-sync/current-user-state-sync-spec.js b/lib/shared/state-sync/current-user-state-sync-spec.js --- a/lib/shared/state-sync/current-user-state-sync-spec.js +++ b/lib/shared/state-sync/current-user-state-sync-spec.js @@ -2,11 +2,24 @@ import { createSelector } from 'reselect'; -import type { StateSyncSpec } from './state-sync-spec.js'; +import type { StateSyncSpec, BoundStateSyncSpec } from './state-sync-spec.js'; import type { AppState } from '../../types/redux-types'; import { type CurrentUserInfo } from '../../types/user-types.js'; import { hash } from '../../utils/objects.js'; +const selector: ( + state: AppState, +) => BoundStateSyncSpec = + createSelector( + (state: AppState) => state.currentUserInfo, + (currentUserInfo: ?CurrentUserInfo) => ({ + ...currentUserStateSyncSpec, + getInfoHash: () => hash(currentUserInfo), + getAllInfosHash: () => hash(currentUserInfo), + getIDs: () => ([]: string[]), + }), + ); + export const currentUserStateSyncSpec: StateSyncSpec< CurrentUserInfo, CurrentUserInfo, @@ -16,13 +29,5 @@ findStoreInconsistencies() { return undefined; }, - selector: createSelector( - (state: AppState) => state.currentUserInfo, - currentUserInfo => ({ - ...currentUserStateSyncSpec, - getInfoHash: () => hash(currentUserInfo), - getAllInfosHash: () => hash(currentUserInfo), - getIDs: () => [], - }), - ), + selector, }); diff --git a/lib/shared/state-sync/entries-state-sync-spec.js b/lib/shared/state-sync/entries-state-sync-spec.js --- a/lib/shared/state-sync/entries-state-sync-spec.js +++ b/lib/shared/state-sync/entries-state-sync-spec.js @@ -3,7 +3,7 @@ import _isEqual from 'lodash/fp/isEqual.js'; import { createSelector } from 'reselect'; -import type { StateSyncSpec } from './state-sync-spec.js'; +import type { StateSyncSpec, BoundStateSyncSpec } from './state-sync-spec.js'; import { type CalendarQuery, type RawEntryInfos, @@ -26,6 +26,24 @@ serverEntryInfosObject, } from '../entry-utils.js'; +const selector: ( + state: AppState, +) => BoundStateSyncSpec< + RawEntryInfos, + RawEntryInfo, + $ReadOnlyArray, +> = createSelector( + (state: AppState) => state.entryStore.entryInfos, + (entryInfos: RawEntryInfos) => ({ + ...entriesStateSyncSpec, + getInfoHash: (id: string) => hash(entryInfos[`${ashoatKeyserverID}|${id}`]), + getAllInfosHash: (calendarQuery: CalendarQuery) => + getEntryInfosHash(entryInfos, calendarQuery), + getIDs: (calendarQuery: CalendarQuery) => + getEntryIDs(entryInfos, calendarQuery), + }), +); + export const entriesStateSyncSpec: StateSyncSpec< RawEntryInfos, RawEntryInfo, @@ -71,18 +89,7 @@ }, ]; }, - selector: createSelector( - (state: AppState) => state.entryStore.entryInfos, - entryInfos => ({ - ...entriesStateSyncSpec, - getInfoHash: (id: string) => - hash(entryInfos[`${ashoatKeyserverID}|${id}`]), - getAllInfosHash: (calendarQuery: CalendarQuery) => - getEntryInfosHash(entryInfos, calendarQuery), - getIDs: (calendarQuery: CalendarQuery) => - getEntryIDs(entryInfos, calendarQuery), - }), - ), + selector, }); const emptyArray: $ReadOnlyArray = diff --git a/lib/shared/state-sync/threads-state-sync-spec.js b/lib/shared/state-sync/threads-state-sync-spec.js --- a/lib/shared/state-sync/threads-state-sync-spec.js +++ b/lib/shared/state-sync/threads-state-sync-spec.js @@ -3,7 +3,7 @@ import _isEqual from 'lodash/fp/isEqual.js'; import { createSelector } from 'reselect'; -import type { StateSyncSpec } from './state-sync-spec.js'; +import type { StateSyncSpec, BoundStateSyncSpec } from './state-sync-spec.js'; import type { AppState } from '../../types/redux-types.js'; import { reportTypes, @@ -21,6 +21,27 @@ import { sanitizeActionSecrets } from '../../utils/sanitization.js'; import { ashoatKeyserverID } from '../../utils/validation-utils.js'; +const selector: ( + state: AppState, +) => BoundStateSyncSpec< + RawThreadInfos, + RawThreadInfo, + $ReadOnlyArray, +> = createSelector( + (state: AppState) => state.integrityStore.threadHashes, + (state: AppState) => state.integrityStore.threadHashingStatus === 'completed', + (threadHashes: { +[string]: number }, threadHashingComplete: boolean) => ({ + ...threadsStateSyncSpec, + getInfoHash: (id: string) => threadHashes[`${ashoatKeyserverID}|${id}`], + getAllInfosHash: threadHashingComplete + ? () => combineUnorderedHashes(values(threadHashes)) + : () => null, + getIDs: threadHashingComplete + ? () => Object.keys(threadHashes).map(id => id.split('|')[1]) + : () => null, + }), +); + export const threadsStateSyncSpec: StateSyncSpec< RawThreadInfos, RawThreadInfo, @@ -53,21 +74,7 @@ }, ]; }, - selector: createSelector( - (state: AppState) => state.integrityStore.threadHashes, - (state: AppState) => - state.integrityStore.threadHashingStatus === 'completed', - (threadHashes, threadHashingComplete) => ({ - ...threadsStateSyncSpec, - getInfoHash: (id: string) => threadHashes[`${ashoatKeyserverID}|${id}`], - getAllInfosHash: threadHashingComplete - ? () => combineUnorderedHashes(values(threadHashes)) - : () => null, - getIDs: threadHashingComplete - ? () => Object.keys(threadHashes).map(id => id.split('|')[1]) - : () => null, - }), - ), + selector, }); const emptyArray: $ReadOnlyArray = diff --git a/lib/shared/state-sync/users-state-sync-spec.js b/lib/shared/state-sync/users-state-sync-spec.js --- a/lib/shared/state-sync/users-state-sync-spec.js +++ b/lib/shared/state-sync/users-state-sync-spec.js @@ -3,7 +3,7 @@ import _isEqual from 'lodash/fp/isEqual.js'; import { createSelector } from 'reselect'; -import type { StateSyncSpec } from './state-sync-spec.js'; +import type { StateSyncSpec, BoundStateSyncSpec } from './state-sync-spec.js'; import type { AppState } from '../../types/redux-types'; import { reportTypes, @@ -17,6 +17,23 @@ import { generateReportID } from '../../utils/report-utils.js'; import { sanitizeActionSecrets } from '../../utils/sanitization.js'; +const selector: ( + state: AppState, +) => BoundStateSyncSpec< + UserInfos, + UserInfo, + $ReadOnlyArray, +> = createSelector( + (state: AppState) => state.userStore.userInfos, + (userInfos: UserInfos) => ({ + ...usersStateSyncSpec, + getInfoHash: (id: string) => hash(userInfos[id]), + getAllInfosHash: () => + combineUnorderedHashes(Object.values(userInfos).map(hash)), + getIDs: () => Object.keys(userInfos), + }), +); + export const usersStateSyncSpec: StateSyncSpec< UserInfos, UserInfo, @@ -52,16 +69,7 @@ }, ]; }, - selector: createSelector( - (state: AppState) => state.userStore.userInfos, - userInfos => ({ - ...usersStateSyncSpec, - getInfoHash: (id: string) => hash(userInfos[id]), - getAllInfosHash: () => - combineUnorderedHashes(Object.values(userInfos).map(hash)), - getIDs: () => Object.keys(userInfos), - }), - ), + selector, }); const emptyArray: $ReadOnlyArray = [];