Skip to content

Commit

Permalink
return devtools command error through sendDevToolsMessage Promise
Browse files Browse the repository at this point in the history
this enables code like:
conn.sendDevToolsMessage(...).then().catch();
which is used by lighthouse.

BUG=

Review-Url: https://codereview.chromium.org/2945763002
Cr-Commit-Position: refs/heads/master@{#480767}
  • Loading branch information
jzfeng authored and Commit Bot committed Jun 20, 2017
1 parent 6503c6a commit daf3aef
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions headless/lib/browser/devtools_api/devtools_connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Connection {
/**
* An object containing pending DevTools protocol commands keyed by id.
*
* @private {!Map<number, !Connection.CallbackFunction>}
* @private {!Map<number, !Connection.PendingCommand>}
*/
this.pendingCommands_ = new Map();

Expand All @@ -42,7 +42,7 @@ class Connection {
* An object containing DevTools protocol events we are listening for keyed
* by name.
*
* @private {!Map<string, !Map<number, !Connection.CallbackFunction>>}
* @private {!Map<string, !Map<number, !Connection.EventFunction>>}
*/
this.eventListeners_ = new Map();

Expand All @@ -62,7 +62,7 @@ class Connection {
*
* @param {string} eventName Name of the DevTools protocol event to listen
* for.
* @param {!Connection.CallbackFunction} listener The callback issued when we
* @param {!Connection.EventFunction} listener The callback issued when we
* receive a DevTools protocol event corresponding to the given name.
* @return {number} The id of this event listener.
*/
Expand Down Expand Up @@ -114,7 +114,7 @@ class Connection {
this.transport_.send(
JSON.stringify({'method': method, 'id': id, 'params': params}));
return new Promise((resolve, reject) => {
this.pendingCommands_.set(id, resolve);
this.pendingCommands_.set(id, {resolve: resolve, reject: reject});
});
}

Expand All @@ -132,8 +132,9 @@ class Connection {
if (!this.pendingCommands_.has(message.id))
throw new Error('Unrecognized id:' + jsonMessage);
if (message.hasOwnProperty('error'))
throw new Error('DevTools protocol error: ' + message.error);
this.pendingCommands_.get(message.id)(message.result);
this.pendingCommands_.get(message.id).reject(message.error);
else
this.pendingCommands_.get(message.id).resolve(message.result);
this.pendingCommands_.delete(message.id);
} else {
if (!message.hasOwnProperty('method') ||
Expand Down Expand Up @@ -164,6 +165,14 @@ class Connection {
/**
* @typedef {function(Object): undefined|function(string): undefined}
*/
Connection.CallbackFunction;
Connection.EventFunction;

/**
* @typedef {{
* resolve: function(!Object),
* reject: function(!Object)
* }}
*/
Connection.PendingCommand;

exports = Connection;

0 comments on commit daf3aef

Please sign in to comment.