2021-03-04 21:44:57 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { MessageAttributesType } from '../model-types.d';
|
|
|
|
import { createBatcher } from './batcher';
|
|
|
|
import { createWaitBatcher } from './waitBatcher';
|
|
|
|
|
2021-03-11 03:21:21 +00:00
|
|
|
const updateMessageBatcher = createBatcher<MessageAttributesType>({
|
2021-03-26 00:00:03 +00:00
|
|
|
name: 'messageBatcher.updateMessageBatcher',
|
2021-03-30 16:56:03 +00:00
|
|
|
wait: 75,
|
2021-03-04 21:44:57 +00:00
|
|
|
maxSize: 50,
|
2021-03-05 17:57:09 +00:00
|
|
|
processBatch: async (messageAttrs: Array<MessageAttributesType>) => {
|
|
|
|
window.log.info('updateMessageBatcher', messageAttrs.length);
|
|
|
|
await window.Signal.Data.saveMessages(messageAttrs, {});
|
2021-03-04 21:44:57 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-03-11 03:21:21 +00:00
|
|
|
let shouldBatch = true;
|
|
|
|
|
|
|
|
export function queueUpdateMessage(messageAttr: MessageAttributesType): void {
|
|
|
|
if (shouldBatch) {
|
|
|
|
updateMessageBatcher.add(messageAttr);
|
|
|
|
} else {
|
|
|
|
window.Signal.Data.saveMessage(messageAttr, {
|
|
|
|
Message: window.Whisper.Message,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setBatchingStrategy(keepBatching = false): void {
|
|
|
|
shouldBatch = keepBatching;
|
|
|
|
}
|
|
|
|
|
2021-03-04 21:44:57 +00:00
|
|
|
export const saveNewMessageBatcher = createWaitBatcher<MessageAttributesType>({
|
2021-03-26 00:00:03 +00:00
|
|
|
name: 'messageBatcher.saveNewMessageBatcher',
|
2021-03-30 16:56:03 +00:00
|
|
|
wait: 75,
|
2021-03-04 21:44:57 +00:00
|
|
|
maxSize: 30,
|
2021-03-05 17:57:09 +00:00
|
|
|
processBatch: async (messageAttrs: Array<MessageAttributesType>) => {
|
|
|
|
window.log.info('saveNewMessageBatcher', messageAttrs.length);
|
|
|
|
await window.Signal.Data.saveMessages(messageAttrs, { forceSave: true });
|
2021-03-04 21:44:57 +00:00
|
|
|
},
|
|
|
|
});
|