Skip to content

Commit a0a4f95

Browse files
committed
refactor(core/biblio): add biblio to conf.state instead of export
1 parent 26b984e commit a0a4f95

File tree

5 files changed

+41
-38
lines changed

5 files changed

+41
-38
lines changed

src/core/biblio.js

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,27 @@
88
import { biblioDB } from "./biblio-db.js";
99
import { createResourceHint } from "./utils.js";
1010

11-
/** @type {Conf['biblio']} */
12-
export const biblio = {};
13-
1411
// for backward compatibity
1512
export { wireReference, stringifyReference } from "./render-biblio.js";
1613

1714
export const name = "core/biblio";
1815

1916
const bibrefsURL = new URL("https://specref.herokuapp.com/bibrefs?refs=");
2017

21-
export async function prepare() {
18+
export async function prepare(conf) {
2219
// Opportunistically dns-prefetch to bibref server, as we don't know yet
2320
// if we will actually need to download references yet.
2421
const link = createResourceHint({
2522
hint: "dns-prefetch",
2623
href: bibrefsURL.origin,
2724
});
2825
document.head.appendChild(link);
29-
}
30-
31-
let doneResolver;
3226

33-
/** @type {Promise<Conf['biblio']>} */
34-
const done = new Promise(resolve => {
35-
doneResolver = resolve;
36-
});
27+
conf.state[name] = {
28+
/** @type {Conf['biblio']} */
29+
biblio: {},
30+
};
31+
}
3732

