From 215ae1a062d04da93344e74823f9c883cfbc87e0 Mon Sep 17 00:00:00 2001 From: Bartosz Grajdek Date: Sun, 17 Sep 2023 19:52:05 +0200 Subject: [PATCH 1/3] [TS migration] Migrate 'PusherConnectionManager.js' lib to TypeScript --- ...nnectionManager.js => PusherConnectionManager.ts} | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) rename src/libs/{PusherConnectionManager.js => PusherConnectionManager.ts} (86%) diff --git a/src/libs/PusherConnectionManager.js b/src/libs/PusherConnectionManager.ts similarity index 86% rename from src/libs/PusherConnectionManager.js rename to src/libs/PusherConnectionManager.ts index a391a4973fd4..557c0e024e9f 100644 --- a/src/libs/PusherConnectionManager.js +++ b/src/libs/PusherConnectionManager.ts @@ -1,4 +1,4 @@ -import lodashGet from 'lodash/get'; +import {ValueOf} from 'type-fest'; import * as Pusher from './Pusher/pusher'; import * as Session from './actions/Session'; import Log from './Log'; @@ -11,8 +11,8 @@ function init() { * current valid token to generate the signed auth response * needed to subscribe to Pusher channels. */ - Pusher.registerCustomAuthorizer((channel) => ({ - authorize: (socketID, callback) => { + Pusher.registerCustomAuthorizer((channel: {name: string}) => ({ + authorize: (socketID: string, callback: () => void) => { Session.authenticatePusher(socketID, channel.name, callback); }, })); @@ -20,11 +20,11 @@ function init() { /** * @params {string} eventName */ - Pusher.registerSocketEventCallback((eventName, error) => { + Pusher.registerSocketEventCallback((eventName: string, error: {type: ValueOf; data: {code: number}}) => { switch (eventName) { case 'error': { - const errorType = lodashGet(error, 'type'); - const code = lodashGet(error, 'data.code'); + const errorType = error?.type; + const code = error?.data?.code; if (errorType === CONST.ERROR.PUSHER_ERROR && code === 1006) { // 1006 code happens when a websocket connection is closed. There may or may not be a reason attached indicating why the connection was closed. // https://datatracker.ietf.org/doc/html/rfc6455#section-7.1.5 From 429e119a3929616dd99565359c27ee76048c5075 Mon Sep 17 00:00:00 2001 From: Bartosz Grajdek Date: Tue, 19 Sep 2023 14:11:18 +0200 Subject: [PATCH 2/3] [TS migration] Migrate 'PusherConnectionManager.js' lib to TypeScript --- src/libs/PusherConnectionManager.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/libs/PusherConnectionManager.ts b/src/libs/PusherConnectionManager.ts index 557c0e024e9f..f5dfc623fc14 100644 --- a/src/libs/PusherConnectionManager.ts +++ b/src/libs/PusherConnectionManager.ts @@ -4,6 +4,8 @@ import * as Session from './actions/Session'; import Log from './Log'; import CONST from '../CONST'; +type EventCallbackError = {type: ValueOf; data: {code: number}}; + function init() { /** * When authTokens expire they will automatically be refreshed. @@ -17,10 +19,7 @@ function init() { }, })); - /** - * @params {string} eventName - */ - Pusher.registerSocketEventCallback((eventName: string, error: {type: ValueOf; data: {code: number}}) => { + Pusher.registerSocketEventCallback((eventName: string, error: EventCallbackError) => { switch (eventName) { case 'error': { const errorType = error?.type; From 7ebcd09ecedf35b2e1f3bc2a9a6365dfacb0f6c1 Mon Sep 17 00:00:00 2001 From: Bartosz Grajdek Date: Wed, 20 Sep 2023 23:38:22 +0200 Subject: [PATCH 3/3] Move custom authorizer channel into separate type --- src/libs/PusherConnectionManager.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libs/PusherConnectionManager.ts b/src/libs/PusherConnectionManager.ts index f5dfc623fc14..4ab08d6dc760 100644 --- a/src/libs/PusherConnectionManager.ts +++ b/src/libs/PusherConnectionManager.ts @@ -5,6 +5,7 @@ import Log from './Log'; import CONST from '../CONST'; type EventCallbackError = {type: ValueOf; data: {code: number}}; +type CustomAuthorizerChannel = {name: string}; function init() { /** @@ -13,7 +14,7 @@ function init() { * current valid token to generate the signed auth response * needed to subscribe to Pusher channels. */ - Pusher.registerCustomAuthorizer((channel: {name: string}) => ({ + Pusher.registerCustomAuthorizer((channel: CustomAuthorizerChannel) => ({ authorize: (socketID: string, callback: () => void) => { Session.authenticatePusher(socketID, channel.name, callback); },