Skip to content

Commit

Permalink
Merge branch 'parellel-promises' of https://github.com/NotWoods/eleventy
Browse files Browse the repository at this point in the history
 into NotWoods-parellel-promises

# Conflicts:
#	src/Engines/Html.js
#	src/Engines/Liquid.js
#	src/Template.js
  • Loading branch information
zachleat committed Oct 30, 2020
2 parents e923284 + feb5a1e commit 220aa97
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 106 deletions.
8 changes: 4 additions & 4 deletions src/Eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ Arguments:
this.watcher = watcher;

let watchDelay;
async function watchRun(path) {
let watchRun = async (path) => {
try {
this._addFileToWatchQueue(path);
clearTimeout(watchDelay);
Expand All @@ -687,16 +687,16 @@ Arguments:
EleventyErrorHandler.fatal(e, "Eleventy fatal watch error");
this.stopWatch();
}
}
};

watcher.on("change", async (path) => {
console.log("File changed:", path);
await watchRun.call(this, path);
await watchRun(path);
});

watcher.on("add", async (path) => {
console.log("File added:", path);
await watchRun.call(this, path);
await watchRun(path);
});

process.on("SIGINT", () => this.stopWatch());
Expand Down
4 changes: 3 additions & 1 deletion src/Engines/Html.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ class Html extends TemplateEngine {
super.getIncludesDir(),
this.extensionMap
);
let fn = await engine.compile(str, inputPath);
let fnReady = engine.compile(str, inputPath);

