Migrate util, types, state, sticker-creator to ESLint
This commit is contained in:
parent
372aa44e49
commit
2ade4acd52
115 changed files with 647 additions and 448 deletions
|
@ -1,24 +1,29 @@
|
|||
import PQueue from 'p-queue';
|
||||
|
||||
// @ts-ignore
|
||||
declare global {
|
||||
interface Window {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
waitBatchers: Array<BatcherType<any>>;
|
||||
waitForAllWaitBatchers: () => Promise<unknown>;
|
||||
}
|
||||
}
|
||||
|
||||
window.waitBatchers = [];
|
||||
|
||||
// @ts-ignore
|
||||
window.waitForAllWaitBatchers = async () => {
|
||||
// @ts-ignore
|
||||
await Promise.all(window.waitBatchers.map(item => item.onIdle()));
|
||||
};
|
||||
|
||||
type ItemHolderType<ItemType> = {
|
||||
resolve: (value?: any) => void;
|
||||
reject: (error: Error) => void;
|
||||
resolve?: (value?: unknown) => void;
|
||||
reject?: (error: Error) => void;
|
||||
item: ItemType;
|
||||
};
|
||||
|
||||
type ExplodedPromiseType = {
|
||||
resolve: (value?: any) => void;
|
||||
reject: (error: Error) => void;
|
||||
promise: Promise<any>;
|
||||
resolve?: (value?: unknown) => void;
|
||||
reject?: (error: Error) => void;
|
||||
promise: Promise<unknown>;
|
||||
};
|
||||
|
||||
type BatcherOptionsType<ItemType> = {
|
||||
|
@ -43,7 +48,7 @@ export function createWaitBatcher<ItemType>(
|
|||
options: BatcherOptionsType<ItemType>
|
||||
): BatcherType<ItemType> {
|
||||
let waitBatcher: BatcherType<ItemType>;
|
||||
let timeout: any;
|
||||
let timeout: NodeJS.Timeout | null;
|
||||
let items: Array<ItemHolderType<ItemType>> = [];
|
||||
const queue = new PQueue({ concurrency: 1 });
|
||||
|
||||
|
@ -55,11 +60,15 @@ export function createWaitBatcher<ItemType>(
|
|||
try {
|
||||
await options.processBatch(itemsRef.map(item => item.item));
|
||||
itemsRef.forEach(item => {
|
||||
item.resolve();
|
||||
if (item.resolve) {
|
||||
item.resolve();
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
itemsRef.forEach(item => {
|
||||
item.reject(error);
|
||||
if (item.reject) {
|
||||
item.reject(error);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
@ -69,18 +78,17 @@ export function createWaitBatcher<ItemType>(
|
|||
let resolve;
|
||||
let reject;
|
||||
|
||||
// tslint:disable-next-line:promise-must-complete
|
||||
const promise = new Promise((resolveParam, rejectParam) => {
|
||||
resolve = resolveParam;
|
||||
reject = rejectParam;
|
||||
});
|
||||
|
||||
// @ts-ignore
|
||||
return { promise, resolve, reject };
|
||||
}
|
||||
|
||||
async function add(item: ItemType) {
|
||||
const { promise, resolve, reject } = _makeExplodedPromise();
|
||||
|
||||
items.push({
|
||||
resolve,
|
||||
reject,
|
||||
|
@ -111,19 +119,20 @@ export function createWaitBatcher<ItemType>(
|
|||
async function onIdle() {
|
||||
while (anyPending()) {
|
||||
if (queue.size > 0 || queue.pending > 0) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await queue.onIdle();
|
||||
}
|
||||
|
||||
if (items.length > 0) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await sleep(options.wait * 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function unregister() {
|
||||
// @ts-ignore
|
||||
window.waitBatchers = window.waitBatchers.filter(
|
||||
(item: any) => item !== waitBatcher
|
||||
item => item !== waitBatcher
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -134,7 +143,6 @@ export function createWaitBatcher<ItemType>(
|
|||
unregister,
|
||||
};
|
||||
|
||||
// @ts-ignore
|
||||
window.waitBatchers.push(waitBatcher);
|
||||
|
||||
return waitBatcher;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue