Skip to content

Commit

Permalink
Merge pull request Expensify#9203 from Expensify/amechler-chunked-pusher
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Mechler authored Jun 1, 2022
2 parents e05524c + 8f7694f commit 14176c5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 21 deletions.
13 changes: 4 additions & 9 deletions src/libs/Pusher/pusher.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,10 @@ function getChannel(channelName) {
* @param {Pusher.Channel} channel
* @param {String} eventName
* @param {Function} [eventCallback]
* @param {Boolean} [isChunked] Do we expect this channel to send chunked/separate blocks of data that need recombining?
*
* @private
*/
function bindEventToChannel(channel, eventName, eventCallback = () => {}, isChunked = false) {
function bindEventToChannel(channel, eventName, eventCallback = () => {}) {
if (!eventName) {
return;
}
Expand All @@ -120,7 +119,7 @@ function bindEventToChannel(channel, eventName, eventCallback = () => {}, isChun
Log.alert('[Pusher] Unable to parse JSON response from Pusher', {error: err, eventData});
return;
}
if (!isChunked) {
if (data.id === undefined || data.chunk === undefined || data.final === undefined) {
eventCallback(data);
return;
}
Expand Down Expand Up @@ -169,9 +168,6 @@ function bindEventToChannel(channel, eventName, eventCallback = () => {}, isChun
* @param {String} channelName
* @param {String} eventName
* @param {Function} [eventCallback]
* @param {Boolean} [isChunked] This parameters tells us whether or not we expect the result to come in individual
* pieces/chunks (because it exceeds
* the 10kB limit that pusher has).
* @param {Function} [onResubscribe] Callback to be called when reconnection happen
*
* @return {Promise}
Expand All @@ -182,7 +178,6 @@ function subscribe(
channelName,
eventName,
eventCallback = () => {},
isChunked = false,
onResubscribe = () => {},
) {
return new Promise((resolve, reject) => {
Expand All @@ -201,7 +196,7 @@ function subscribe(
channel.bind('pusher:subscription_succeeded', () => {
// Check so that we do not bind another event with each reconnect attempt
if (!isBound) {
bindEventToChannel(channel, eventName, eventCallback, isChunked);
bindEventToChannel(channel, eventName, eventCallback);
resolve();
isBound = true;
return;
Expand All @@ -225,7 +220,7 @@ function subscribe(
reject(error);
});
} else {
bindEventToChannel(channel, eventName, eventCallback, isChunked);
bindEventToChannel(channel, eventName, eventCallback);
resolve();
}
});
Expand Down
13 changes: 4 additions & 9 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -728,9 +728,8 @@ function getReportChannelName(reportID) {
*
* @param {String} eventName
* @param {Function} onEvent
* @param {Boolean} isChunked
*/
function subscribeToPrivateUserChannelEvent(eventName, onEvent, isChunked = false) {
function subscribeToPrivateUserChannelEvent(eventName, onEvent) {
const pusherChannelName = `private-encrypted-user-accountID-${currentUserAccountID}${CONFIG.PUSHER.SUFFIX}`;

/**
Expand Down Expand Up @@ -759,7 +758,7 @@ function subscribeToPrivateUserChannelEvent(eventName, onEvent, isChunked = fals
Log.hmmm('[Report] Failed to subscribe to Pusher channel', false, {error, pusherChannelName, eventName});
}

Pusher.subscribe(pusherChannelName, eventName, onEventPush, isChunked, onPusherResubscribeToPrivateUserChannel)
Pusher.subscribe(pusherChannelName, eventName, onEventPush, onPusherResubscribeToPrivateUserChannel)
.catch(onSubscriptionFailed);
}

Expand All @@ -781,17 +780,13 @@ function subscribeToUserEvents() {
subscribeToPrivateUserChannelEvent(Pusher.TYPE.REPORT_COMMENT, pushJSON => updateReportWithNewAction(pushJSON.reportID, pushJSON.reportAction, pushJSON.notificationPreference));

// Live-update a report's actions when a 'chunked report comment' event is received.
subscribeToPrivateUserChannelEvent(
Pusher.TYPE.REPORT_COMMENT_CHUNK,
pushJSON => updateReportWithNewAction(pushJSON.reportID, pushJSON.reportAction, pushJSON.notificationPreference),
true,
);
subscribeToPrivateUserChannelEvent(Pusher.TYPE.REPORT_COMMENT_CHUNK, pushJSON => updateReportWithNewAction(pushJSON.reportID, pushJSON.reportAction, pushJSON.notificationPreference));

// Live-update a report's actions when an 'edit comment' event is received.
subscribeToPrivateUserChannelEvent(Pusher.TYPE.REPORT_COMMENT_EDIT, pushJSON => updateReportActionMessage(pushJSON.reportID, pushJSON.sequenceNumber, pushJSON.message));

// Live-update a report's actions when an 'edit comment chunk' event is received.
subscribeToPrivateUserChannelEvent(Pusher.TYPE.REPORT_COMMENT_EDIT_CHUNK, pushJSON => updateReportActionMessage(pushJSON.reportID, pushJSON.sequenceNumber, pushJSON.message), true);
subscribeToPrivateUserChannelEvent(Pusher.TYPE.REPORT_COMMENT_EDIT_CHUNK, pushJSON => updateReportActionMessage(pushJSON.reportID, pushJSON.sequenceNumber, pushJSON.message));

// Live-update a report's pinned state when a 'report toggle pinned' event is received.
subscribeToPrivateUserChannelEvent(Pusher.TYPE.REPORT_TOGGLE_PINNED, pushJSON => updateReportPinnedState(pushJSON.reportID, pushJSON.isPinned));
Expand Down
6 changes: 3 additions & 3 deletions src/libs/actions/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ function subscribeToUserEvents() {
// Live-update an user's preferred locale
Pusher.subscribe(pusherChannelName, Pusher.TYPE.PREFERRED_LOCALE, (pushJSON) => {
Onyx.merge(ONYXKEYS.NVP_PREFERRED_LOCALE, pushJSON.preferredLocale);
}, false,
},
() => {
NetworkConnection.triggerReconnectionCallbacks('pusher re-subscribed to private user channel');
})
Expand All @@ -320,7 +320,7 @@ function subscribeToUserEvents() {
// Subscribe to screen share requests sent by GuidesPlus agents
Pusher.subscribe(pusherChannelName, Pusher.TYPE.SCREEN_SHARE_REQUEST, (pushJSON) => {
Onyx.merge(ONYXKEYS.SCREEN_SHARE_REQUEST, pushJSON);
}, false,
},
() => {
NetworkConnection.triggerReconnectionCallbacks('pusher re-subscribed to private user channel');
})
Expand Down Expand Up @@ -351,7 +351,7 @@ function subscribeToExpensifyCardUpdates() {
} else {
Onyx.merge(ONYXKEYS.USER, {isCheckingDomain: pushJSON.isCheckingDomain});
}
}, false,
},
() => {
NetworkConnection.triggerReconnectionCallbacks('pusher re-subscribed to private user channel');
})
Expand Down

0 comments on commit 14176c5

Please sign in to comment.