diff --git a/lib/ops/user-store-ops.js b/lib/ops/user-store-ops.js new file mode 100644 --- /dev/null +++ b/lib/ops/user-store-ops.js @@ -0,0 +1,82 @@ +// @flow + +import { type BaseStoreOpsHandlers } from './base-ops.js'; +import type { UserInfo, UserInfos } from '../types/user-types.js'; + +// client types +export type ReplaceUserOperation = { + +type: 'replace_user', + +payload: UserInfo, +}; + +export type RemoveUsersOperation = { + +type: 'remove_users', + +payload: { +ids: $ReadOnlyArray }, +}; + +export type RemoveAllUsersOperation = { + +type: 'remove_all_users', +}; + +export type UserStoreOperation = + | ReplaceUserOperation + | RemoveUsersOperation + | RemoveAllUsersOperation; + +// SQLite types +export type ClientDBUserInfo = { + +id: string, + +username: ?string, + +relationshipStatus?: string, + +avatar?: ?string, +}; + +export type ClientDBReplaceUserOperation = { + +type: 'replace_user', + +payload: ClientDBUserInfo, +}; + +export type ClientDBUserStoreOperation = + | ClientDBReplaceUserOperation + | RemoveUsersOperation + | RemoveAllUsersOperation; + +const userStoreOpsHandlers: BaseStoreOpsHandlers< + UserInfos, + UserStoreOperation, + ClientDBUserStoreOperation, + UserInfos, + ClientDBUserInfo, +> = { + processStoreOperations( + userInfos: UserInfos, + ops: $ReadOnlyArray, + ): UserInfos { + if (ops.length === 0) { + return userInfos; + } + + let processedUserInfos = { ...userInfos }; + for (const operation: UserStoreOperation of ops) { + if (operation.type === 'replace_user') { + processedUserInfos[operation.payload.id] = operation.payload; + } else if (operation.type === 'remove_users') { + for (const id of operation.payload.ids) { + delete processedUserInfos[id]; + } + } else if (operation.type === 'remove_all_users') { + processedUserInfos = {}; + } + } + return processedUserInfos; + }, + + convertOpsToClientDBOps(): $ReadOnlyArray { + return []; + }, + translateClientDBData(): UserInfos { + return {}; + }, +}; + +export { userStoreOpsHandlers };