Fix infinite loop when no citations were present

This commit is contained in:
Ludwig Schubert
2017-08-10 11:19:32 -07:00
parent a1800ca2b3
commit 8f4722173a
4 changed files with 211 additions and 12 deletions
+6
View File
@@ -72,6 +72,12 @@ export class Cite extends T(HTMLElement) {
}
disconnectedCallback() {
const options = { detail: [this, this.keys], bubbles: true };
const event = new CustomEvent('onCiteKeyRemoved', options);
document.dispatchEvent(event);
}
/* Observed Attributes */
// renderCitationNumbers(citations) {
+15 -11
View File
@@ -17,14 +17,14 @@ export const Controller = {
const [citeTag, keys] = event.detail;
// ensure we have citations
if (frontMatter.citations.length === 0) {
if (!frontMatter.citationsCollected) {
// console.debug('onCiteKeyCreated, but unresolved dependency ("citations"). Enqueing.');
Controller.waitingOn.citations.push(() => Controller.listeners.onCiteKeyCreated(event));
return;
}
// ensure we have a loaded bibliography
if (frontMatter.bibliography.size === 0) {
if (!frontMatter.bibliographyParsed) {
// console.debug('onCiteKeyCreated, but unresolved dependency ("bibliography"). Enqueing.');
Controller.waitingOn.bibliography.push(() => Controller.listeners.onCiteKeyCreated(event));
return;
@@ -37,11 +37,12 @@ export const Controller = {
},
onCiteKeyChanged(event) {
const [citeTag, keys] = event.detail;
// const [citeTag, keys] = event.detail;
// update citations
frontMatter.citations = collectCitations();
for (const waitingCallback of Controller.waitingOn.citations) {
frontMatter.citationsCollected = true;
for (const waitingCallback of Controller.waitingOn.citations.slice()) {
waitingCallback();
}
@@ -61,10 +62,10 @@ export const Controller = {
citeTag.entries = entries;
}
const numbers = keys.map( key => frontMatter.citations.indexOf(key) );
citeTag.numbers = numbers;
const entries = keys.map( key => frontMatter.bibliography.get(key) );
citeTag.entries = entries;
},
onCiteKeyRemoved(event) {
Controller.listeners.onCiteKeyChanged(event);
},
onBibliographyChanged(event) {
@@ -72,12 +73,13 @@ export const Controller = {
const bibliography = event.detail;
frontMatter.bibliography = bibliography;
for (const waitingCallback of Controller.waitingOn.bibliography) {
frontMatter.bibliographyParsed = true;
for (const waitingCallback of Controller.waitingOn.bibliography.slice()) {
waitingCallback();
}
// ensure we have citations
if (frontMatter.citations.length === 0) {
if (!frontMatter.citationsCollected) {
Controller.waitingOn.citations.push( function() {
Controller.listeners.onBibliographyChanged({target: event.target, detail: event.detail});
});
@@ -87,6 +89,7 @@ export const Controller = {
const entries = new Map(frontMatter.citations.map( citationKey => {
return [citationKey, frontMatter.bibliography.get(citationKey)];
}));
bibliographyTag.entries = entries;
},
@@ -122,7 +125,8 @@ export const Controller = {
// console.debug('Resolving "citations" dependency due to initial DOM load.');
frontMatter.citations = collectCitations();
for (const waitingCallback of Controller.waitingOn.citations) {
frontMatter.citationsCollected = true;
for (const waitingCallback of Controller.waitingOn.citations.slice()) {
waitingCallback();
}
+2 -1
View File
@@ -33,6 +33,7 @@ export class FrontMatter {
this.authors = []; // Array of Author(s)
this.bibliography = new Map();
this.bibliographyParsed = false;
// {
// "gregor2015draw": {
// "title": "DRAW: A recurrent neural network for image generation",
@@ -47,7 +48,7 @@ export class FrontMatter {
// Citation keys should be listed in the order that they are appear in the document.
// Each key refers to a key in the bibliography dictionary.
this.citations = []; // [ "gregor2015draw", "mercier2011humans" ]
this.citationsCollected = false;
//
// Assigned from posts.csv