From 3c3a50a193ac7581f9e508dade07e342d3f98fa6 Mon Sep 17 00:00:00 2001 From: trevor-signal <131492920+trevor-signal@users.noreply.github.com> Date: Thu, 28 Mar 2024 13:12:34 -0400 Subject: [PATCH] Rely on socket connection events for message request handling registration --- ts/background.ts | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/ts/background.ts b/ts/background.ts index 1b988d1693d3..9eec9aeac0b0 100644 --- a/ts/background.ts +++ b/ts/background.ts @@ -1554,7 +1554,18 @@ export async function startApp(): Promise { window.Whisper.events.on('online', () => { log.info('background: online'); - if (!remotelyExpired) { + + strictAssert(server !== undefined, 'WebAPI not initialized'); + strictAssert( + messageReceiver !== undefined, + 'MessageReceiver not initialized' + ); + messageReceiver.reset(); + server.registerRequestHandler(messageReceiver); + + // The first call to connect should be done via start(), ensuring that the app is + // ready first + if (!remotelyExpired && connectCount > 0) { drop(connect()); } }); @@ -1566,6 +1577,9 @@ export async function startApp(): Promise { drop(AttachmentDownloads.stop()); drop(messageReceiver?.drain()); + if (messageReceiver) { + server?.unregisterRequestHandler(messageReceiver); + } if (connectCount === 0) { log.info('background: offline, never connected, showing inbox'); @@ -1661,14 +1675,6 @@ export async function startApp(): Promise { void window.Signal.Services.initializeGroupCredentialFetcher(); - strictAssert(server !== undefined, 'WebAPI not initialized'); - strictAssert( - messageReceiver !== undefined, - 'MessageReceiver not initialized' - ); - messageReceiver.reset(); - server.registerRequestHandler(messageReceiver); - drop( AttachmentDownloads.start({ logger: log,