Skip to content

Commit

Permalink
feat: allow async functions for pre/postProc/afterAll (closes #1034) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcos Cáceres committed Jan 16, 2017
1 parent f71f3a3 commit 46268dd
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 11 deletions.
13 changes: 8 additions & 5 deletions src/core/base-runner.js
Expand Up @@ -2,8 +2,8 @@
// The module in charge of running the whole processing pipeline.
import { pub } from "core/pubsubhub";
import "core/default-root-attr";
import "core/pre-process";
import "core/post-process"
import { done as preProcessDone } from "core/pre-process";
import { done as postProcessDone } from "core/post-process";
import "core/respec-ready";
import "core/override-configuration";
import "core/include-config";
Expand All @@ -21,16 +21,19 @@ function toRunnable(plug) {
}

export async function runAll(plugs) {
pub("start-all", window.respecConfig);
pub("start-all", respecConfig);
await preProcessDone;
const runnables = plugs
.filter(plug => plug && typeof plug.run === "function" && plug !== this)
.map(toRunnable);
for (const task of runnables) {
try {
await task(window.respecConfig);
await task(respecConfig);
} catch (err) {
console.error(err);
}
}
pub("end-all", window.respecConfig);
pub("plugins-done", respecConfig);
await postProcessDone;
pub("end-all", respecConfig);
};
16 changes: 13 additions & 3 deletions src/core/post-process.js
Expand Up @@ -10,11 +10,21 @@
*/
import { sub } from "core/pubsubhub";

sub("end-all", config => {
let doneResolver;
export const done = new Promise(resolve => doneResolver = resolve);

sub("plugins-done", async config => {
const result = [];
if (Array.isArray(config.postProcess)) {
config.postProcess.forEach(f => f(config));
const values = await Promise.all(
config.postProcess
.filter(f => typeof f === "function")
.map(f => Promise.resolve(f(config, document)))
);
result.push(...values);
}
if (typeof config.afterEnd === "function") {
config.afterEnd();
result.push(await Promise.resolve(config.afterEnd(config, document)));
}
doneResolver(result);
}, { once: true });
14 changes: 12 additions & 2 deletions src/core/pre-process.js
Expand Up @@ -9,8 +9,18 @@
*/
import { sub } from "core/pubsubhub";

sub("start-all", config => {
let doneResolver;
export const done = new Promise(resolve => doneResolver = resolve);

sub("start-all", async config => {
const result = [];
if (Array.isArray(config.preProcess)) {
config.preProcess.forEach(f => f(config));
const values = await Promise.all(
config.preProcess
.filter(f => typeof f === "function")
.map(f => Promise.resolve(f(config, document)))
);
result.push(...values);
}
doneResolver(result);
}, { once: true });
5 changes: 4 additions & 1 deletion tests/spec/SpecHelper.js
Expand Up @@ -51,7 +51,10 @@ function decorateDocument(doc, opts) {
var path = opts.jsPath || "../js/";
var loader = this.ownerDocument.createElement("script");
var config = this.ownerDocument.createElement("script");
var configText = "var respecConfig = " + JSON.stringify(opts.config || {}) + ";";
var configText = "";
if (opts.config) {
configText = "var respecConfig = " + JSON.stringify(opts.config || {}) + ";";
}
config.classList.add("remove");
config.innerText = configText;
var loadAttr = {
Expand Down
80 changes: 80 additions & 0 deletions tests/spec/core/pre-process-spec.html
@@ -0,0 +1,80 @@
<!doctype html>
<script src="/js/deps/require.js" data-main="/js/profile-w3c-common">
</script>
<script class='remove'>
function noOp() {}

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

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

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

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

function afterEnd() {
return new Promise(resolve => {
setTimeout(() => {
document.querySelector("#afterend").innerHTML = "pass";
resolve();
}, 4);
});
}

const respecConfig = {
specStatus: "unofficial",
shortName: "i",
editors: [{
name: "Foo",
url: "https://foo.com/"
}],
};

respecConfig.preProcess = [
preSyncFunc,
preAsyncFunc,
noOp, {},
];

respecConfig.postProcess = [
postSyncFunc,
postAsyncFunc,
noOp, {},
];
respecConfig.afterEnd = afterEnd;
</script>
<section id='abstract'>
<p>
abstract.
</p>
</section>
<section id='sotd'>
<p>
CUSTOM PARAGRAPH
</p>
</section>
<section>
<p id="pre-sync">FAIL</p>
<p id="pre-async">FAIL</p>
<p id="post-sync">FAIL</p>
<p id="post-async">FAIL</p>
<p id="afterend">FAIL</p>
</section>
29 changes: 29 additions & 0 deletions tests/spec/core/pre-process-spec.js
@@ -0,0 +1,29 @@
"use strict";
describe("Core - preProcess, postProcess, afterEnd", () => {
afterAll(done => {
flushIframes();
done();
});
let doc;

beforeAll(done => {
const ops = makeStandardOps();
ops.config = null; // use src doc's config
const cb = iframe => {
doc = iframe;
done();
};
makeRSDoc(ops, cb, "spec/core/pre-process-spec.html");
});

it("runs the preProcess and postProces arrays", () => {
expect(doc.querySelector("#pre-sync").innerHTML).toEqual("pass");
expect(doc.querySelector("#pre-async").innerHTML).toEqual("pass");
expect(doc.querySelector("#post-sync").innerHTML).toEqual("pass");
expect(doc.querySelector("#post-async").innerHTML).toEqual("pass");
});

it("runs afterEnd method", () => {
expect(doc.querySelector("#afterend").innerHTML).toEqual("pass");
});
});
1 change: 1 addition & 0 deletions tests/testFiles.json
Expand Up @@ -19,6 +19,7 @@
"spec/core/jquery-enhanced-spec.js",
"spec/core/markdown-spec.js",
"spec/core/override-configuration-spec.js",
"spec/core/pre-process-spec.js",
"spec/core/ready-promise-spec.js",
"spec/core/remove-respec-spec.js",
"spec/core/requirements-spec.js",
Expand Down

0 comments on commit 46268dd

Please sign in to comment.