File size: 2,734 Bytes
732c5cb 01274b0 732c5cb 01274b0 732c5cb 01274b0 732c5cb 01274b0 732c5cb 01274b0 732c5cb 9bec131 01274b0 732c5cb 9bec131 732c5cb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | // 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 };
})();
|