Skip to content

Commit

Permalink
Ensure queues are restarted once the empty event is received
Browse files Browse the repository at this point in the history
  • Loading branch information
trevor-signal committed Apr 30, 2024
1 parent 2506d6e commit d1f296a
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions ts/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1620,6 +1620,35 @@ export async function startApp(): Promise<void> {
window.addEventListener('online', onNavigatorOnline);
window.addEventListener('offline', onNavigatorOffline);

window.Whisper.events.on('socketStatusChange', () => {
if (window.getSocketStatus() === SocketStatus.OPEN) {
pauseQueuesAndNotificationsOnSocketConnect();
}
});

// 1. When the socket is connected, to avoid a flood of operations before we catch up,
// we pause some queues.
function pauseQueuesAndNotificationsOnSocketConnect() {
log.info('pauseQueuesAndNotificationsOnSocketConnect: pausing');
profileKeyResponseQueue.pause();
lightSessionResetQueue.pause();
onDecryptionErrorQueue.pause();
onRetryRequestQueue.pause();
window.Whisper.deliveryReceiptQueue.pause();
notificationService.disable();
}

// 2. After the socket finishes processing any queued messages, restart these queues
function restartQueuesAndNotificationsOnEmpty() {
log.info('restartQueuesAndNotificationsOnEmpty: restarting');
profileKeyResponseQueue.start();
lightSessionResetQueue.start();
onDecryptionErrorQueue.start();
onRetryRequestQueue.start();
window.Whisper.deliveryReceiptQueue.start();
notificationService.enable();
}

function isSocketOnline() {
const socketStatus = window.getSocketStatus();
return (
Expand Down Expand Up @@ -1704,14 +1733,6 @@ export async function startApp(): Promise<void> {

connectCount += 1;

// To avoid a flood of operations before we catch up, we pause some queues.
profileKeyResponseQueue.pause();
lightSessionResetQueue.pause();
onDecryptionErrorQueue.pause();
onRetryRequestQueue.pause();
window.Whisper.deliveryReceiptQueue.pause();
notificationService.disable();

void window.Signal.Services.initializeGroupCredentialFetcher();

drop(AttachmentDownloadManager.start());
Expand Down Expand Up @@ -1983,12 +2004,7 @@ export async function startApp(): Promise<void> {
// Start listeners here, after we get through our queue.
UpdateKeysListener.init(window.Whisper.events, newVersion);

profileKeyResponseQueue.start();
lightSessionResetQueue.start();
onDecryptionErrorQueue.start();
onRetryRequestQueue.start();
window.Whisper.deliveryReceiptQueue.start();
notificationService.enable();
restartQueuesAndNotificationsOnEmpty();

await onAppView;

Expand Down

0 comments on commit d1f296a

Please sign in to comment.