From 430cdce8b27e376511c7e1048080dd820c093d1c Mon Sep 17 00:00:00 2001 From: Sid Vishnoi Date: Fri, 19 Feb 2021 20:12:00 +0530 Subject: [PATCH] refactor(core/biblio): add `biblio` to conf.state instead of export --- src/core/biblio.js | 31 ++++++++++++------------------- src/core/data-cite.js | 23 +++++++++++++---------- src/core/render-biblio.js | 17 +++++++++++------ src/type-helper.d.ts | 1 + src/w3c/seo.js | 7 ++++--- 5 files changed, 41 insertions(+), 38 deletions(-) diff --git a/src/core/biblio.js b/src/core/biblio.js index c13d2dd02d..9cec7e7116 100644 --- a/src/core/biblio.js +++ b/src/core/biblio.js @@ -8,9 +8,6 @@ import { biblioDB } from "./biblio-db.js"; import { createResourceHint } from "./utils.js"; -/** @type {Conf['biblio']} */ -export const biblio = {}; - // for backward compatibity export { wireReference, stringifyReference } from "./render-biblio.js"; @@ -18,7 +15,7 @@ export const name = "core/biblio"; const bibrefsURL = new URL("https://specref.herokuapp.com/bibrefs?refs="); -export async function prepare() { +export async function prepare(conf) { // Opportunistically dns-prefetch to bibref server, as we don't know yet // if we will actually need to download references yet. const link = createResourceHint({ @@ -26,14 +23,12 @@ export async function prepare() { href: bibrefsURL.origin, }); document.head.appendChild(link); -} - -let doneResolver; -/** @type {Promise} */ -const done = new Promise(resolve => { - doneResolver = resolve; -}); + conf.state[name] = { + /** @type {Conf['biblio']} */ + biblio: {}, + }; +} export async function updateFromNetwork( refs, @@ -65,17 +60,17 @@ export async function updateFromNetwork( } /** + * @param {Conf['biblio']} biblio * @param {string} key - * @returns {Promise} + * @returns {BiblioData} */ -export async function resolveRef(key) { - const biblio = await done; +export function resolveRef(biblio, key) { if (!biblio.hasOwnProperty(key)) { return null; } const entry = biblio[key]; if (entry.aliasOf) { - return await resolveRef(entry.aliasOf); + return resolveRef(biblio, entry.aliasOf); } return entry; } @@ -133,12 +128,11 @@ export class Plugin { } async run() { - const finish = () => { - doneResolver(this.conf.biblio); - }; if (!this.conf.localBiblio) { this.conf.localBiblio = {}; } + /** @type {Conf["biblio"]} */ + const biblio = this.conf.state[name].biblio; this.conf.biblio = biblio; const localAliases = Object.keys(this.conf.localBiblio) .filter(key => this.conf.localBiblio[key].hasOwnProperty("aliasOf")) @@ -172,6 +166,5 @@ export class Plugin { Object.assign(biblio, data); } Object.assign(biblio, this.conf.localBiblio); - finish(); } } diff --git a/src/core/data-cite.js b/src/core/data-cite.js index 1e7e34f579..e596591c11 100644 --- a/src/core/data-cite.js +++ b/src/core/data-cite.js @@ -9,13 +9,13 @@ * `data-cite` to `href` attributes. `data-cite` attributes are added to markup * directly by the author as well as via other modules like core/xref. */ -import { biblio, resolveRef, updateFromNetwork } from "./biblio.js"; import { refTypeFromContext, showError, showWarning, wrapInner, } from "./utils.js"; +import { resolveRef, updateFromNetwork } from "./biblio.js"; import { sub } from "./pubsubhub.js"; export const name = "core/data-cite"; @@ -26,9 +26,10 @@ export const name = "core/data-cite"; export const THIS_SPEC = "__SPEC__"; /** + * @param {Conf["biblio"]} biblio * @param {CiteDetails} citeDetails */ -async function getLinkProps(citeDetails) { +function getLinkProps(biblio, citeDetails) { const { key, frag, path } = citeDetails; let href = ""; let title = ""; @@ -37,7 +38,7 @@ async function getLinkProps(citeDetails) { href = document.location.href; } else { // Let's go look it up in spec ref... - const entry = await resolveRef(key); + const entry = resolveRef(biblio, key); if (!entry) { return null; } @@ -156,18 +157,20 @@ export function toCiteDetails(elem) { return details; } -export async function run() { +export async function run(conf) { + /** @type {Conf["biblio"]} */ + const biblio = conf.state["core/biblio"].biblio; /** @type {NodeListOf} */ const elems = document.querySelectorAll( "dfn[data-cite]:not([data-cite='']), a[data-cite]:not([data-cite=''])" ); - await updateBiblio([...elems]); + await updateBiblio(biblio, [...elems]); for (const elem of elems) { const originalKey = elem.dataset.cite; const citeDetails = toCiteDetails(elem); - const linkProps = await getLinkProps(citeDetails); + const linkProps = getLinkProps(biblio, citeDetails); if (linkProps) { linkElem(elem, linkProps, citeDetails); } else { @@ -181,14 +184,14 @@ export async function run() { /** * Fetch and update `biblio` with entries corresponding to given elements + * @param {Conf["biblio"]} biblio * @param {HTMLElement[]} elems */ -async function updateBiblio(elems) { - const promisesForBibEntries = elems.map(toCiteDetails).map(async entry => { - const result = await resolveRef(entry.key); +async function updateBiblio(biblio, elems) { + const bibEntries = elems.map(toCiteDetails).map(entry => { + const result = resolveRef(biblio, entry.key); return { entry, result }; }); - const bibEntries = await Promise.all(promisesForBibEntries); const missingBibEntries = bibEntries .filter(({ result }) => result === null) diff --git a/src/core/render-biblio.js b/src/core/render-biblio.js index 4adb2cb530..42d184db4c 100644 --- a/src/core/render-biblio.js +++ b/src/core/render-biblio.js @@ -3,7 +3,6 @@ // renders the biblio data pre-processed in core/biblio import { addId, getIntlData, showError } from "./utils.js"; -import { biblio } from "./biblio.js"; import { html } from "./import-maps.js"; export const name = "core/render-biblio"; @@ -73,6 +72,8 @@ const endWithDot = endNormalizer("."); /** @param {Conf} conf */ export function run(conf) { + /** @type {Conf["biblio"]} */ + const biblio = conf.state["core/biblio"].biblio; const informs = Array.from(conf.informativeReferences); const norms = Array.from(conf.normativeReferences); @@ -90,11 +91,11 @@ export function run(conf) { refSection.classList.add("appendix"); if (norms.length) { - const sec = createReferencesSection(norms, l10n.norm_references); + const sec = createReferencesSection(biblio, norms, l10n.norm_references); refSection.appendChild(sec); } if (informs.length) { - const sec = createReferencesSection(informs, l10n.info_references); + const sec = createReferencesSection(biblio, informs, l10n.info_references); refSection.appendChild(sec); } @@ -102,12 +103,15 @@ export function run(conf) { } /** + * @param {Conf["biblio"]} biblio * @param {string[]} refs * @param {string} title * @returns {HTMLElement} */ -function createReferencesSection(refs, title) { - const { goodRefs, badRefs } = groupRefs(refs.map(toRefContent)); +function createReferencesSection(biblio, refs, title) { + const { goodRefs, badRefs } = groupRefs( + refs.map(ref => toRefContent(biblio, ref)) + ); const uniqueRefs = getUniqueRefs(goodRefs); const refsToShow = uniqueRefs @@ -132,10 +136,11 @@ function createReferencesSection(refs, title) { /** * returns refcontent and unique key for a reference among its aliases * and warns about circular references + * @param {Conf["biblio"]} biblio * @param {String} ref * @typedef {ReturnType} Ref */ -function toRefContent(ref) { +function toRefContent(biblio, ref) { let refcontent = biblio[ref]; let key = ref; const circular = new Set([key]); diff --git a/src/type-helper.d.ts b/src/type-helper.d.ts index a39b8fca9b..ca1b0ec5c8 100644 --- a/src/type-helper.d.ts +++ b/src/type-helper.d.ts @@ -121,6 +121,7 @@ interface Conf { localBiblio?: Record; biblio: Record; shortName: string; + state: Record>; } type ResourceHintOption = { diff --git a/src/w3c/seo.js b/src/w3c/seo.js index d0ad71544d..0379fba456 100644 --- a/src/w3c/seo.js +++ b/src/w3c/seo.js @@ -69,6 +69,9 @@ export async function run(conf) { } async function addJSONLDInfo(conf, doc) { + /** @type {Conf["biblio"]} */ + const biblio = conf.state["core/biblio"].biblio; + // Content for JSON const type = ["TechArticle"]; if (conf.rdfStatus) type.push(conf.rdfStatus); @@ -133,9 +136,7 @@ async function addJSONLDInfo(conf, doc) { ...conf.normativeReferences, ...conf.informativeReferences, ]; - const citationContents = await Promise.all( - citationIds.map(ref => resolveRef(ref)) - ); + const citationContents = citationIds.map(ref => resolveRef(biblio, ref)); jsonld.citation = citationContents .filter(ref => typeof ref === "object") .map(addRef);