Signal-Desktop/ts/util/batcher.ts

122 lines
2.9 KiB
TypeScript
Raw Normal View History

// Copyright 2019-2021 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
2019-09-26 19:56:31 +00:00
import PQueue from 'p-queue';
import { sleep } from './sleep';
declare global {
// We want to extend `window`'s properties, so we need an interface.
// eslint-disable-next-line no-restricted-syntax
interface Window {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
batchers: Array<BatcherType<any>>;
waitForAllBatchers: () => Promise<unknown>;
}
}
2019-09-26 19:56:31 +00:00
window.batchers = [];
window.waitForAllBatchers = async () => {
await Promise.all(window.batchers.map(item => item.flushAndWait()));
2019-09-26 19:56:31 +00:00
};
export type BatcherOptionsType<ItemType> = {
2021-03-26 00:00:03 +00:00
name: string;
2019-09-26 19:56:31 +00:00
wait: number;
maxSize: number;
processBatch: (items: Array<ItemType>) => void | Promise<void>;
2019-09-26 19:56:31 +00:00
};
export type BatcherType<ItemType> = {
2019-09-26 19:56:31 +00:00
add: (item: ItemType) => void;
anyPending: () => boolean;
onIdle: () => Promise<void>;
flushAndWait: () => Promise<void>;
2019-09-26 19:56:31 +00:00
unregister: () => void;
};
export function createBatcher<ItemType>(
options: BatcherOptionsType<ItemType>
): BatcherType<ItemType> {
let batcher: BatcherType<ItemType>;
let timeout: NodeJS.Timeout | null;
2019-09-26 19:56:31 +00:00
let items: Array<ItemType> = [];
const queue = new PQueue({ concurrency: 1, timeout: 1000 * 60 * 2 });
2019-09-26 19:56:31 +00:00
function _kickBatchOff() {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
2019-09-26 19:56:31 +00:00
const itemsRef = items;
items = [];
queue.add(async () => {
await options.processBatch(itemsRef);
});
}
function add(item: ItemType) {
items.push(item);
2021-03-26 00:00:03 +00:00
if (items.length === 1) {
// Set timeout once when we just pushed the first item so that the wait
// time is bounded by `options.wait` and not extended by further pushes.
timeout = setTimeout(_kickBatchOff, options.wait);
2021-03-26 00:00:03 +00:00
} else if (items.length >= options.maxSize) {
_kickBatchOff();
2019-09-26 19:56:31 +00:00
}
}
function anyPending(): boolean {
return queue.size > 0 || queue.pending > 0 || items.length > 0;
}
async function onIdle() {
while (anyPending()) {
if (queue.size > 0 || queue.pending > 0) {
// eslint-disable-next-line no-await-in-loop
2019-09-26 19:56:31 +00:00
await queue.onIdle();
}
if (items.length > 0) {
// eslint-disable-next-line no-await-in-loop
2019-09-26 19:56:31 +00:00
await sleep(options.wait * 2);
}
}
}
function unregister() {
window.batchers = window.batchers.filter(item => item !== batcher);
2019-09-26 19:56:31 +00:00
}
async function flushAndWait() {
2021-03-26 00:00:03 +00:00
window.log.info(
`Flushing ${options.name} batcher items.length=${items.length}`
);
while (anyPending()) {
_kickBatchOff();
2021-03-26 00:00:03 +00:00
if (queue.size > 0 || queue.pending > 0) {
// eslint-disable-next-line no-await-in-loop
await queue.onIdle();
}
}
window.log.info(`Flushing complete ${options.name} for batcher`);
}
2019-09-26 19:56:31 +00:00
batcher = {
add,
anyPending,
onIdle,
flushAndWait,
2019-09-26 19:56:31 +00:00
unregister,
};
window.batchers.push(batcher);
return batcher;
}