// Own first-page intent, inline/cache acquisition and application. (function () { /** @param {SearchBootstrapDependencies} deps */ function create(deps) { const policy = VoiceOfMLInitialPayload; function readInline() { return deps.inlinePayload(); } /** @param {SearchInitialPayload | null} data @param {number} intent @param {number | null} [acquiredAt] */ function apply(data, intent, acquiredAt = Date.now()) { if (intent !== deps.intent()) return false; if (!data || !Array.isArray(data.results) || !deps.eligible()) return false; if (!policy.accepts(data, deps.view())) return false; if (!deps.cancelSearch() || intent !== deps.intent()) return false; const page = deps.commitPage(data); if (acquiredAt !== null) deps.cacheResponse(Object.assign(deps.cloneData(data), page), acquiredAt); deps.renderPage(); deps.syncRoute(); deps.prefetch(); return true; } /** @param {number} intent */ async function fresh(intent) { const { mode, repo } = deps.view(); // View identity alone cannot distinguish a new intent returning to the same key. const key = deps.viewKey(); const current = () => intent === deps.intent() && deps.view().mode === mode && deps.view().repo === repo && deps.viewKey() === key; if (!deps.eligible()) { if (!current()) return; deps.search(); return; } if (!current()) return; const url = deps.payloadURL(); const cached = deps.cachedResponse(); let entry = deps.cache.get(url); if (cached && deps.applyPayload(Object.assign(deps.cloneData(cached), { mode: deps.view().mode, repo: deps.view().repoFull, }), intent, null)) { if (entry && deps.identity(entry.data) !== deps.identity(cached)) deps.cache.delete(url); deps.invalidateInline(cached); return; } if (deps.applyPayload(deps.readInline(), intent, deps.inlineAcquiredAt)) return; try { if (!entry) { const data = await deps.fetchJSON(url, 6000); if (!current()) return; if (data && policy.accepts(data, deps.view())) { entry = { data, time: Date.now() }; deps.cache.set(url, entry); } } if (!current()) return; if (entry && policy.accepts(entry.data, deps.view())) { if (deps.applyPayload(entry.data, intent, entry.time)) return; } else if (entry && deps.cache.get(url) === entry) deps.cache.delete(url); } catch (e) {} if (!current()) return; deps.search(); } return { readInline, apply, fresh }; } globalThis.VoiceOfMLSearchBootstrap = { create }; })();