Skip to content

Commit

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

Depends on D9744

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/D9745
  • Loading branch information
palys-swm committed Nov 10, 2023
1 parent d27123e commit 8005048
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 22 deletions.
23 changes: 1 addition & 22 deletions lib/shared/update-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,7 @@ function keyForUpdateData(updateData: UpdateData): ?string {
}

function keyForUpdateInfo(updateInfo: ServerUpdateInfo): ?string {
if (
updateInfo.type === updateTypes.UPDATE_THREAD ||
updateInfo.type === updateTypes.JOIN_THREAD
) {
return updateInfo.threadInfo.id;
} else if (
updateInfo.type === updateTypes.UPDATE_THREAD_READ_STATUS ||
updateInfo.type === updateTypes.DELETE_THREAD
) {
return updateInfo.threadID;
} else if (updateInfo.type === updateTypes.UPDATE_ENTRY) {
const { id } = updateInfo.entryInfo;
invariant(id, 'should be set');
return id;
} else if (updateInfo.type === updateTypes.UPDATE_CURRENT_USER) {
return updateInfo.currentUserInfo.id;
} else if (updateInfo.type === updateTypes.DELETE_ACCOUNT) {
return updateInfo.deletedUserID;
} else if (updateInfo.type === updateTypes.UPDATE_USER) {
return updateInfo.updatedUserID;
}
return null;
return updateSpecs[updateInfo.type].keyForUpdateInfo?.(updateInfo) ?? null;
}

// ESLint doesn't recognize that invariant always throws
Expand Down
3 changes: 3 additions & 0 deletions lib/shared/updates/delete-account-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ export const deleteAccountSpec: UpdateSpec<
keyForUpdateData(data: AccountDeletionUpdateData) {
return data.deletedUserID;
},
keyForUpdateInfo(info: AccountDeletionUpdateInfo) {
return info.deletedUserID;
},
typesOfReplacedUpdatesForMatchingKey: 'all_types',
infoValidator: tShape<AccountDeletionUpdateInfo>({
type: tNumber(updateTypes.DELETE_ACCOUNT),
Expand Down
3 changes: 3 additions & 0 deletions lib/shared/updates/delete-thread-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ export const deleteThreadSpec: UpdateSpec<
keyForUpdateData(data: ThreadDeletionUpdateData) {
return data.threadID;
},
keyForUpdateInfo(info: ThreadDeletionUpdateInfo) {
return info.threadID;
},
typesOfReplacedUpdatesForMatchingKey: 'all_types',
infoValidator: tShape<ThreadDeletionUpdateInfo>({
type: tNumber(updateTypes.DELETE_THREAD),
Expand Down
3 changes: 3 additions & 0 deletions lib/shared/updates/join-thread-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ export const joinThreadSpec: UpdateSpec<
keyForUpdateData(data: ThreadJoinUpdateData) {
return data.threadID;
},
keyForUpdateInfo(info: ThreadJoinUpdateInfo) {
return info.threadInfo.id;
},
typesOfReplacedUpdatesForMatchingKey: 'all_types',
infoValidator: tShape<ThreadJoinUpdateInfo>({
type: tNumber(updateTypes.JOIN_THREAD),
Expand Down
3 changes: 3 additions & 0 deletions lib/shared/updates/update-current-user-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ export const updateCurrentUserSpec: UpdateSpec<
keyForUpdateData(data: CurrentUserUpdateData) {
return data.userID;
},
keyForUpdateInfo(info: CurrentUserUpdateInfo) {
return info.currentUserInfo.id;
},
typesOfReplacedUpdatesForMatchingKey: 'all_types',
infoValidator: tShape<CurrentUserUpdateInfo>({
type: tNumber(updateTypes.UPDATE_CURRENT_USER),
Expand Down
5 changes: 5 additions & 0 deletions lib/shared/updates/update-entry-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ export const updateEntrySpec: UpdateSpec<
keyForUpdateData(data: EntryUpdateData) {
return data.entryID;
},
keyForUpdateInfo(info: EntryUpdateInfo) {
const { id } = info.entryInfo;
invariant(id, 'should be set');
return id;
},
typesOfReplacedUpdatesForMatchingKey: 'all_types',
infoValidator: tShape<EntryUpdateInfo>({
type: tNumber(updateTypes.UPDATE_ENTRY),
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 @@ -92,6 +92,7 @@ export type UpdateSpec<
) => ?UpdateInfo,
+deleteCondition: ?UpdateTypes,
+keyForUpdateData?: (data: Data) => string,
+keyForUpdateInfo?: (info: UpdateInfo) => string,
+typesOfReplacedUpdatesForMatchingKey: ?UpdateTypes,
+infoValidator: TType<UpdateInfo>,
};
3 changes: 3 additions & 0 deletions lib/shared/updates/update-thread-read-status-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ export const updateThreadReadStatusSpec: UpdateSpec<
keyForUpdateData(data: ThreadReadStatusUpdateData) {
return data.threadID;
},
keyForUpdateInfo(info: ThreadReadStatusUpdateInfo) {
return info.threadID;
},
typesOfReplacedUpdatesForMatchingKey: new Set([
updateTypes.UPDATE_THREAD_READ_STATUS,
]),
Expand Down
3 changes: 3 additions & 0 deletions lib/shared/updates/update-thread-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ export const updateThreadSpec: UpdateSpec<
keyForUpdateData(data: ThreadUpdateData) {
return data.threadID;
},
keyForUpdateInfo(info: ThreadUpdateInfo) {
return info.threadInfo.id;
},
typesOfReplacedUpdatesForMatchingKey: new Set([
updateTypes.UPDATE_THREAD_READ_STATUS,
]),
Expand Down
3 changes: 3 additions & 0 deletions lib/shared/updates/update-user-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ export const updateUserSpec: UpdateSpec<
keyForUpdateData(data: UserUpdateData) {
return data.updatedUserID;
},
keyForUpdateInfo(info: UserUpdateInfo) {
return info.updatedUserID;
},
typesOfReplacedUpdatesForMatchingKey: null,
infoValidator: tShape<UserUpdateInfo>({
type: tNumber(updateTypes.UPDATE_USER),
Expand Down

0 comments on commit 8005048

Please sign in to comment.