Skip to content

Commit

Permalink
Merge pull request #27918 from VickyStash/ts-migration/pusherUtils-lib
Browse files Browse the repository at this point in the history
[No QA] [TS migration] Migrate 'PusherUtils.js' lib to TypeScript
  • Loading branch information
Hayata Suenaga authored Oct 6, 2023
2 parents 3470a65 + 1b611f1 commit ed99aa7
Showing 1 changed file with 14 additions and 30 deletions.
44 changes: 14 additions & 30 deletions src/libs/PusherUtils.js → src/libs/PusherUtils.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
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<void>;

// Keeps track of all the callbacks that need triggered for each event type
const multiEventCallbackMapping = {};
const multiEventCallbackMapping: Record<string, Callback> = {};

/**
* @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<void> {
if (!multiEventCallbackMapping[eventType]) {
return Promise.resolve();
}
Expand All @@ -29,38 +26,25 @@ 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: (pushJSON: PushJSON) => void) {
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);
}

function onPusherResubscribeToPrivateUserChannel() {
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);
}
Expand Down

0 comments on commit ed99aa7

Please sign in to comment.