background/connect: Prevent concurrent calls to connect
This commit is contained in:
parent
1d1fccd96f
commit
cd599f92c8
1 changed files with 322 additions and 307 deletions
|
@ -1716,7 +1716,15 @@ type WhatIsThis = typeof window.WhatIsThis;
|
|||
}
|
||||
|
||||
let connectCount = 0;
|
||||
let connecting = false;
|
||||
async function connect(firstRun?: boolean) {
|
||||
if (connecting) {
|
||||
window.log.warn('connect already running', { connectCount });
|
||||
return;
|
||||
}
|
||||
try {
|
||||
connecting = true;
|
||||
|
||||
window.log.info('connect', { firstRun, connectCount });
|
||||
|
||||
if (reconnectTimer) {
|
||||
|
@ -1764,8 +1772,8 @@ type WhatIsThis = typeof window.WhatIsThis;
|
|||
|
||||
if (connectCount === 0) {
|
||||
try {
|
||||
// Force a re-fetch before we process our queue. We may want to turn on something
|
||||
// which changes how we process incoming messages!
|
||||
// Force a re-fetch before we process our queue. We may want to turn on
|
||||
// something which changes how we process incoming messages!
|
||||
await window.Signal.RemoteConfig.refreshRemoteConfig();
|
||||
} catch (error) {
|
||||
window.log.error(
|
||||
|
@ -1840,14 +1848,15 @@ type WhatIsThis = typeof window.WhatIsThis;
|
|||
|
||||
preMessageReceiverStatus = null;
|
||||
|
||||
// eslint-disable-next-line no-inner-declarations
|
||||
function addQueuedEventListener(name: WhatIsThis, handler: WhatIsThis) {
|
||||
messageReceiver.addEventListener(name, (...args: Array<WhatIsThis>) =>
|
||||
eventHandlerQueue.add(async () => {
|
||||
try {
|
||||
await handler(...args);
|
||||
} finally {
|
||||
// message/sent: Message.handleDataMessage has its own queue and will trigger
|
||||
// this event itself when complete.
|
||||
// message/sent: Message.handleDataMessage has its own queue and will
|
||||
// trigger this event itself when complete.
|
||||
// error: Error processing (below) also has its own queue and self-trigger.
|
||||
if (name !== 'message' && name !== 'sent' && name !== 'error') {
|
||||
window.Whisper.events.trigger('incrementProgress');
|
||||
|
@ -1872,7 +1881,10 @@ type WhatIsThis = typeof window.WhatIsThis;
|
|||
addQueuedEventListener('typing', onTyping);
|
||||
addQueuedEventListener('sticker-pack', onStickerPack);
|
||||
addQueuedEventListener('viewSync', onViewSync);
|
||||
addQueuedEventListener('messageRequestResponse', onMessageRequestResponse);
|
||||
addQueuedEventListener(
|
||||
'messageRequestResponse',
|
||||
onMessageRequestResponse
|
||||
);
|
||||
addQueuedEventListener('profileKeyUpdate', onProfileKeyUpdate);
|
||||
addQueuedEventListener('fetchLatest', onFetchLatestSync);
|
||||
addQueuedEventListener('keys', onKeysSync);
|
||||
|
@ -2043,6 +2055,9 @@ type WhatIsThis = typeof window.WhatIsThis;
|
|||
window.storage.onready(async () => {
|
||||
idleDetector.start();
|
||||
});
|
||||
} finally {
|
||||
connecting = false;
|
||||
}
|
||||
}
|
||||
|
||||
function onChangeTheme() {
|
||||
|
|
Loading…
Reference in a new issue