concurrent.ts
This file
Path
src/utils/concurrent.ts
55 lines 路 1.0 KiB 路 TypeScript
Last changed by
Not known.
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556/** * @module * Concurrent utility. */
const DEFAULT_CONCURRENCY = 1024
export interface Pool { run<T>(fn: () => T): Promise<T>}
export const createPool = ({ concurrency, interval,}: { concurrency?: number interval?: number} = {}): Pool => { concurrency ||= DEFAULT_CONCURRENCY
if (concurrency === Infinity) { // unlimited return { run: async (fn) => fn(), } }
const pool: Set<{}> = new Set() const run = async <T>( fn: () => T, promise?: Promise<T>, resolve?: (result: T) => void ): Promise<T> => { if (pool.size >= (concurrency as number)) { promise ||= new Promise<T>((r) => (resolve = r)) setTimeout(() => run(fn, promise, resolve)) return promise } const marker = {} pool.add(marker) const result = await fn() if (interval) { setTimeout(() => pool.delete(marker), interval) } else { pool.delete(marker) } if (resolve) { resolve(result) return promise as Promise<T> } else { return result } } return { run }}