signal-desktop/ts/util/messageBatcher.ts

67 lines
2 KiB
TypeScript
Raw Normal View History

2021-03-04 21:44:57 +00:00
// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { ReadonlyMessageAttributesType } from '../model-types.d';
2021-03-04 21:44:57 +00:00
import { createBatcher } from './batcher';
import { createWaitBatcher } from './waitBatcher';
2024-07-22 18:16:33 +00:00
import { DataWriter } from '../sql/Client';
import * as log from '../logging/log';
2021-03-04 21:44:57 +00:00
const updateMessageBatcher = createBatcher<ReadonlyMessageAttributesType>({
2021-03-26 00:00:03 +00:00
name: 'messageBatcher.updateMessageBatcher',
wait: 75,
2021-03-04 21:44:57 +00:00
maxSize: 50,
processBatch: async (messageAttrs: Array<ReadonlyMessageAttributesType>) => {
log.info('updateMessageBatcher', messageAttrs.length);
// Grab the latest from the cache in case they've changed
const messagesToSave = messageAttrs.map(
message => window.MessageCache.accessAttributes(message.id) ?? message
);
2024-07-22 18:16:33 +00:00
await DataWriter.saveMessages(messagesToSave, {
ourAci: window.textsecure.storage.user.getCheckedAci(),
2021-12-20 21:04:02 +00:00
});
2021-03-04 21:44:57 +00:00
},
});
let shouldBatch = true;
export function queueUpdateMessage(
messageAttr: ReadonlyMessageAttributesType
): void {
if (shouldBatch) {
updateMessageBatcher.add(messageAttr);
} else {
2024-07-22 18:16:33 +00:00
void DataWriter.saveMessage(messageAttr, {
ourAci: window.textsecure.storage.user.getCheckedAci(),
2021-12-20 21:04:02 +00:00
});
}
}
export function setBatchingStrategy(keepBatching = false): void {
shouldBatch = keepBatching;
}
export const saveNewMessageBatcher =
createWaitBatcher<ReadonlyMessageAttributesType>({
name: 'messageBatcher.saveNewMessageBatcher',
wait: 75,
maxSize: 30,
processBatch: async (
messageAttrs: Array<ReadonlyMessageAttributesType>
) => {
log.info('saveNewMessageBatcher', messageAttrs.length);
// Grab the latest from the cache in case they've changed
const messagesToSave = messageAttrs.map(
message => window.MessageCache.accessAttributes(message.id) ?? message
);
await DataWriter.saveMessages(messagesToSave, {
forceSave: true,
ourAci: window.textsecure.storage.user.getCheckedAci(),
});
},
});