Simplify messageReceiver initialization & reset

Co-authored-by: Scott Nonnenberg <scott@signal.org>
This commit is contained in:
trevor-signal 2024-04-02 16:43:20 -04:00 committed by GitHub
parent f057bc560f
commit dfd564e67f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 318 additions and 289 deletions

View file

@ -305,6 +305,8 @@ export default class MessageReceiver
private pniIdentityKeyCheckRequired?: boolean;
private isAppReadyForProcessing: boolean = false;
constructor({ server, storage, serverTrustRoot }: MessageReceiverOptions) {
super();
@ -352,6 +354,15 @@ export default class MessageReceiver
maxSize: 30,
processBatch: this.cacheRemoveBatch.bind(this),
});
window.Whisper.events.on('app-ready-for-processing', () => {
this.isAppReadyForProcessing = true;
this.reset();
});
window.Whisper.events.on('online', () => {
this.reset();
});
}
public getAndResetProcessedCount(): number {
@ -467,27 +478,36 @@ export default class MessageReceiver
}
public reset(): void {
// We always process our cache before processing a new websocket message
drop(
this.incomingQueue.add(
createTaskWithTimeout(
async () => this.queueAllCached(),
'incomingQueue/queueAllCached',
{
timeout: 10 * durations.MINUTE,
}
)
)
);
log.info('MessageReceiver.reset');
this.count = 0;
this.isEmptied = false;
this.stoppingProcessing = false;
if (!this.isAppReadyForProcessing) {
log.info('MessageReceiver.reset: not ready yet, returning early');
return;
}
drop(this.addCachedMessagesToQueue());
}
private addCachedMessagesToQueue(): Promise<void> {
log.info('MessageReceiver.addCachedMessagesToQueue');
return this.incomingQueue.add(
createTaskWithTimeout(
async () => this.queueAllCached(),
'incomingQueue/queueAllCached',
{
timeout: 10 * durations.MINUTE,
}
)
);
}
public stopProcessing(): void {
log.info('MessageReceiver.stopProcessing');
this.stoppingProcessing = true;
this.isAppReadyForProcessing = false;
}
public hasEmptied(): boolean {