Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 19 additions & 21 deletions src/core/post-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,25 @@ export const name = "core/post-process";

export async function run(config) {
if (Array.isArray(config.postProcess)) {
const promises = config.postProcess
.filter(f => {
const isFunction = typeof f === "function";
if (!isFunction) {
const msg = "Every item in `postProcess` must be a JS function.";
showError(msg, name);
}
return isFunction;
})
.map(async (f, i) => {
const fnName = `${name}/${f.name || `[${i}]`}`;
const utils = makePluginUtils(fnName);
try {
return await f(config, document, utils);
} catch (err) {
const msg = `Function ${f.name} threw an error during \`postProcess\`.`;
const hint = "See developer console.";
showError(msg, name, { hint, cause: err });
}
});
await Promise.all(promises);
const functions = config.postProcess.filter(f => {
const isFunction = typeof f === "function";
if (!isFunction) {
const msg = "Every item in `postProcess` must be a JS function.";
showError(msg, name);
}
return isFunction;
});
for (const [i, f] of functions.entries()) {
const fnName = `${name}/${f.name || `[${i}]`}`;
const utils = makePluginUtils(fnName);
try {
await f(config, document, utils);
} catch (err) {
const msg = `Function ${f.name} threw an error during \`postProcess\`.`;
const hint = "See developer console.";
showError(msg, name, { hint, cause: err });
}
}
}
if (typeof config.afterEnd === "function") {
await config.afterEnd(config, document);
Expand Down
40 changes: 19 additions & 21 deletions src/core/pre-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,24 @@ export const name = "core/pre-process";

export async function run(config) {
if (Array.isArray(config.preProcess)) {
const promises = config.preProcess
.filter(f => {
const isFunction = typeof f === "function";
if (!isFunction) {
const msg = "Every item in `preProcess` must be a JS function.";
showError(msg, name);
}
return isFunction;
})
.map(async (f, i) => {
const fnName = `${name}/${f.name || `[${i}]`}`;
const utils = makePluginUtils(fnName);
try {
return await f(config, document, utils);
} catch (err) {
const msg = `Function ${f.name} threw an error during \`preProcess\`.`;
const hint = "See developer console.";
showError(msg, name, { hint, cause: err });
}
});
await Promise.all(promises);
const functions = config.preProcess.filter(f => {
const isFunction = typeof f === "function";
if (!isFunction) {
const msg = "Every item in `preProcess` must be a JS function.";
showError(msg, name);
}
return isFunction;
});
for (const [i, f] of functions.entries()) {
const fnName = `${name}/${f.name || `[${i}]`}`;
const utils = makePluginUtils(fnName);
try {
await f(config, document, utils);
} catch (err) {
const msg = `Function ${f.name} threw an error during \`preProcess\`.`;
const hint = "See developer console.";
showError(msg, name, { hint, cause: err });
}
}
}
}
23 changes: 14 additions & 9 deletions tests/spec/core/pre-process-spec.html
Original file line number Diff line number Diff line change
@@ -1,53 +1,58 @@
<!doctype html>
<script class='remove'>
let counter = 0;

function pass(id) {
document.getElementById(id).innerHTML = `pass ${++counter}`;
}

function noOp() {}

function preSyncFunc() {
document.getElementById("pre-sync").innerHTML = "pass";
pass("pre-sync");
}

function preAsyncFunc() {
return new Promise(resolve => {
setTimeout(() => {
document.getElementById("pre-async").innerHTML = "pass";
pass("pre-async");
resolve();
}, 4);
});
}

function preWithWarning(_conf, _doc, utils) {
document.getElementById("pre-warning").innerHTML = "pass";
pass("pre-warning");
utils.showWarning("This is a warning");
}

function postSyncFunc() {
document.getElementById("post-sync").innerHTML = "pass";
pass("post-sync");
}

function postAsyncFunc() {
return new Promise(resolve => {
setTimeout(() => {
document.getElementById("post-async").innerHTML = "pass";
pass("post-async");
resolve();
}, 4);
});
}

function postWithDetailedError(_conf, _doc, utils) {
const el = document.getElementById("post-error");
el.innerHTML = "pass";
pass("post-error");
utils.showError("This is an error", {
hint: "This is a hint",
title: "This is a title",
details: "This is a detailed error message",
elements: [el],
elements: [document.getElementById("post-error")],
});
}

function afterEnd() {
return new Promise(resolve => {
setTimeout(() => {
document.getElementById("afterend").innerHTML = "pass";
pass("afterend");
resolve();
}, 4);
});
Expand Down
26 changes: 18 additions & 8 deletions tests/spec/core/pre-process-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,23 @@ describe("Core - preProcess, postProcess, afterEnd", () => {
doc = await makeRSDoc(ops, "spec/core/pre-process-spec.html");
});

it("runs the preProcess and postProces arrays", () => {
expect(doc.getElementById("pre-sync").innerHTML).toBe("pass");
expect(doc.getElementById("pre-async").innerHTML).toBe("pass");
expect(doc.getElementById("post-sync").innerHTML).toBe("pass");
expect(doc.getElementById("post-async").innerHTML).toBe("pass");
expect(doc.getElementById("pre-warning").innerHTML).toBe("pass");
expect(doc.getElementById("post-error").innerHTML).toBe("pass");
it("runs the preProcess and postProcess arrays", () => {
expect(doc.getElementById("pre-sync").innerHTML).toContain("pass");
expect(doc.getElementById("pre-async").innerHTML).toContain("pass");
expect(doc.getElementById("post-sync").innerHTML).toContain("pass");
expect(doc.getElementById("post-async").innerHTML).toContain("pass");
expect(doc.getElementById("pre-warning").innerHTML).toContain("pass");
expect(doc.getElementById("post-error").innerHTML).toContain("pass");
});

it("runs preProcess, postProcess, and afterEnd in order", () => {
expect(doc.getElementById("pre-sync").innerHTML).toBe("pass 1");
expect(doc.getElementById("pre-async").innerHTML).toBe("pass 2");
expect(doc.getElementById("pre-warning").innerHTML).toBe("pass 3");
expect(doc.getElementById("post-sync").innerHTML).toBe("pass 4");
expect(doc.getElementById("post-async").innerHTML).toBe("pass 5");
expect(doc.getElementById("post-error").innerHTML).toBe("pass 6");
expect(doc.getElementById("afterend").innerHTML).toBe("pass 7");
});

it("can show warnings and errors", () => {
Expand Down Expand Up @@ -48,6 +58,6 @@ describe("Core - preProcess, postProcess, afterEnd", () => {
});

it("runs afterEnd method", () => {
expect(doc.getElementById("afterend").innerHTML).toBe("pass");
expect(doc.getElementById("afterend").innerHTML).toContain("pass");
});
});