Skip to content
This repository has been archived by the owner on Aug 4, 2022. It is now read-only.

Commit

Permalink
Bug 1197146: Part 1: Prime dispatcher in listener to resolve promises
Browse files Browse the repository at this point in the history
Because of the asynchronous behaviour of some internal utilities, such
as ElementManager, we need to employ promises to create a bridge over
them and the synchronous returning functions.

r=dburns
  • Loading branch information
andreastt committed Aug 21, 2015
1 parent d0be40b commit 7fc1262
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions testing/marionette/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Cu.import("chrome://marionette/content/elements.js");
Cu.import("chrome://marionette/content/error.js");
Cu.import("resource://gre/modules/FileUtils.jsm");
Cu.import("resource://gre/modules/NetUtil.jsm");
Cu.import("resource://gre/modules/Task.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
let utils = {};
utils.window = content;
Expand Down Expand Up @@ -147,25 +148,34 @@ function emitTouchEventForIFrame(message) {
message.force, 90);
}

// Eventually we will not have a closure for every single command, but
// use a generic dispatch for all listener commands.
//
// Perhaps one could even conceive having a separate instance of
// CommandProcessor for the listener, because the code is mostly the same.
function dispatch(fn) {
return function(msg) {
let id = msg.json.command_id;
try {

let req = Task.spawn(function*() {
let rv;
if (typeof msg.json == "undefined" || msg.json instanceof Array) {
rv = fn.apply(null, msg.json);
return yield fn.apply(null, msg.json);
} else {
rv = fn(msg.json);
return yield fn(msg.json);
}
});

let okOrValueResponse = rv => {
if (typeof rv == "undefined") {
sendOk(id);
} else {
sendResponse({value: rv}, id);
}
} catch (e) {
sendError(e, id);
}
};

req.then(okOrValueResponse, err => sendError(err, id))
.catch(error.report);
};
}

Expand Down

0 comments on commit 7fc1262

Please sign in to comment.