Skip to content

Commit dbd0421

Browse files
committed
Update Pusher module export since now it's platform-specific
1 parent 863a59a commit dbd0421

File tree

12 files changed

+52
-50
lines changed

12 files changed

+52
-50
lines changed

src/libs/API/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Log from '@libs/Log';
55
import {HandleUnusedOptimisticID, Logging, Pagination, Reauthentication, RecheckConnection, SaveResponseInOnyx} from '@libs/Middleware';
66
import {isOffline} from '@libs/Network/NetworkStore';
77
import {push as pushToSequentialQueue, waitForIdle as waitForSequentialQueueIdle} from '@libs/Network/SequentialQueue';
8-
import {getPusherSocketID} from '@libs/Pusher';
8+
import Pusher from '@libs/Pusher';
99
import {processWithMiddleware, use} from '@libs/Request';
1010
import {getLength as getPersistedRequestsLength} from '@userActions/PersistedRequests';
1111
import CONST from '@src/CONST';
@@ -70,7 +70,7 @@ function prepareRequest<TCommand extends ApiCommand>(
7070

7171
// We send the pusherSocketID with all write requests so that the api can include it in push events to prevent Pusher from sending the events to the requesting client. The push event
7272
// is sent back to the requesting client in the response data instead, which prevents a replay effect in the UI. See https://github.com/Expensify/App/issues/12775.
73-
pusherSocketID: isWriteRequest ? getPusherSocketID() : undefined,
73+
pusherSocketID: isWriteRequest ? Pusher.getPusherSocketID() : undefined,
7474
};
7575

7676
// Assemble all request metadata (used by middlewares, and for persisted requests stored in Onyx)

src/libs/Navigation/AppNavigator/AuthScreens.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import type {AuthScreensParamList, CentralPaneName, CentralPaneScreensParamList}
3434
import {isOnboardingFlowName} from '@libs/NavigationUtils';
3535
import NetworkConnection from '@libs/NetworkConnection';
3636
import onyxSubscribe from '@libs/onyxSubscribe';
37-
import * as Pusher from '@libs/Pusher';
37+
import Pusher from '@libs/Pusher';
3838
import PusherConnectionManager from '@libs/PusherConnectionManager';
3939
import * as ReportUtils from '@libs/ReportUtils';
4040
import * as SearchQueryUtils from '@libs/SearchQueryUtils';

src/libs/Pusher/index.native.ts

+18-16
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,8 @@ import {authenticatePusher} from '@userActions/Session';
99
import CONST from '@src/CONST';
1010
import ONYXKEYS from '@src/ONYXKEYS';
1111
import TYPE from './EventType';
12-
import type {
13-
Args,
14-
ChunkedDataEvents,
15-
EventCallbackError,
16-
EventData,
17-
PingPongEvent,
18-
PusherEventName,
19-
SocketEventCallback,
20-
SocketEventName,
21-
States,
22-
UserIsLeavingRoomEvent,
23-
UserIsTypingEvent,
24-
} from './types';
12+
import type {Args, ChunkedDataEvents, EventCallbackError, EventData, PusherEventName, SocketEventCallback, SocketEventName, States} from './types';
13+
import type PusherModule from './types';
2514

