docs/.vitepress/theme/components/extrepo/iconQueue.ts

895a3c0227ce8313912df20aedc960d9fb22aeca
wotaku/docs/.vitepress/theme/components/extrepo/iconQueue.ts

26 lines · 611 bytes

 1const MAX_CONCURRENT_MOBILE = 6
 2const MAX_CONCURRENT_DESKTOP = 24
 3
 4function maxConcurrent(): number {
 5  if (typeof window === 'undefined') return MAX_CONCURRENT_DESKTOP
 6  return window.matchMedia('(max-width: 767px)').matches ? MAX_CONCURRENT_MOBILE : MAX_CONCURRENT_DESKTOP
 7}
 8
 9let active = 0
10const queue: (() => Promise<void>)[] = []
11
12function pump() {
13  while (active < maxConcurrent() && queue.length) {
14    const job = queue.shift()!
15    active++
16    job().finally(() => {
17      active--
18      pump()
19    })
20  }
21}
22
23export function scheduleIconLoad(load: () => Promise<void>) {
24  queue.push(load)
25  pump()
26}