Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[No QA] [TS migration] Migrate 'PusherUtils.js' lib to TypeScript #27918

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
VickyStash marked this conversation as resolved.
Show resolved Hide resolved
"fbjs": "^3.0.2",
"htmlparser2": "^7.2.0",
"idb-keyval": "^6.2.1",
Expand Down
46 changes: 16 additions & 30 deletions src/libs/PusherUtils.js → src/libs/PusherUtils.ts
Original file line number Diff line number Diff line change
@@ -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<void>;

type OnEvent = (pushJSON: PushJSON) => void;
hayata-suenaga marked this conversation as resolved.
Show resolved Hide resolved

// 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 +28,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: 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);
}

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
Loading