deno.land / x / hono@v4.2.5 / utils / concurrent.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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 }}
hono

Version Info

Tagged at
a month ago