(input: QueueInput)
| 6 | } |
| 7 | |
| 8 | export function createRefreshQueue(input: QueueInput) { |
| 9 | const queued = new Map<string, string>() |
| 10 | let root = false |
| 11 | let running = false |
| 12 | let timer: ReturnType<typeof setTimeout> | undefined |
| 13 | |
| 14 | const key = input.key ?? ((directory: string) => directory) |
| 15 | |
| 16 | const tick = () => new Promise<void>((resolve) => setTimeout(resolve, 0)) |
| 17 | |
| 18 | const take = (count: number) => { |
| 19 | if (queued.size === 0) return [] as string[] |
| 20 | const items: string[] = [] |
| 21 | for (const [id, directory] of queued) { |
| 22 | queued.delete(id) |
| 23 | items.push(directory) |
| 24 | if (items.length >= count) break |
| 25 | } |
| 26 | return items |
| 27 | } |
| 28 | |
| 29 | const schedule = () => { |
| 30 | if (timer) return |
| 31 | timer = setTimeout(() => { |
| 32 | timer = undefined |
| 33 | void drain() |
| 34 | }, 0) |
| 35 | } |
| 36 | |
| 37 | const push = (directory: string) => { |
| 38 | if (!directory) return |
| 39 | queued.set(key(directory), directory) |
| 40 | if (input.paused()) return |
| 41 | schedule() |
| 42 | } |
| 43 | |
| 44 | const refresh = () => { |
| 45 | root = true |
| 46 | if (input.paused()) return |
| 47 | schedule() |
| 48 | } |
| 49 | |
| 50 | async function drain() { |
| 51 | if (running) return |
| 52 | running = true |
| 53 | try { |
| 54 | while (true) { |
| 55 | if (input.paused()) return |
| 56 | if (root) { |
| 57 | root = false |
| 58 | await input.bootstrap() |
| 59 | await tick() |
| 60 | continue |
| 61 | } |
| 62 | const dirs = take(2) |
| 63 | if (dirs.length === 0) return |
| 64 | await Promise.all(dirs.map((dir) => input.bootstrapInstance(dir))) |
| 65 | await tick() |
no outgoing calls
no test coverage detected