Skip to content

Commit

Permalink
[lib] Convert update data into raw update info in a spec
Browse files Browse the repository at this point in the history
Summary:
Move the code to specs.

Depends on D9745

https://linear.app/comm/issue/ENG-4233/introduce-update-specs-containing-functions-from-update-utils

Test Plan: Reintroduce the old code and check if it gives the same result.

Reviewers: kamil, inka, bartek

Reviewed By: kamil

Subscribers: ashoat, wyilio

Differential Revision: https://phab.comm.dev/D9746
  • Loading branch information
palys-swm committed Nov 10, 2023
1 parent 8005048 commit 52fe10d
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 70 deletions.
71 changes: 1 addition & 70 deletions lib/shared/update-utils.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// @flow

import invariant from 'invariant';
import _maxBy from 'lodash/fp/maxBy.js';

import { updateSpecs } from './updates/update-specs.js';
import { updateTypes } from '../types/update-types-enum.js';
import {
type ServerUpdateInfo,
type UpdateData,
Expand All @@ -29,78 +27,11 @@ function keyForUpdateInfo(updateInfo: ServerUpdateInfo): ?string {
return updateSpecs[updateInfo.type].keyForUpdateInfo?.(updateInfo) ?? null;
}

// ESLint doesn't recognize that invariant always throws
// eslint-disable-next-line consistent-return
function rawUpdateInfoFromUpdateData(
updateData: UpdateData,
id: string,
): RawUpdateInfo {
if (updateData.type === updateTypes.DELETE_ACCOUNT) {
return {
type: updateTypes.DELETE_ACCOUNT,
id,
time: updateData.time,
deletedUserID: updateData.deletedUserID,
};
} else if (updateData.type === updateTypes.UPDATE_THREAD) {
return {
type: updateTypes.UPDATE_THREAD,
id,
time: updateData.time,
threadID: updateData.threadID,
};
} else if (updateData.type === updateTypes.UPDATE_THREAD_READ_STATUS) {
return {
type: updateTypes.UPDATE_THREAD_READ_STATUS,
id,
time: updateData.time,
threadID: updateData.threadID,
unread: updateData.unread,
};
} else if (updateData.type === updateTypes.DELETE_THREAD) {
return {
type: updateTypes.DELETE_THREAD,
id,
time: updateData.time,
threadID: updateData.threadID,
};
} else if (updateData.type === updateTypes.JOIN_THREAD) {
return {
type: updateTypes.JOIN_THREAD,
id,
time: updateData.time,
threadID: updateData.threadID,
};
} else if (updateData.type === updateTypes.BAD_DEVICE_TOKEN) {
return {
type: updateTypes.BAD_DEVICE_TOKEN,
id,
time: updateData.time,
deviceToken: updateData.deviceToken,
};
} else if (updateData.type === updateTypes.UPDATE_ENTRY) {
return {
type: updateTypes.UPDATE_ENTRY,
id,
time: updateData.time,
entryID: updateData.entryID,
};
} else if (updateData.type === updateTypes.UPDATE_CURRENT_USER) {
return {
type: updateTypes.UPDATE_CURRENT_USER,
id,
time: updateData.time,
};
} else if (updateData.type === updateTypes.UPDATE_USER) {
return {
type: updateTypes.UPDATE_USER,
id,
time: updateData.time,
updatedUserID: updateData.updatedUserID,
};
} else {
invariant(false, `unrecognized updateType ${updateData.type}`);
}
return updateSpecs[updateData.type].rawInfoFromData(updateData, id);
}

export {
Expand Down
8 changes: 8 additions & 0 deletions lib/shared/updates/bad-device-token-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ export const badDeviceTokenSpec: UpdateSpec<
const { deviceToken } = data;
return JSON.stringify({ deviceToken });
},
rawInfoFromData(data: BadDeviceTokenUpdateData, id: string) {
return {
type: updateTypes.BAD_DEVICE_TOKEN,
id,
time: data.time,
deviceToken: data.deviceToken,
};
},
updateInfoFromRawInfo(info: BadDeviceTokenRawUpdateInfo) {
return {
type: updateTypes.BAD_DEVICE_TOKEN,
Expand Down
8 changes: 8 additions & 0 deletions lib/shared/updates/delete-account-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ export const deleteAccountSpec: UpdateSpec<
updateContentForServerDB(data: AccountDeletionUpdateData) {
return JSON.stringify({ deletedUserID: data.deletedUserID });
},
rawInfoFromData(data: AccountDeletionUpdateData, id: string) {
return {
type: updateTypes.DELETE_ACCOUNT,
id,
time: data.time,
deletedUserID: data.deletedUserID,
};
},
updateInfoFromRawInfo(info: AccountDeletionRawUpdateInfo) {
return {
type: updateTypes.DELETE_ACCOUNT,
Expand Down
8 changes: 8 additions & 0 deletions lib/shared/updates/delete-thread-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ export const deleteThreadSpec: UpdateSpec<
const { threadID } = data;
return JSON.stringify({ threadID });
},
rawInfoFromData(data: ThreadDeletionUpdateData, id: string) {
return {
type: updateTypes.DELETE_THREAD,
id,
time: data.time,
threadID: data.threadID,
};
},
updateInfoFromRawInfo(info: ThreadDeletionRawUpdateInfo) {
return {
type: updateTypes.DELETE_THREAD,
Expand Down
8 changes: 8 additions & 0 deletions lib/shared/updates/join-thread-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ export const joinThreadSpec: UpdateSpec<
detailedThreadID: update.threadID,
};
},
rawInfoFromData(data: ThreadJoinUpdateData, id: string) {
return {
type: updateTypes.JOIN_THREAD,
id,
time: data.time,
threadID: data.threadID,
};
},
updateInfoFromRawInfo(
info: ThreadJoinRawUpdateInfo,
params: UpdateInfoFromRawInfoParams,
Expand Down
7 changes: 7 additions & 0 deletions lib/shared/updates/update-current-user-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ export const updateCurrentUserSpec: UpdateSpec<
currentUser: true,
};
},
rawInfoFromData(data: CurrentUserUpdateData, id: string) {
return {
type: updateTypes.UPDATE_CURRENT_USER,
id,
time: data.time,
};
},
updateInfoFromRawInfo(
info: CurrentUserRawUpdateInfo,
params: UpdateInfoFromRawInfoParams,
Expand Down
8 changes: 8 additions & 0 deletions lib/shared/updates/update-entry-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ export const updateEntrySpec: UpdateSpec<
entryID: update.entryID,
};
},
rawInfoFromData(data: EntryUpdateData, id: string) {
return {
type: updateTypes.UPDATE_ENTRY,
id,
time: data.time,
entryID: data.entryID,
};
},
updateInfoFromRawInfo(
info: EntryRawUpdateInfo,
params: UpdateInfoFromRawInfoParams,
Expand Down
1 change: 1 addition & 0 deletions lib/shared/updates/update-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export type UpdateSpec<
+currentUser?: boolean,
+userID?: string,
},
+rawInfoFromData: (data: Data, id: string) => RawInfo,
+updateInfoFromRawInfo: (
info: RawInfo,
params: UpdateInfoFromRawInfoParams,
Expand Down
9 changes: 9 additions & 0 deletions lib/shared/updates/update-thread-read-status-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ export const updateThreadReadStatusSpec: UpdateSpec<
const { threadID, unread } = data;
return JSON.stringify({ threadID, unread });
},
rawInfoFromData(data: ThreadReadStatusUpdateData, id: string) {
return {
type: updateTypes.UPDATE_THREAD_READ_STATUS,
id,
time: data.time,
threadID: data.threadID,
unread: data.unread,
};
},
updateInfoFromRawInfo(info: ThreadReadStatusRawUpdateInfo) {
return {
type: updateTypes.UPDATE_THREAD_READ_STATUS,
Expand Down
8 changes: 8 additions & 0 deletions lib/shared/updates/update-thread-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ export const updateThreadSpec: UpdateSpec<
threadID: update.threadID,
};
},
rawInfoFromData(data: ThreadUpdateData, id: string) {
return {
type: updateTypes.UPDATE_THREAD,
id,
time: data.time,
threadID: data.threadID,
};
},
updateInfoFromRawInfo(
info: ThreadRawUpdateInfo,
params: UpdateInfoFromRawInfoParams,
Expand Down
8 changes: 8 additions & 0 deletions lib/shared/updates/update-user-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ export const updateUserSpec: UpdateSpec<
const { updatedUserID } = data;
return JSON.stringify({ updatedUserID });
},
rawInfoFromData(data: UserUpdateData, id: string) {
return {
type: updateTypes.UPDATE_USER,
id,
time: data.time,
updatedUserID: data.updatedUserID,
};
},
updateInfoFromRawInfo(info: UserRawUpdateInfo) {
return {
type: updateTypes.UPDATE_USER,
Expand Down

0 comments on commit 52fe10d

Please sign in to comment.