Skip to content

Commit

Permalink
Fix up JS types
Browse files Browse the repository at this point in the history
  • Loading branch information
jleyba committed Oct 28, 2014
1 parent 25d1823 commit 9a554af
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 30 deletions.
4 changes: 2 additions & 2 deletions javascript/safari-driver/extension/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ safaridriver.extension.Server.prototype.execute = function(
if (!handler) {
this.logMessage_('Unknown command: ' + command.getName(),
goog.log.Logger.Level.SEVERE);
return webdriver.promise.rejected(bot.response.createErrorResponse(
Error('Unknown command: ' + command.getName())));
return webdriver.promise.rejected(
Error('Unknown command: ' + command.getName()));
}

var description = this.session_.getId() + '::' + command.getName();
Expand Down
31 changes: 14 additions & 17 deletions javascript/safari-driver/inject/page/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,29 +248,26 @@ safaridriver.inject.page.onCommand_ = function(message, e) {
}

var command = message.getCommand();

var response = new webdriver.promise.Deferred();
// When the response is resolved, we want to wrap it up in a message and
// send it back to the injected script. This does all that.
response.then(function(value) {
var encodedValue = safaridriver.inject.page.encoder.encode(value);
// If the command result contains any DOM elements from another
// document, the encoded value will contain promises that will resolve
// once the owner documents have encoded the elements. Therefore, we
// must wait for those to resolve.
return webdriver.promise.fullyResolved(encodedValue);
}).then(bot.response.createResponse, bot.response.createErrorResponse).
then(function(response) {
safaridriver.inject.CommandRegistry.getInstance()
.execute(command, goog.global)
// When the response is resolved, we want to wrap it up in a message and
// send it back to the injected script. This does all that.
.then(function(value) {
var encodedValue = safaridriver.inject.page.encoder.encode(value);
// If the command result contains any DOM elements from another
// document, the encoded value will contain promises that will resolve
// once the owner documents have encoded the elements. Therefore, we
// must wait for those to resolve.
return webdriver.promise.fullyResolved(encodedValue);
})
.then(bot.response.createResponse, bot.response.createErrorResponse)
.then(function(response) {
var responseMessage = new safaridriver.message.Response(
command.getId(), response);
goog.log.fine(safaridriver.inject.page.LOG_,
'Sending ' + command.getName() + ' response: ' + responseMessage);
responseMessage.send(window);
});

safaridriver.inject.CommandRegistry.getInstance()
.execute(command, goog.global)
.then(response.fulfill, response.reject);
};


Expand Down
4 changes: 2 additions & 2 deletions javascript/safari-driver/inject/tab.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ safaridriver.inject.Tab.prototype.installPageScript_ = function(opt_dom) {
var docEl = dom.getDocument().documentElement;
goog.dom.appendChild(docEl, script);

this.installedPageScript_.thenFinally(function() {
this.installedPageScript_.promise.thenFinally(function() {
goog.dom.removeNode(script);
});
}, this));
Expand Down Expand Up @@ -622,7 +622,7 @@ safaridriver.inject.Tab.prototype.executeInPage = function(command) {

message.send(window);

return commandResponse.then(function(result) {
return commandResponse.promise.then(function(result) {
return bot.inject.wrapValue(result);
});
}, this));
Expand Down
34 changes: 25 additions & 9 deletions javascript/webdriver/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ goog.inherits(webdriver.promise.CancellationError, goog.debug.Error);
* used to schedule callbacks on a promised value.
*
* @interface
* @extends {IThenable.<T>}
* @template T
*/
webdriver.promise.Thenable = function() {};
Expand All @@ -108,7 +109,7 @@ webdriver.promise.Thenable.prototype.isPending = function() {};
* @param {?(function(T): (R|webdriver.promise.Promise.<R>))=} opt_callback The
* function to call if this promise is successfully resolved. The function
* should expect a single argument: the promise's resolved value.
* @param {?(function(Error): (R|webdriver.promise.Promise.<R>))=} opt_errback
* @param {?(function(*): (R|webdriver.promise.Promise.<R>))=} opt_errback
* The function to call if this promise is rejected. The function should
* expect a single argument: the rejection reason.
* @return {!webdriver.promise.Promise.<R>} A new promise which will be
Expand Down Expand Up @@ -136,7 +137,7 @@ webdriver.promise.Thenable.prototype.then = function(
* });
* </code></pre>
*
* @param {function(Error): (R|webdriver.promise.Promise.<R>)} errback The
* @param {function(*): (R|webdriver.promise.Promise.<R>)} errback The
* function to call if this promise is rejected. The function should
* expect a single argument: the rejection reason.
* @return {!webdriver.promise.Promise.<R>} A new promise which will be
Expand Down Expand Up @@ -249,6 +250,12 @@ webdriver.promise.Thenable.isImplementation = function(object) {
* fulfilled or rejected state, at which point the promise is considered
* resolved.
*
* @param {function(
* function((T|IThenable.<T>|Thenable)=),
* function(*=))} resolver
* Function that is invoked immediately to begin computation of this
* promise's value. The function should accept a pair of callback functions,
* one for fulfilling the promise and another for rejecting it.
* @param {webdriver.promise.ControlFlow=} opt_flow The control flow
* this instance was created under. Defaults to the currently active flow.
* @constructor
Expand Down Expand Up @@ -531,14 +538,22 @@ webdriver.promise.Deferred = function(opt_flow) {
}
};

this.fulfill = function(value) {
checkNotSelf(value);
fulfill(value);
/**
* Resolves this deferred with the given value.
* @param {(T|IThenable.<T>|Thenable)=} opt_value The fulfilled value.
*/
this.fulfill = function(opt_value) {
checkNotSelf(opt_value);
fulfill(opt_value);
};

this.reject = function(reason) {
checkNotSelf(reason);
reject(reason);
/**
* Rejects this promise with the given reason.
* @param {*=} opt_reason The rejection reason.
*/
this.reject = function(opt_reason) {
checkNotSelf(opt_reason);
reject(opt_reason);
};
};
webdriver.promise.Thenable.addImplementation(webdriver.promise.Deferred);
Expand Down Expand Up @@ -1587,10 +1602,11 @@ webdriver.promise.ControlFlow.prototype.abortFrame_ = function(error) {
* returned promise will be rejected.
*
* @param {!Function} fn The function to execute.
* @param {function(*)} callback The function to call with a successful result.
* @param {function(T)} callback The function to call with a successful result.
* @param {function(*)} errback The function to call if there is an error.
* @param {boolean=} opt_activate Whether the active frame should be updated to
* the newly created frame so tasks are treated as sub-tasks.
* @template T
* @private
*/
webdriver.promise.ControlFlow.prototype.runInNewFrame_ = function(
Expand Down

0 comments on commit 9a554af

Please sign in to comment.