From 7d967c7ac547dd3e9529b48f9c628c631603b8ca Mon Sep 17 00:00:00 2001 From: Bartosz Grajdek Date: Thu, 7 Sep 2023 13:40:25 +0200 Subject: [PATCH 1/5] [TS migration] Migrate 'OnyxUpdates.js' lib to TypeScript --- src/ONYXKEYS.ts | 2 +- src/libs/actions/{OnyxUpdates.js => OnyxUpdates.ts} | 7 +------ src/types/onyx/UpdatesFromServer.ts | 6 ++++++ src/types/onyx/index.ts | 2 ++ 4 files changed, 10 insertions(+), 7 deletions(-) rename src/libs/actions/{OnyxUpdates.js => OnyxUpdates.ts} (73%) create mode 100644 src/types/onyx/UpdatesFromServer.ts diff --git a/src/ONYXKEYS.ts b/src/ONYXKEYS.ts index b3ef88cc0094..54c3808ea54e 100755 --- a/src/ONYXKEYS.ts +++ b/src/ONYXKEYS.ts @@ -366,7 +366,7 @@ type OnyxValues = { [ONYXKEYS.SELECTED_TAB]: string; [ONYXKEYS.RECEIPT_MODAL]: OnyxTypes.ReceiptModal; [ONYXKEYS.MAPBOX_ACCESS_TOKEN]: OnyxTypes.MapboxAccessToken; - [ONYXKEYS.ONYX_UPDATES_FROM_SERVER]: number; + [ONYXKEYS.ONYX_UPDATES_FROM_SERVER]: OnyxTypes.UpdatesFromServer; [ONYXKEYS.ONYX_UPDATES_LAST_UPDATE_ID_APPLIED_TO_CLIENT]: number; [ONYXKEYS.MAX_CANVAS_AREA]: number; [ONYXKEYS.MAX_CANVAS_HEIGHT]: number; diff --git a/src/libs/actions/OnyxUpdates.js b/src/libs/actions/OnyxUpdates.ts similarity index 73% rename from src/libs/actions/OnyxUpdates.js rename to src/libs/actions/OnyxUpdates.ts index e582016f0109..f5810b7761f2 100644 --- a/src/libs/actions/OnyxUpdates.js +++ b/src/libs/actions/OnyxUpdates.ts @@ -1,12 +1,7 @@ import Onyx from 'react-native-onyx'; import ONYXKEYS from '../../ONYXKEYS'; -/** - * - * @param {Number} [lastUpdateID] - * @param {Number} [previousUpdateID] - */ -function saveUpdateIDs(lastUpdateID = 0, previousUpdateID = 0) { +function saveUpdateIDs(lastUpdateID = 0, previousUpdateID = 0): void { // Return early if there were no updateIDs if (!lastUpdateID) { return; diff --git a/src/types/onyx/UpdatesFromServer.ts b/src/types/onyx/UpdatesFromServer.ts new file mode 100644 index 000000000000..785ce4ae7675 --- /dev/null +++ b/src/types/onyx/UpdatesFromServer.ts @@ -0,0 +1,6 @@ +type UpdatesFromServer = { + lastUpdateIDFromServer: number; + previousUpdateIDFromServer: number; +}; + +export default UpdatesFromServer; diff --git a/src/types/onyx/index.ts b/src/types/onyx/index.ts index 039448fac531..2c6575c61a2d 100644 --- a/src/types/onyx/index.ts +++ b/src/types/onyx/index.ts @@ -43,6 +43,7 @@ import SecurityGroup from './SecurityGroup'; import Transaction from './Transaction'; import Form, {AddDebitCardForm} from './Form'; import RecentWaypoints from './RecentWaypoints'; +import UpdatesFromServer from './UpdatesFromServer'; export type { Account, @@ -91,4 +92,5 @@ export type { Form, AddDebitCardForm, RecentWaypoints, + UpdatesFromServer, }; From 040e52f455acf664c4fe2039a86a1fce24d0ad9d Mon Sep 17 00:00:00 2001 From: Bartosz Grajdek Date: Tue, 12 Sep 2023 15:18:15 +0200 Subject: [PATCH 2/5] resolve conflicts --- src/libs/actions/OnyxUpdates.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/OnyxUpdates.ts b/src/libs/actions/OnyxUpdates.ts index d8111753fc30..783ea6904d35 100644 --- a/src/libs/actions/OnyxUpdates.ts +++ b/src/libs/actions/OnyxUpdates.ts @@ -56,8 +56,8 @@ function applyPusherOnyxUpdates(updates: OnyxUpdate[]) { * @param [updateParams.response] Exists if updateParams.type === 'https' * @param [updateParams.updates] Exists if updateParams.type === 'pusher' */ -function apply({lastUpdateID, type, request, response, updates}: Merge): Promise; function apply({lastUpdateID, type, request, response, updates}: Merge): Promise; +function apply({lastUpdateID, type, request, response, updates}: Merge): Promise; function apply({lastUpdateID, type, request, response, updates}: OnyxUpdatesFromServer): Promise | undefined { console.debug(`[OnyxUpdateManager] Applying update type: ${type} with lastUpdateID: ${lastUpdateID}`, {request, response, updates}); From 0dac6d87ac23321d35702c819fd32fc817b6ab24 Mon Sep 17 00:00:00 2001 From: Bartosz Grajdek Date: Wed, 13 Sep 2023 14:38:54 +0200 Subject: [PATCH 3/5] Resolve conflicts --- src/libs/actions/OnyxUpdates.ts | 7 ++++--- src/types/onyx/OnyxUpdatesFromServer.ts | 9 +++++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/libs/actions/OnyxUpdates.ts b/src/libs/actions/OnyxUpdates.ts index 783ea6904d35..e8ede650863a 100644 --- a/src/libs/actions/OnyxUpdates.ts +++ b/src/libs/actions/OnyxUpdates.ts @@ -1,4 +1,4 @@ -import Onyx, {OnyxEntry, OnyxUpdate} from 'react-native-onyx'; +import Onyx, {OnyxEntry} from 'react-native-onyx'; import {Merge} from 'type-fest'; import PusherUtils from '../PusherUtils'; import ONYXKEYS from '../../ONYXKEYS'; @@ -6,6 +6,7 @@ import * as QueuedOnyxUpdates from './QueuedOnyxUpdates'; import CONST from '../../CONST'; import {OnyxUpdatesFromServer, Request} from '../../types/onyx'; import Response from '../../types/onyx/Response'; +import {OnyxUpdateEvent} from '../../types/onyx/OnyxUpdatesFromServer'; // This key needs to be separate from ONYXKEYS.ONYX_UPDATES_FROM_SERVER so that it can be updated without triggering the callback when the server IDs are updated. If that // callback were triggered it would lead to duplicate processing of server updates. @@ -43,7 +44,7 @@ function applyHTTPSOnyxUpdates(request: Request, response: Response) { }); } -function applyPusherOnyxUpdates(updates: OnyxUpdate[]) { +function applyPusherOnyxUpdates(updates: OnyxUpdateEvent[]) { console.debug('[OnyxUpdateManager] Applying pusher update'); const pusherEventPromises = updates.map((update) => PusherUtils.triggerMultiEventHandler(update.eventType, update.data)); return Promise.all(pusherEventPromises).then(() => { @@ -56,7 +57,7 @@ function applyPusherOnyxUpdates(updates: OnyxUpdate[]) { * @param [updateParams.response] Exists if updateParams.type === 'https' * @param [updateParams.updates] Exists if updateParams.type === 'pusher' */ -function apply({lastUpdateID, type, request, response, updates}: Merge): Promise; +function apply({lastUpdateID, type, request, response, updates}: Merge): Promise; function apply({lastUpdateID, type, request, response, updates}: Merge): Promise; function apply({lastUpdateID, type, request, response, updates}: OnyxUpdatesFromServer): Promise | undefined { console.debug(`[OnyxUpdateManager] Applying update type: ${type} with lastUpdateID: ${lastUpdateID}`, {request, response, updates}); diff --git a/src/types/onyx/OnyxUpdatesFromServer.ts b/src/types/onyx/OnyxUpdatesFromServer.ts index 02a96d4ce230..50b1503b90bd 100644 --- a/src/types/onyx/OnyxUpdatesFromServer.ts +++ b/src/types/onyx/OnyxUpdatesFromServer.ts @@ -2,13 +2,18 @@ import {OnyxUpdate} from 'react-native-onyx'; import Request from './Request'; import Response from './Response'; +type OnyxUpdateEvent = { + eventType: string; + data: OnyxUpdate[]; +}; + type OnyxUpdatesFromServer = { type: 'https' | 'pusher'; lastUpdateID: number | string; previousUpdateID: number | string; request?: Request; response?: Response; - updates?: OnyxUpdate[]; + updates?: OnyxUpdateEvent[]; }; -export default OnyxUpdatesFromServer; +export type {OnyxUpdatesFromServer, OnyxUpdateEvent}; From 083a30e20a14518163c0e7523d9835a2c6df802c Mon Sep 17 00:00:00 2001 From: Bartosz Grajdek Date: Wed, 13 Sep 2023 14:39:04 +0200 Subject: [PATCH 4/5] Remove void returns --- src/libs/actions/OnyxUpdates.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/OnyxUpdates.ts b/src/libs/actions/OnyxUpdates.ts index e8ede650863a..94f7b1549b13 100644 --- a/src/libs/actions/OnyxUpdates.ts +++ b/src/libs/actions/OnyxUpdates.ts @@ -82,7 +82,7 @@ function apply({lastUpdateID, type, request, response, updates}: OnyxUpdatesFrom * @param [updateParams.response] Exists if updateParams.type === 'https' * @param [updateParams.updates] Exists if updateParams.type === 'pusher' */ -function saveUpdateInformation(updateParams: OnyxUpdatesFromServer): void { +function saveUpdateInformation(updateParams: OnyxUpdatesFromServer) { // Always use set() here so that the updateParams are never merged and always unique to the request that came in Onyx.set(ONYXKEYS.ONYX_UPDATES_FROM_SERVER, updateParams); } From 84f00e682a0b66b800503d53ae40816c2de7a198 Mon Sep 17 00:00:00 2001 From: Bartosz Grajdek Date: Wed, 13 Sep 2023 14:41:10 +0200 Subject: [PATCH 5/5] Fix imports --- src/libs/actions/OnyxUpdates.ts | 3 +-- src/types/onyx/index.ts | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libs/actions/OnyxUpdates.ts b/src/libs/actions/OnyxUpdates.ts index 94f7b1549b13..50a4fdffc3ae 100644 --- a/src/libs/actions/OnyxUpdates.ts +++ b/src/libs/actions/OnyxUpdates.ts @@ -4,9 +4,8 @@ import PusherUtils from '../PusherUtils'; import ONYXKEYS from '../../ONYXKEYS'; import * as QueuedOnyxUpdates from './QueuedOnyxUpdates'; import CONST from '../../CONST'; -import {OnyxUpdatesFromServer, Request} from '../../types/onyx'; +import {OnyxUpdatesFromServer, OnyxUpdateEvent, Request} from '../../types/onyx'; import Response from '../../types/onyx/Response'; -import {OnyxUpdateEvent} from '../../types/onyx/OnyxUpdatesFromServer'; // This key needs to be separate from ONYXKEYS.ONYX_UPDATES_FROM_SERVER so that it can be updated without triggering the callback when the server IDs are updated. If that // callback were triggered it would lead to duplicate processing of server updates. diff --git a/src/types/onyx/index.ts b/src/types/onyx/index.ts index d908c0b36ce1..47262a14540e 100644 --- a/src/types/onyx/index.ts +++ b/src/types/onyx/index.ts @@ -33,7 +33,7 @@ import ReimbursementAccountDraft from './ReimbursementAccountDraft'; import WalletTransfer from './WalletTransfer'; import ReceiptModal from './ReceiptModal'; import MapboxAccessToken from './MapboxAccessToken'; -import OnyxUpdatesFromServer from './OnyxUpdatesFromServer'; +import {OnyxUpdatesFromServer, OnyxUpdateEvent} from './OnyxUpdatesFromServer'; import Download from './Download'; import PolicyMember from './PolicyMember'; import Policy from './Policy'; @@ -93,6 +93,7 @@ export type { Form, AddDebitCardForm, OnyxUpdatesFromServer, + OnyxUpdateEvent, RecentWaypoints, RecentlyUsedCategories, };