From 843ce9855be248ab0acf06d23ac7676eba269a7b Mon Sep 17 00:00:00 2001 From: Viktoryia Kliushun Date: Thu, 21 Sep 2023 08:19:31 +0200 Subject: [PATCH 1/3] [TS migration] migrate 'PusherUtils.js' lib --- package-lock.json | 12 +++--- package.json | 2 +- src/libs/{PusherUtils.js => PusherUtils.ts} | 46 +++++++-------------- 3 files changed, 23 insertions(+), 37 deletions(-) rename src/libs/{PusherUtils.js => PusherUtils.ts} (60%) diff --git a/package-lock.json b/package-lock.json index 64abe30d6187..437ff457775a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -50,7 +50,7 @@ "date-fns-tz": "^2.0.0", "dom-serializer": "^0.2.2", "domhandler": "^4.3.0", - "expensify-common": "git+ssh://git@github.com/Expensify/expensify-common.git#35bff866a8d345b460ea6256f0a0f0a8a7f81086", + "expensify-common": "git+ssh://git@github.com/Expensify/expensify-common.git#1609f1848cc0c2528064519c3ea48b4953a708ee", "fbjs": "^3.0.2", "htmlparser2": "^7.2.0", "idb-keyval": "^6.2.1", @@ -28106,8 +28106,8 @@ }, "node_modules/expensify-common": { "version": "1.0.0", - "resolved": "git+ssh://git@github.com/Expensify/expensify-common.git#35bff866a8d345b460ea6256f0a0f0a8a7f81086", - "integrity": "sha512-O0KbaMljSFyoZXcxtx5B2qBL+n0md3wYOrArMVT8N0W+4wKncn+p8/vrmVlUMHe/vDloUbVmUZNdHwOZNWdx3w==", + "resolved": "git+ssh://git@github.com/Expensify/expensify-common.git#1609f1848cc0c2528064519c3ea48b4953a708ee", + "integrity": "sha512-xfLRjBgBxr3rwQX/PCGjfmEJ2N1btMXN6dmNSpJLQFE7U1FNSGl1E958ISorZWgqpw9qfQxbgp5lZIDnMlH4hQ==", "license": "MIT", "dependencies": { "classnames": "2.3.1", @@ -68254,9 +68254,9 @@ } }, "expensify-common": { - "version": "git+ssh://git@github.com/Expensify/expensify-common.git#35bff866a8d345b460ea6256f0a0f0a8a7f81086", - "integrity": "sha512-O0KbaMljSFyoZXcxtx5B2qBL+n0md3wYOrArMVT8N0W+4wKncn+p8/vrmVlUMHe/vDloUbVmUZNdHwOZNWdx3w==", - "from": "expensify-common@git+ssh://git@github.com/Expensify/expensify-common.git#35bff866a8d345b460ea6256f0a0f0a8a7f81086", + "version": "git+ssh://git@github.com/Expensify/expensify-common.git#1609f1848cc0c2528064519c3ea48b4953a708ee", + "integrity": "sha512-xfLRjBgBxr3rwQX/PCGjfmEJ2N1btMXN6dmNSpJLQFE7U1FNSGl1E958ISorZWgqpw9qfQxbgp5lZIDnMlH4hQ==", + "from": "expensify-common@git+ssh://git@github.com/Expensify/expensify-common.git#1609f1848cc0c2528064519c3ea48b4953a708ee", "requires": { "classnames": "2.3.1", "clipboard": "2.0.4", diff --git a/package.json b/package.json index 6f0d4d70f768..87fc5fda85d8 100644 --- a/package.json +++ b/package.json @@ -92,7 +92,7 @@ "date-fns-tz": "^2.0.0", "dom-serializer": "^0.2.2", "domhandler": "^4.3.0", - "expensify-common": "git+ssh://git@github.com/Expensify/expensify-common.git#35bff866a8d345b460ea6256f0a0f0a8a7f81086", + "expensify-common": "git+ssh://git@github.com/Expensify/expensify-common.git#1609f1848cc0c2528064519c3ea48b4953a708ee", "fbjs": "^3.0.2", "htmlparser2": "^7.2.0", "idb-keyval": "^6.2.1", diff --git a/src/libs/PusherUtils.js b/src/libs/PusherUtils.ts similarity index 60% rename from src/libs/PusherUtils.js rename to src/libs/PusherUtils.ts index b4615d3c7d8b..1c2a3ff32e1a 100644 --- a/src/libs/PusherUtils.js +++ b/src/libs/PusherUtils.ts @@ -1,26 +1,25 @@ +import {OnyxUpdate} from 'react-native-onyx'; import CONFIG from '../CONFIG'; import Log from './Log'; import NetworkConnection from './NetworkConnection'; import * as Pusher from './Pusher/pusher'; import CONST from '../CONST'; +import {OnyxUpdateEvent, OnyxUpdatesFromServer} from '../types/onyx'; + +type PushJSON = OnyxUpdateEvent[] | OnyxUpdatesFromServer; + +type Callback = (data: OnyxUpdate[]) => Promise; + +type OnEvent = (pushJSON: PushJSON) => void; // Keeps track of all the callbacks that need triggered for each event type -const multiEventCallbackMapping = {}; +const multiEventCallbackMapping: Record = {}; -/** - * @param {String} eventType - * @param {Function} callback - */ -function subscribeToMultiEvent(eventType, callback) { +function subscribeToMultiEvent(eventType: string, callback: Callback) { multiEventCallbackMapping[eventType] = callback; } -/** - * @param {String} eventType - * @param {Mixed} data - * @returns {Promise} - */ -function triggerMultiEventHandler(eventType, data) { +function triggerMultiEventHandler(eventType: string, data: OnyxUpdate[]): Promise { if (!multiEventCallbackMapping[eventType]) { return Promise.resolve(); } @@ -29,18 +28,11 @@ function triggerMultiEventHandler(eventType, data) { /** * Abstraction around subscribing to private user channel events. Handles all logs and errors automatically. - * - * @param {String} eventName - * @param {String} accountID - * @param {Function} onEvent */ -function subscribeToPrivateUserChannelEvent(eventName, accountID, onEvent) { +function subscribeToPrivateUserChannelEvent(eventName: string, accountID: string, onEvent: OnEvent) { const pusherChannelName = `${CONST.PUSHER.PRIVATE_USER_CHANNEL_PREFIX}${accountID}${CONFIG.PUSHER.SUFFIX}`; - /** - * @param {Object} pushJSON - */ - function logPusherEvent(pushJSON) { + function logPusherEvent(pushJSON: PushJSON) { Log.info(`[Report] Handled ${eventName} event sent by Pusher`, false, pushJSON); } @@ -48,19 +40,13 @@ function subscribeToPrivateUserChannelEvent(eventName, accountID, onEvent) { NetworkConnection.triggerReconnectionCallbacks('Pusher re-subscribed to private user channel'); } - /** - * @param {*} pushJSON - */ - function onEventPush(pushJSON) { + function onEventPush(pushJSON: PushJSON) { logPusherEvent(pushJSON); onEvent(pushJSON); } - /** - * @param {*} error - */ - function onSubscriptionFailed(error) { - Log.hmmm('Failed to subscribe to Pusher channel', false, {error, pusherChannelName, eventName}); + function onSubscriptionFailed(error: Error) { + Log.hmmm('Failed to subscribe to Pusher channel', {error, pusherChannelName, eventName}); } Pusher.subscribe(pusherChannelName, eventName, onEventPush, onPusherResubscribeToPrivateUserChannel).catch(onSubscriptionFailed); } From edc7c4ee5558bc9ed08a0c8ef7e1ce1918723af7 Mon Sep 17 00:00:00 2001 From: Viktoryia Kliushun Date: Thu, 21 Sep 2023 10:35:08 +0200 Subject: [PATCH 2/3] Update Callback type --- src/libs/PusherUtils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/PusherUtils.ts b/src/libs/PusherUtils.ts index 1c2a3ff32e1a..4de7e8db7caa 100644 --- a/src/libs/PusherUtils.ts +++ b/src/libs/PusherUtils.ts @@ -8,7 +8,7 @@ import {OnyxUpdateEvent, OnyxUpdatesFromServer} from '../types/onyx'; type PushJSON = OnyxUpdateEvent[] | OnyxUpdatesFromServer; -type Callback = (data: OnyxUpdate[]) => Promise; +type Callback = (data: OnyxUpdate[]) => Promise; type OnEvent = (pushJSON: PushJSON) => void; @@ -19,7 +19,7 @@ function subscribeToMultiEvent(eventType: string, callback: Callback) { multiEventCallbackMapping[eventType] = callback; } -function triggerMultiEventHandler(eventType: string, data: OnyxUpdate[]): Promise { +function triggerMultiEventHandler(eventType: string, data: OnyxUpdate[]): Promise { if (!multiEventCallbackMapping[eventType]) { return Promise.resolve(); } From 1b611f12da56d475442a13d4fda9203ea920d8ac Mon Sep 17 00:00:00 2001 From: Viktoryia Kliushun Date: Tue, 3 Oct 2023 08:40:01 +0200 Subject: [PATCH 3/3] Remove OnEvent type --- src/libs/PusherUtils.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/libs/PusherUtils.ts b/src/libs/PusherUtils.ts index 4de7e8db7caa..5baa4b68d5f8 100644 --- a/src/libs/PusherUtils.ts +++ b/src/libs/PusherUtils.ts @@ -10,8 +10,6 @@ type PushJSON = OnyxUpdateEvent[] | OnyxUpdatesFromServer; type Callback = (data: OnyxUpdate[]) => Promise; -type OnEvent = (pushJSON: PushJSON) => void; - // Keeps track of all the callbacks that need triggered for each event type const multiEventCallbackMapping: Record = {}; @@ -29,7 +27,7 @@ function triggerMultiEventHandler(eventType: string, data: OnyxUpdate[]): Promis /** * Abstraction around subscribing to private user channel events. Handles all logs and errors automatically. */ -function subscribeToPrivateUserChannelEvent(eventName: string, accountID: string, onEvent: OnEvent) { +function subscribeToPrivateUserChannelEvent(eventName: string, accountID: string, onEvent: (pushJSON: PushJSON) => void) { const pusherChannelName = `${CONST.PUSHER.PRIVATE_USER_CHANNEL_PREFIX}${accountID}${CONFIG.PUSHER.SUFFIX}`; function logPusherEvent(pushJSON: PushJSON) {