return async function (data) {
let fn = await fnReady;

return fn(data);
};
} else {
Expand Down
5 changes: 4 additions & 1 deletion src/Engines/Liquid.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ class Liquid extends TemplateEngine {

async compile(str, inputPath) {
let engine = this.liquidLib;
let tmpl = await engine.parse(str, inputPath);
let tmplReady = engine.parse(str, inputPath);

// Required for relative includes
let options = {};
Expand All @@ -215,7 +215,10 @@ class Liquid extends TemplateEngine {
TemplatePath.getDirFromFilePath(inputPath),
];
}

return async function (data) {
let tmpl = await tmplReady;

return engine.render(tmpl, data, options);
};
}
Expand Down
18 changes: 9 additions & 9 deletions src/Engines/Markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Markdown extends TemplateEngine {
// This is separate so devs can pass in a new mdLib and still use the official eleventy plugin for markdown highlighting
if (this.config.markdownHighlighter) {
this.mdLib.set({
highlight: this.config.markdownHighlighter
highlight: this.config.markdownHighlighter,
});
}

Expand All @@ -39,7 +39,7 @@ class Markdown extends TemplateEngine {

return Object.assign(
{
html: true
html: true,
},
this.markdownOptions || {}
);
Expand All @@ -49,8 +49,6 @@ class Markdown extends TemplateEngine {
let mdlib = this.mdLib;

if (preTemplateEngine) {
let fn;

let engine;
if (typeof preTemplateEngine === "string") {
engine = this.engineManager.getEngine(
Expand All @@ -62,26 +60,28 @@ class Markdown extends TemplateEngine {
engine = preTemplateEngine;
}

fn = await engine.compile(str, inputPath);
let fnReady = engine.compile(str, inputPath);

if (bypassMarkdown) {
return async function(data) {
return async function (data) {
let fn = await fnReady;
return fn(data);
};
} else {
return async function(data) {
return async function (data) {
let fn = await fnReady;
let preTemplateEngineRender = await fn(data);
let finishedRender = mdlib.render(preTemplateEngineRender, data);
return finishedRender;
};
}
} else {
if (bypassMarkdown) {
return function() {
return function () {
return str;
};
} else {
return function(data) {
return function (data) {
return mdlib.render(str, data);
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/Engines/TemplateEngine.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class TemplateEngine {
let fn = await this.compile(str);
return fn(data);
} catch (e) {
return Promise.reject(e);
throw e;
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/Plugins/Pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,12 @@ class Pagination {
cloned.setPaginationData(override);

// TO DO subdirectory to links if the site doesn’t live at /
links.push("/" + (await cloned.getOutputLink()));
hrefs.push(await cloned.getOutputHref());
let [outputLink, outputHref] = await Promise.all([
cloned.getOutputLink(),
cloned.getOutputHref(),
]);
links.push("/" + outputLink);
hrefs.push(outputHref);
}

// we loop twice to pass in the appropriate prev/next links (already full generated now)
Expand Down
182 changes: 94 additions & 88 deletions src/Template.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,19 +194,19 @@ class Template extends TemplateContent {
let str = await super.render(data, templateData, true);
return str;
} else if (Array.isArray(data)) {
let arr = [];
for (let j = 0, k = data.length; j < k; j++) {
arr.push(await this.mapDataAsRenderedTemplates(data[j], templateData));
}
return arr;
return Promise.all(
data.map((item) => this.mapDataAsRenderedTemplates(item, templateData))
);
} else if (lodashIsObject(data)) {
let obj = {};
for (let value in data) {
obj[value] = await this.mapDataAsRenderedTemplates(
data[value],
templateData
);
}
await Promise.all(
Object.keys(data).map(async (value) => {
obj[value] = await this.mapDataAsRenderedTemplates(
data[value],
templateData
);
})
);
return obj;
}

Expand Down Expand Up @@ -463,85 +463,90 @@ class Template extends TemplateContent {
}

async getTemplates(data) {
let results = [];

if (!Pagination.hasPagination(data)) {
await this.addComputedData(data);

results.push({
template: this,
inputPath: this.inputPath,
fileSlug: this.fileSlugStr,
filePathStem: this.filePathStem,
data: data,
date: data.page.date,
outputPath: data.page.outputPath,
url: data.page.url,
set templateContent(content) {
this._templateContent = content;
},
get templateContent() {
if (this._templateContent === undefined) {
// should at least warn here
throw new TemplateContentPrematureUseError(
`Tried to use templateContent too early (${this.inputPath})`
);
}
return this._templateContent;
},
});
} else {
// needs collections for pagination items
// but individual pagination entries won’t be part of a collection
this.paging = new Pagination(data);
this.paging.setTemplate(this);
let pageTemplates = await this.paging.getPageTemplates();
let pageNumber = 0;
for (let page of pageTemplates) {
let pageData = Object.assign({}, await page.getData());

await page.addComputedData(pageData);

// Issue #115
if (data.collections) {
pageData.collections = data.collections;
}

results.push({
template: page,
return [
{
template: this,
inputPath: this.inputPath,
fileSlug: this.fileSlugStr,
filePathStem: this.filePathStem,
data: pageData,
date: pageData.page.date,
pageNumber: pageNumber++,
outputPath: pageData.page.outputPath,
url: pageData.page.url,
data: data,
date: data.page.date,
outputPath: data.page.outputPath,
url: data.page.url,
set templateContent(content) {
this._templateContent = content;
},
get templateContent() {
if (this._templateContent === undefined) {
// should at least warn here
throw new TemplateContentPrematureUseError(
`Tried to use templateContent too early (${this.inputPath} page ${this.pageNumber})`
`Tried to use templateContent too early (${this.inputPath})`
);
}
return this._templateContent;
},
});
}
}
},
];
} else {
// needs collections for pagination items
// but individual pagination entries won’t be part of a collection
this.paging = new Pagination(data);
this.paging.setTemplate(this);
let pageTemplates = await this.paging.getPageTemplates();

return await Promise.all(
pageTemplates.map(async (page, pageNumber) => {
let pageData = Object.assign({}, await page.getData());

return results;
await page.addComputedData(pageData);

// Issue #115
if (data.collections) {
pageData.collections = data.collections;
}

return {
template: page,
inputPath: this.inputPath,
fileSlug: this.fileSlugStr,
filePathStem: this.filePathStem,
data: pageData,
date: pageData.page.date,
pageNumber: pageNumber,
outputPath: pageData.page.outputPath,
url: pageData.page.url,
set templateContent(content) {
this._templateContent = content;
},
get templateContent() {
if (this._templateContent === undefined) {
throw new TemplateContentPrematureUseError(
`Tried to use templateContent too early (${this.inputPath} page ${this.pageNumber})`
);
}
return this._templateContent;
},
};
})
);
}
}

async getRenderedTemplates(data) {
let pages = await this.getTemplates(data);
for (let page of pages) {
let content = await page.template._getContent(page.outputPath, page.data);
await Promise.all(
pages.map(async (page) => {
let content = await page.template._getContent(
page.outputPath,
page.data
);

page.templateContent = content;
}
page.templateContent = content;
})
);
return pages;
}

Expand Down Expand Up @@ -618,14 +623,12 @@ class Template extends TemplateContent {
}

async writeMapEntry(mapEntry) {
let promises = [];
for (let page of mapEntry._pages) {
let content = await this.renderPageEntry(mapEntry, page);
let promise = this._write(page.outputPath, content);
promises.push(promise);
}

return Promise.all(promises);
await Promise.all(
mapEntry._pages.map(async (page) => {
let content = await this.renderPageEntry(mapEntry, page);
return this._write(page.outputPath, content);
})
);
}

// TODO is this still used by anything but tests?
Expand All @@ -636,7 +639,7 @@ class Template extends TemplateContent {
promises.push(this._write(tmpl.outputPath, tmpl.templateContent));
}

return Promise.all(promises);
await Promise.all(promises);
}

// TODO this but better
Expand Down Expand Up @@ -763,19 +766,22 @@ class Template extends TemplateContent {

async _testCompleteRender() {
let entries = await this.getTemplateMapEntries();
let contents = [];

for (let entry of entries) {
entry._pages = await entry.template.getTemplates(entry.data);
let nestedContent = await Promise.all(
entries.map(async (entry) => {
entry._pages = await entry.template.getTemplates(entry.data);
return Promise.all(
entry._pages.map(async (page) => {
page.templateContent = await entry.template.getTemplateMapContent(
page
);
return this.renderPageEntry(entry, page);
})
);
})
);

let page;
for (page of entry._pages) {
page.templateContent = await entry.template.getTemplateMapContent(page);
}
for (page of entry._pages) {
contents.push(await this.renderPageEntry(entry, page));
}
}
let contents = [].concat(...nestedContent);
return contents;
}
}
Expand Down

0 comments on commit 220aa97

Please sign in to comment.