2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2019-2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-09-26 19:56:31 +00:00
|
|
|
import PQueue from 'p-queue';
|
|
|
|
|
2020-10-13 22:21:42 +00:00
|
|
|
import { sleep } from './sleep';
|
|
|
|
|
2020-09-14 21:56:35 +00:00
|
|
|
declare global {
|
|
|
|
interface Window {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
waitBatchers: Array<BatcherType<any>>;
|
|
|
|
waitForAllWaitBatchers: () => Promise<unknown>;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-26 19:56:31 +00:00
|
|
|
window.waitBatchers = [];
|
|
|
|
|
|
|
|
window.waitForAllWaitBatchers = async () => {
|
|
|
|
await Promise.all(window.waitBatchers.map(item => item.onIdle()));
|
|
|
|
};
|
|
|
|
|
|
|
|
type ItemHolderType<ItemType> = {
|
2020-09-14 21:56:35 +00:00
|
|
|
resolve?: (value?: unknown) => void;
|
|
|
|
reject?: (error: Error) => void;
|
2019-09-26 19:56:31 +00:00
|
|
|
item: ItemType;
|
|
|
|
};
|
|
|
|
|
|
|
|
type ExplodedPromiseType = {
|
2020-09-14 21:56:35 +00:00
|
|
|
resolve?: (value?: unknown) => void;
|
|
|
|
reject?: (error: Error) => void;
|
|
|
|
promise: Promise<unknown>;
|
2019-09-26 19:56:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type BatcherOptionsType<ItemType> = {
|
|
|
|
wait: number;
|
|
|
|
maxSize: number;
|
|
|
|
processBatch: (items: Array<ItemType>) => Promise<void>;
|
|
|
|
};
|
|
|
|
|
|
|
|
type BatcherType<ItemType> = {
|
|
|
|
add: (item: ItemType) => Promise<void>;
|
|
|
|
anyPending: () => boolean;
|
|
|
|
onIdle: () => Promise<void>;
|
|
|
|
unregister: () => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
export function createWaitBatcher<ItemType>(
|
|
|
|
options: BatcherOptionsType<ItemType>
|
|
|
|
): BatcherType<ItemType> {
|
|
|
|
let waitBatcher: BatcherType<ItemType>;
|
2020-09-14 21:56:35 +00:00
|
|
|
let timeout: NodeJS.Timeout | null;
|
2019-09-26 19:56:31 +00:00
|
|
|
let items: Array<ItemHolderType<ItemType>> = [];
|
2020-09-18 20:40:41 +00:00
|
|
|
const queue = new PQueue({ concurrency: 1, timeout: 1000 * 60 * 2 });
|
2019-09-26 19:56:31 +00:00
|
|
|
|
|
|
|
function _kickBatchOff() {
|
|
|
|
const itemsRef = items;
|
|
|
|
items = [];
|
|
|
|
queue.add(async () => {
|
|
|
|
try {
|
|
|
|
await options.processBatch(itemsRef.map(item => item.item));
|
|
|
|
itemsRef.forEach(item => {
|
2020-09-14 21:56:35 +00:00
|
|
|
if (item.resolve) {
|
|
|
|
item.resolve();
|
|
|
|
}
|
2019-09-26 19:56:31 +00:00
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
itemsRef.forEach(item => {
|
2020-09-14 21:56:35 +00:00
|
|
|
if (item.reject) {
|
|
|
|
item.reject(error);
|
|
|
|
}
|
2019-09-26 19:56:31 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function _makeExplodedPromise(): ExplodedPromiseType {
|
|
|
|
let resolve;
|
|
|
|
let reject;
|
|
|
|
|
|
|
|
const promise = new Promise((resolveParam, rejectParam) => {
|
|
|
|
resolve = resolveParam;
|
|
|
|
reject = rejectParam;
|
|
|
|
});
|
|
|
|
|
|
|
|
return { promise, resolve, reject };
|
|
|
|
}
|
|
|
|
|
|
|
|
async function add(item: ItemType) {
|
|
|
|
const { promise, resolve, reject } = _makeExplodedPromise();
|
2020-09-14 21:56:35 +00:00
|
|
|
|
2019-09-26 19:56:31 +00:00
|
|
|
items.push({
|
|
|
|
resolve,
|
|
|
|
reject,
|
|
|
|
item,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (timeout) {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
timeout = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (items.length >= options.maxSize) {
|
|
|
|
_kickBatchOff();
|
|
|
|
} else {
|
|
|
|
timeout = setTimeout(() => {
|
|
|
|
timeout = null;
|
|
|
|
_kickBatchOff();
|
|
|
|
}, options.wait);
|
|
|
|
}
|
|
|
|
|
|
|
|
await promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2020-09-14 21:56:35 +00:00
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2019-09-26 19:56:31 +00:00
|
|
|
await queue.onIdle();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (items.length > 0) {
|
2020-09-14 21:56:35 +00:00
|
|
|
// eslint-disable-next-line no-await-in-loop
|
2019-09-26 19:56:31 +00:00
|
|
|
await sleep(options.wait * 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function unregister() {
|
|
|
|
window.waitBatchers = window.waitBatchers.filter(
|
2020-09-14 21:56:35 +00:00
|
|
|
item => item !== waitBatcher
|
2019-09-26 19:56:31 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
waitBatcher = {
|
|
|
|
add,
|
|
|
|
anyPending,
|
|
|
|
onIdle,
|
|
|
|
unregister,
|
|
|
|
};
|
|
|
|
|
|
|
|
window.waitBatchers.push(waitBatcher);
|
|
|
|
|
|
|
|
return waitBatcher;
|
|
|
|
}
|