(() => { // At most two background listings at a time; no previews or revision fetches. const queue = []; let active = 0; const pump = () => { while (active < 2 && queue.length) { active++; queue.shift()().finally(() => { active--; pump(); }); } }; const enqueue = job => { queue.push(job); pump(); }; const observer = new IntersectionObserver(entries => { for (const entry of entries) if (entry.isIntersecting) { observer.unobserve(entry.target); entry.target._load(); } }, {rootMargin: '100px'}); for (const list of document.querySelectorAll('[data-bookmark-source]')) { const button = list.parentElement.querySelector('[data-bookmark-load]'); let after = '', busy = false; const load = () => { if (busy) return; busy = true; button.disabled = true; list.setAttribute('aria-busy', 'true'); enqueue(async () => { try { const url = new URL(list.dataset.bookmarkSource, location.origin); if (after) url.searchParams.set('after', after); const response = await fetch(url, {cache: 'no-store', signal: AbortSignal.timeout(15000)}); if (!response.ok) throw new Error(String(response.status)); const data = await response.json(); for (const link of data.links) { const li = document.createElement('li'), a = document.createElement('a'); a.href = link.href; a.textContent = link.label; li.append(a); list.append(li); } after = data.next; button.hidden = !after; button.textContent = button.dataset.more; } catch (_) { button.textContent = button.dataset.error; } finally { busy = false; button.disabled = false; list.setAttribute('aria-busy', 'false'); } }); }; list._load = load; button.addEventListener('click', load); observer.observe(list); } })();