2615
let shouldForceOffline = false;
2716
Onyx.connect({
@@ -349,6 +338,19 @@ if (window) {
349338
window.getPusherInstance = () => socket;
350339
}
351340

352-
export {init, subscribe, unsubscribe, getChannel, isSubscribed, isAlreadySubscribing, sendEvent, disconnect, reconnect, registerSocketEventCallback, TYPE, getPusherSocketID};
353-
354-
export type {EventCallbackError, States, UserIsTypingEvent, UserIsLeavingRoomEvent, PingPongEvent};
341+
const MobilePusher: PusherModule = {
342+
init,
343+
subscribe,
344+
unsubscribe,
345+
getChannel,
346+
isSubscribed,
347+
isAlreadySubscribing,
348+
sendEvent,
349+
disconnect,
350+
reconnect,
351+
registerSocketEventCallback,
352+
TYPE,
353+
getPusherSocketID,
354+
};
355+
356+
export default MobilePusher;

src/libs/Pusher/index.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,14 @@ import type {
1111
ChunkedDataEvents,
1212
EventCallbackError,
1313
EventData,
14-
PingPongEvent,
1514
PusherEventName,
1615
PusherSubscribtionErrorData,
1716
PusherWithAuthParams,
1817
SocketEventCallback,
1918
SocketEventName,
2019
States,
21-
UserIsLeavingRoomEvent,
22-
UserIsTypingEvent,
2320
} from './types';
21+
import type PusherModule from './types';
2422

2523
let shouldForceOffline = false;
2624
Onyx.connect({
@@ -380,7 +378,7 @@ if (window) {
380378
window.getPusherInstance = () => socket;
381379
}
382380

383-
export {
381+
const WebPusher: PusherModule = {
384382
init,
385383
subscribe,
386384
unsubscribe,
@@ -396,4 +394,4 @@ export {
396394
getPusherSocketID,
397395
};
398396

399-
export type {EventCallbackError, States, UserIsTypingEvent, UserIsLeavingRoomEvent, PingPongEvent};
397+
export default WebPusher;

src/libs/Pusher/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ type PusherModule = {
7979
registerSocketEventCallback: (cb: SocketEventCallback) => void;
8080
registerCustomAuthorizer?: (authorizer: ChannelAuthorizerGenerator) => void;
8181
getPusherSocketID: () => string | undefined;
82+
TYPE: typeof TYPE;
8283
};
8384

8485
export default PusherModule;

src/libs/PusherConnectionManager.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import type {ChannelAuthorizationCallback} from 'pusher-js/with-encryption';
22
import CONST from '@src/CONST';
33
import {authenticatePusher} from './actions/Session';
44
import Log from './Log';
5-
import {reconnect, registerCustomAuthorizer, registerSocketEventCallback} from './Pusher';
6-
import type {EventCallbackError, States} from './Pusher';
7-
import type {SocketEventName} from './Pusher/types';
5+
import Pusher from './Pusher';
6+
import type {EventCallbackError, SocketEventName, States} from './Pusher/types';
87

98
function init() {
109
/**
@@ -13,13 +12,13 @@ function init() {
1312
* current valid token to generate the signed auth response
1413
* needed to subscribe to Pusher channels.
1514
*/
16-
registerCustomAuthorizer?.((channel) => ({
15+
Pusher.registerCustomAuthorizer?.((channel) => ({
1716
authorize: (socketId: string, callback: ChannelAuthorizationCallback) => {
1817
authenticatePusher(socketId, channel.name, callback);
1918
},
2019
}));
2120

22-
registerSocketEventCallback((eventName: SocketEventName, error?: EventCallbackError | States) => {
21+
Pusher.registerSocketEventCallback((eventName: SocketEventName, error?: EventCallbackError | States) => {
2322
switch (eventName) {
2423
case 'error': {
2524
if (error && 'type' in error) {
@@ -35,7 +34,7 @@ function init() {
3534
// On the advice from Pusher directly, they suggested to manually reconnect in those scenarios.
3635
if (errorMessage) {
3736
Log.hmmm('[PusherConnectionManager] Channels Error 1006 message', {errorMessage});
38-
reconnect();
37+
Pusher.reconnect();
3938
}
4039
} else if (errorType === CONST.ERROR.PUSHER_ERROR && code === 4201) {
4140
// This means the connection was closed because Pusher did not receive a reply from the client when it pinged them for a response

src/libs/PusherUtils.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import CONST from '@src/CONST';
44
import type {OnyxUpdatesFromServer} from '@src/types/onyx';
55
import Log from './Log';
66
import NetworkConnection from './NetworkConnection';
7-
import {subscribe} from './Pusher';
8-
import type {PingPongEvent} from './Pusher';
7+
import Pusher from './Pusher';
8+
import type {PingPongEvent} from './Pusher/types';
99

1010
type Callback = (data: OnyxUpdate[]) => Promise<void>;
1111

@@ -50,7 +50,7 @@ function subscribeToPrivateUserChannelEvent(eventName: string, accountID: string
5050
function onSubscriptionFailed(error: Error) {
5151
Log.hmmm('Failed to subscribe to Pusher channel', {error, pusherChannelName, eventName});
5252
}
53-
subscribe(pusherChannelName, eventName, onEventPush, onPusherResubscribeToPrivateUserChannel).catch(onSubscriptionFailed);
53+
Pusher.subscribe(pusherChannelName, eventName, onEventPush, onPusherResubscribeToPrivateUserChannel).catch(onSubscriptionFailed);
5454
}
5555

5656
export default {

src/libs/actions/Report.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ import * as PhoneNumber from '@libs/PhoneNumber';
7979
import getPolicyEmployeeAccountIDs from '@libs/PolicyEmployeeListUtils';
8080
import {extractPolicyIDFromPath, getPolicy} from '@libs/PolicyUtils';
8181
import processReportIDDeeplink from '@libs/processReportIDDeeplink';
82-
import * as Pusher from '@libs/Pusher';
82+
import Pusher from '@libs/Pusher';
83+
import type {UserIsLeavingRoomEvent, UserIsTypingEvent} from '@libs/Pusher/types';
8384
import * as ReportActionsUtils from '@libs/ReportActionsUtils';
8485
import type {OptimisticAddCommentReportAction, OptimisticChatReport} from '@libs/ReportUtils';
8586
import {
@@ -398,7 +399,7 @@ function getReportChannelName(reportID: string): string {
398399
*
399400
* This method makes sure that no matter which we get, we return the "new" format
400401
*/
401-
function getNormalizedStatus(typingStatus: Pusher.UserIsTypingEvent | Pusher.UserIsLeavingRoomEvent): ReportUserIsTyping {
402+
function getNormalizedStatus(typingStatus: UserIsTypingEvent | UserIsLeavingRoomEvent): ReportUserIsTyping {
402403
let normalizedStatus: ReportUserIsTyping;
403404

404405
if (typingStatus.userLogin) {
@@ -463,7 +464,7 @@ function subscribeToReportLeavingEvents(reportID: string | undefined) {
463464
Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_USER_IS_LEAVING_ROOM}${reportID}`, false);
464465

465466
const pusherChannelName = getReportChannelName(reportID);
466-
Pusher.subscribe(pusherChannelName, Pusher.TYPE.USER_IS_LEAVING_ROOM, (leavingStatus: Pusher.UserIsLeavingRoomEvent) => {
467+
Pusher.subscribe(pusherChannelName, Pusher.TYPE.USER_IS_LEAVING_ROOM, (leavingStatus: UserIsLeavingRoomEvent) => {
467468
// If the pusher message comes from OldDot, we expect the leaving status to be keyed by user
468469
// login OR by 'Concierge'. If the pusher message comes from NewDot, it is keyed by accountID
469470
// since personal details are keyed by accountID.
@@ -1499,7 +1500,7 @@ function saveReportDraftComment(reportID: string, comment: string | null, callba
14991500
/** Broadcasts whether or not a user is typing on a report over the report's private pusher channel. */
15001501
function broadcastUserIsTyping(reportID: string) {
15011502
const privateReportChannelName = getReportChannelName(reportID);
1502-
const typingStatus: Pusher.UserIsTypingEvent = {
1503+
const typingStatus: UserIsTypingEvent = {
15031504
[currentUserAccountID]: true,
15041505
};
15051506
Pusher.sendEvent(privateReportChannelName, Pusher.TYPE.USER_IS_TYPING, typingStatus);
@@ -1508,7 +1509,7 @@ function broadcastUserIsTyping(reportID: string) {
15081509
/** Broadcasts to the report's private pusher channel whether a user is leaving a report */
15091510
function broadcastUserIsLeavingRoom(reportID: string) {
15101511
const privateReportChannelName = getReportChannelName(reportID);
1511-
const leavingStatus: Pusher.UserIsLeavingRoomEvent = {
1512+
const leavingStatus: UserIsLeavingRoomEvent = {
15121513
[currentUserAccountID]: true,
15131514
};
15141515
Pusher.sendEvent(privateReportChannelName, Pusher.TYPE.USER_IS_LEAVING_ROOM, leavingStatus);

src/libs/actions/Session/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import * as MainQueue from '@libs/Network/MainQueue';
3636
import * as NetworkStore from '@libs/Network/NetworkStore';
3737
import {getCurrentUserEmail} from '@libs/Network/NetworkStore';
3838
import NetworkConnection from '@libs/NetworkConnection';
39-
import * as Pusher from '@libs/Pusher';
39+
import Pusher from '@libs/Pusher';
4040
import {getReportIDFromLink, parseReportRouteParams as parseReportRouteParamsReportUtils} from '@libs/ReportUtils';
4141
import * as SessionUtils from '@libs/SessionUtils';
4242
import {clearSoundAssetsCache} from '@libs/Sound';

src/libs/actions/User.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ import {isOffline} from '@libs/Network/NetworkStore';
3232
import * as SequentialQueue from '@libs/Network/SequentialQueue';
3333
import * as NumberUtils from '@libs/NumberUtils';
3434
import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils';
35-
import * as Pusher from '@libs/Pusher';
35+
import Pusher from '@libs/Pusher';
36+
import type {PingPongEvent} from '@libs/Pusher/types';
3637
import PusherUtils from '@libs/PusherUtils';
3738
import * as ReportActionsUtils from '@libs/ReportActionsUtils';
3839
import * as ReportUtils from '@libs/ReportUtils';
@@ -905,7 +906,7 @@ function subscribeToPusherPong() {
905906
lastPongReceivedTimestamp = Date.now();
906907

907908
// Calculate the latency between the client and the server
908-
const pongEvent = pushJSON as Pusher.PingPongEvent;
909+
const pongEvent = pushJSON as PingPongEvent;
909910
const latency = Date.now() - Number(pongEvent.pingTimestamp);
910911
Log.info(`[Pusher PINGPONG] The event took ${latency} ms`);
911912

tests/utils/PusherHelper.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {init, sendEvent, TYPE, unsubscribe} from '@libs/Pusher';
1+
import Pusher from '@libs/Pusher';
22
import CONFIG from '@src/CONFIG';
33
import CONST from '@src/CONST';
44
import PusherConnectionManager from '@src/libs/PusherConnectionManager';
@@ -11,12 +11,12 @@ function setup() {
1111
// channel already in a subscribed state. These methods are normally used to prevent
1212
// duplicated subscriptions, but we don't need them for this test so forcing them to
1313
// return false will make the testing less complex.
14-
jest.spyOn(require('@libs/Pusher'), 'isSubscribed').mockReturnValue(false);
15-
jest.spyOn(require('@libs/Pusher'), 'isAlreadySubscribing').mockReturnValue(false);
14+
jest.spyOn(Pusher, 'isSubscribed').mockReturnValue(false);
15+
jest.spyOn(Pusher, 'isAlreadySubscribing').mockReturnValue(false);
1616

1717
// Connect to Pusher
1818
PusherConnectionManager.init();
19-
init({
19+
Pusher.init({
2020
appKey: CONFIG.PUSHER.APP_KEY,
2121
cluster: CONFIG.PUSHER.CLUSTER,
2222
authEndpoint: `${CONFIG.EXPENSIFY.DEFAULT_API_ROOT}api/AuthenticatePusher?`,
@@ -29,13 +29,13 @@ function setup() {
2929
}
3030

3131
function emitOnyxUpdate(args: OnyxServerUpdate[]) {
32-
sendEvent(CHANNEL_NAME, TYPE.MULTIPLE_EVENTS, {
32+
Pusher.sendEvent(CHANNEL_NAME, Pusher.TYPE.MULTIPLE_EVENTS, {
3333
type: 'pusher',
3434
lastUpdateID: 0,
3535
previousUpdateID: 0,
3636
updates: [
3737
{
38-
eventType: TYPE.MULTIPLE_EVENT_TYPE.ONYX_API_UPDATE,
38+
eventType: Pusher.TYPE.MULTIPLE_EVENT_TYPE.ONYX_API_UPDATE,
3939
data: args,
4040
},
4141
],
@@ -45,7 +45,7 @@ function emitOnyxUpdate(args: OnyxServerUpdate[]) {
4545
function teardown() {
4646
// Unsubscribe from account channel after each test since we subscribe in the function
4747
// subscribeToUserEvents and we don't want duplicate event subscriptions.
48-
unsubscribe(CHANNEL_NAME);
48+
Pusher.unsubscribe(CHANNEL_NAME);
4949
}
5050

5151
export default {

tests/utils/TestHelper.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Onyx from 'react-native-onyx';
55
import type {ConnectOptions} from 'react-native-onyx/dist/types';
66
import type {ApiCommand, ApiRequestCommandParameters} from '@libs/API/types';
77
import {translateLocal} from '@libs/Localize';
8-
import {init} from '@libs/Pusher';
8+
import Pusher from '@libs/Pusher';
99
import PusherConnectionManager from '@libs/PusherConnectionManager';
1010
import CONFIG from '@src/CONFIG';
1111
import CONST from '@src/CONST';
@@ -47,7 +47,7 @@ function setupApp() {
4747

4848
// Connect to Pusher
4949
PusherConnectionManager.init();
50-
init({
50+
Pusher.init({
5151
appKey: CONFIG.PUSHER.APP_KEY,
5252
cluster: CONFIG.PUSHER.CLUSTER,
5353
authEndpoint: `${CONFIG.EXPENSIFY.DEFAULT_API_ROOT}api/AuthenticatePusher?`,

0 commit comments

Comments
 (0)