let rors = $state({})
const asked = new Set()
let pending = new Set()
let scheduled = false
const BATCH = 100
async function resolve() {
scheduled = false
const ids = [...pending].slice(0, BATCH)
pending = new Set([...pending].slice(BATCH))
if (ids.length === 0) return
const params = ids.map(id => `id=${encodeURIComponent(id)}`).join('&')
try {
const response = await fetch(`/api/funders?${params}`)
if (response.ok) {
const found = await response.json()
rors = { ...rors, ...Object.fromEntries(ids.map(id => [id, found[id] ?? ''])) }
}
} catch { }
if (pending.size > 0) schedule()
}
function schedule() {
if (scheduled) return
scheduled = true
queueMicrotask(resolve)
}
export function funderRor(funderId = '') {
const id = String(funderId ?? '').trim()
if (!id || !id.includes('10.13039/')) return ''
if (id in rors) return rors[id]
if (!asked.has(id)) {
asked.add(id)
pending.add(id)
schedule()
}
return ''
}