Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use promises to yield between phases #435

Merged
merged 3 commits into from May 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions js/Promise.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 36 additions & 30 deletions js/core/base-runner.js
Expand Up @@ -79,7 +79,7 @@ if (window.console) {


define(
["jquery"],
["jquery", "Promise.min"],
function () {
return {
runAll: function (plugs) {
Expand All @@ -98,43 +98,49 @@ define(
});
respecEvents.pub("start", "core/base-runner");

if (respecConfig.preProcess) {
for (var i = 0; i < respecConfig.preProcess.length; i++) {
try { respecConfig.preProcess[i].apply(this); }
catch (e) { respecEvents.pub("error", e); }
}
}

var pipeline = Promise.resolve();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are sure all users of ReSpec are using Promise capable browsers, right? I have not idea if Internet Explorer latest stable supports them... if yes, let's merge this. We can always ask on the mailing list.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(or does "Promise.min" include some kind of pollyfill?)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NM, I see the commit message answered my question :)

// the first in the plugs is going to be us
plugs.shift();

var pipeline;
pipeline = function () {
if (!plugs.length) {
if (respecConfig.postProcess) {
for (var i = 0; i < respecConfig.postProcess.length; i++) {
try { respecConfig.postProcess[i].apply(this); }
catch (e) { respecEvents.pub("error", e); }
}
plugs.forEach(function(plug) {
pipeline = pipeline.then(function () {
if (plug.run) {
return new Promise(function runPlugin(resolve, reject) {
var result = plug.run.call(plug, respecConfig, document, resolve, respecEvents);
// If the plugin returns a promise, have that
// control the end of the plugin's run.
// Otherwise, assume it'll call resolve() as a
// completion callback.
if (result) {
resolve(result);
}
}).catch(function(e) {
respecEvents.pub("error", e);
respecEvents.pub("end", "unknown/with-error");
});
}
if (respecConfig.afterEnd) {
try { respecConfig.afterEnd.apply(window, Array.prototype.slice.call(arguments)); }
else return Promise.resolve();
});
});
return pipeline.then(function() {
if (respecConfig.postProcess) {
for (var i = 0; i < respecConfig.postProcess.length; i++) {
try { respecConfig.postProcess[i].apply(this); }
catch (e) { respecEvents.pub("error", e); }
}
respecEvents.pub("end", "core/base-runner");
return;
}
var plug = plugs.shift();
if (plug.run) {
try { plug.run.call(plug, respecConfig, document, pipeline, respecEvents); }
catch (e) {
respecEvents.pub("error", e);
respecEvents.pub("end", "unknown/with-error");
pipeline();
}
}
else pipeline();
};
if (respecConfig.preProcess) {
for (var i = 0; i < respecConfig.preProcess.length; i++) {
try { respecConfig.preProcess[i].apply(this); }
if (respecConfig.afterEnd) {
try { respecConfig.afterEnd.apply(window, Array.prototype.slice.call(arguments)); }
catch (e) { respecEvents.pub("error", e); }
}
}
pipeline();
respecEvents.pub("end", "core/base-runner");
});
}
};
}
Expand Down