signal-desktop/ts/util/messageBatcher.ts

49 lines
1.5 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 { MessageAttributesType } from '../model-types.d';
2021-03-04 21:44:57 +00:00
import { createBatcher } from './batcher';
import { createWaitBatcher } from './waitBatcher';
import * as log from '../logging/log';
2021-03-04 21:44:57 +00:00
const updateMessageBatcher = createBatcher<MessageAttributesType>({
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<MessageAttributesType>) => {
log.info('updateMessageBatcher', messageAttrs.length);
2021-12-20 21:04:02 +00:00
await window.Signal.Data.saveMessages(messageAttrs, {
ourUuid: window.textsecure.storage.user.getCheckedUuid().toString(),
});
2021-03-04 21:44:57 +00:00
},
});
let shouldBatch = true;
export function queueUpdateMessage(messageAttr: MessageAttributesType): void {
if (shouldBatch) {
updateMessageBatcher.add(messageAttr);
} else {
2021-12-20 21:04:02 +00:00
window.Signal.Data.saveMessage(messageAttr, {
ourUuid: window.textsecure.storage.user.getCheckedUuid().toString(),
});
}
}
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',
wait: 75,
2021-03-04 21:44:57 +00:00
maxSize: 30,
processBatch: async (messageAttrs: Array<MessageAttributesType>) => {
log.info('saveNewMessageBatcher', messageAttrs.length);
2021-06-16 22:20:17 +00:00
await window.Signal.Data.saveMessages(messageAttrs, {
forceSave: true,
2021-12-20 21:04:02 +00:00
ourUuid: window.textsecure.storage.user.getCheckedUuid().toString(),
2021-06-16 22:20:17 +00:00
});
2021-03-04 21:44:57 +00:00
},
});