3833
export async function updateFromNetwork(
3934
refs,
@@ -65,17 +60,17 @@ export async function updateFromNetwork(
6560
}
6661

6762
/**
63+
* @param {Conf['biblio']} biblio
6864
* @param {string} key
69-
* @returns {Promise<BiblioData>}
65+
* @returns {BiblioData}
7066
*/
71-
export async function resolveRef(key) {
72-
const biblio = await done;
67+
export function resolveRef(biblio, key) {
7368
if (!biblio.hasOwnProperty(key)) {
7469
return null;
7570
}
7671
const entry = biblio[key];
7772
if (entry.aliasOf) {
78-
return await resolveRef(entry.aliasOf);
73+
return resolveRef(biblio, entry.aliasOf);
7974
}
8075
return entry;
8176
}
@@ -133,12 +128,11 @@ export class Plugin {
133128
}
134129

135130
async run() {
136-
const finish = () => {
137-
doneResolver(this.conf.biblio);
138-
};
139131
if (!this.conf.localBiblio) {
140132
this.conf.localBiblio = {};
141133
}
134+
/** @type {Conf["biblio"]} */
135+
const biblio = this.conf.state[name].biblio;
142136
this.conf.biblio = biblio;
143137
const localAliases = Object.keys(this.conf.localBiblio)
144138
.filter(key => this.conf.localBiblio[key].hasOwnProperty("aliasOf"))
@@ -172,6 +166,5 @@ export class Plugin {
172166
Object.assign(biblio, data);
173167
}
174168
Object.assign(biblio, this.conf.localBiblio);
175-
finish();
176169
}
177170
}

src/core/data-cite.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
* `data-cite` to `href` attributes. `data-cite` attributes are added to markup
1010
* directly by the author as well as via other modules like core/xref.
1111
*/
12-
import { biblio, resolveRef, updateFromNetwork } from "./biblio.js";
1312
import {
1413
refTypeFromContext,
1514
showError,
1615
showWarning,
1716
wrapInner,
1817
} from "./utils.js";
18+
import { resolveRef, updateFromNetwork } from "./biblio.js";
1919
import { sub } from "./pubsubhub.js";
2020
export const name = "core/data-cite";
2121

@@ -26,9 +26,10 @@ export const name = "core/data-cite";
2626
export const THIS_SPEC = "__SPEC__";
2727

2828
/**
29+
* @param {Conf["biblio"]} biblio
2930
* @param {CiteDetails} citeDetails
3031
*/
31-
async function getLinkProps(citeDetails) {
32+
function getLinkProps(biblio, citeDetails) {
3233
const { key, frag, path } = citeDetails;
3334
let href = "";
3435
let title = "";
@@ -37,7 +38,7 @@ async function getLinkProps(citeDetails) {
3738
href = document.location.href;
3839
} else {
3940
// Let's go look it up in spec ref...
40-
const entry = await resolveRef(key);
41+
const entry = resolveRef(biblio, key);
4142
if (!entry) {
4243
return null;
4344
}
@@ -156,18 +157,20 @@ export function toCiteDetails(elem) {
156157
return details;
157158
}
158159

159-
export async function run() {
160+
export async function run(conf) {
161+
/** @type {Conf["biblio"]} */
162+
const biblio = conf.state["core/biblio"].biblio;
160163
/** @type {NodeListOf<HTMLElement>} */
161164
const elems = document.querySelectorAll(
162165
"dfn[data-cite]:not([data-cite='']), a[data-cite]:not([data-cite=''])"
163166
);
164167

165-
await updateBiblio([...elems]);
168+
await updateBiblio(biblio, [...elems]);
166169

167170
for (const elem of elems) {
168171
const originalKey = elem.dataset.cite;
169172
const citeDetails = toCiteDetails(elem);
170-
const linkProps = await getLinkProps(citeDetails);
173+
const linkProps = getLinkProps(biblio, citeDetails);
171174
if (linkProps) {
172175
linkElem(elem, linkProps, citeDetails);
173176
} else {
@@ -181,14 +184,14 @@ export async function run() {
181184

182185
/**
183186
* Fetch and update `biblio` with entries corresponding to given elements
187+
* @param {Conf["biblio"]} biblio
184188
* @param {HTMLElement[]} elems
185189
*/
186-
async function updateBiblio(elems) {
187-
const promisesForBibEntries = elems.map(toCiteDetails).map(async entry => {
188-
const result = await resolveRef(entry.key);
190+
async function updateBiblio(biblio, elems) {
191+
const bibEntries = elems.map(toCiteDetails).map(entry => {
192+
const result = resolveRef(biblio, entry.key);
189193
return { entry, result };
190194
});
191-
const bibEntries = await Promise.all(promisesForBibEntries);
192195

193196
const missingBibEntries = bibEntries
194197
.filter(({ result }) => result === null)

src/core/render-biblio.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// renders the biblio data pre-processed in core/biblio
44

55
import { addId, getIntlData, showError } from "./utils.js";
6-
import { biblio } from "./biblio.js";
76
import { html } from "./import-maps.js";
87

98
export const name = "core/render-biblio";
@@ -73,6 +72,8 @@ const endWithDot = endNormalizer(".");
7372

7473
/** @param {Conf} conf */
7574
export function run(conf) {
75+
/** @type {Conf["biblio"]} */
76+
const biblio = conf.state["core/biblio"].biblio;
7677
const informs = Array.from(conf.informativeReferences);
7778
const norms = Array.from(conf.normativeReferences);
7879

@@ -90,24 +91,27 @@ export function run(conf) {
9091
refSection.classList.add("appendix");
9192

9293
if (norms.length) {
93-
const sec = createReferencesSection(norms, l10n.norm_references);
94+
const sec = createReferencesSection(biblio, norms, l10n.norm_references);
9495
refSection.appendChild(sec);
9596
}
9697
if (informs.length) {
97-
const sec = createReferencesSection(informs, l10n.info_references);
98+
const sec = createReferencesSection(biblio, informs, l10n.info_references);
9899
refSection.appendChild(sec);
99100
}
100101

101102
document.body.appendChild(refSection);
102103
}
103104

104105
/**
106+
* @param {Conf["biblio"]} biblio
105107
* @param {string[]} refs
106108
* @param {string} title
107109
* @returns {HTMLElement}
108110
*/
109-
function createReferencesSection(refs, title) {
110-
const { goodRefs, badRefs } = groupRefs(refs.map(toRefContent));
111+
function createReferencesSection(biblio, refs, title) {
112+
const { goodRefs, badRefs } = groupRefs(
113+
refs.map(ref => toRefContent(biblio, ref))
114+
);
111115
const uniqueRefs = getUniqueRefs(goodRefs);
112116

113117
const refsToShow = uniqueRefs
@@ -132,10 +136,11 @@ function createReferencesSection(refs, title) {
132136
/**
133137
* returns refcontent and unique key for a reference among its aliases
134138
* and warns about circular references
139+
* @param {Conf["biblio"]} biblio
135140
* @param {String} ref
136141
* @typedef {ReturnType<typeof toRefContent>} Ref
137142
*/
138-
function toRefContent(ref) {
143+
function toRefContent(biblio, ref) {
139144
let refcontent = biblio[ref];
140145
let key = ref;
141146
const circular = new Set([key]);

src/type-helper.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ interface Conf {
121121
localBiblio?: Record<string, BiblioData>;
122122
biblio: Record<string, BiblioData>;
123123
shortName: string;
124+
state: Record<string, Record<string, any>>;
124125
}
125126

126127
type ResourceHintOption = {

src/w3c/seo.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ export async function run(conf) {
6969
}
7070

7171
async function addJSONLDInfo(conf, doc) {
72+
/** @type {Conf["biblio"]} */
73+
const biblio = conf.state["core/biblio"].biblio;
74+
7275
// Content for JSON
7376
const type = ["TechArticle"];
7477
if (conf.rdfStatus) type.push(conf.rdfStatus);
@@ -133,9 +136,7 @@ async function addJSONLDInfo(conf, doc) {
133136
...conf.normativeReferences,
134137
...conf.informativeReferences,
135138
];
136-
const citationContents = await Promise.all(
137-
citationIds.map(ref => resolveRef(ref))
138-
);
139+
const citationContents = citationIds.map(ref => resolveRef(biblio, ref));
139140
jsonld.citation = citationContents
140141
.filter(ref => typeof ref === "object")
141142
.map(addRef);

0 commit comments

Comments
 (0)