Skip to content

Commit

Permalink
Move towards the standard promise API
Browse files Browse the repository at this point in the history
Promise is no longer an abstract class and its constructor takes a
resolver function. Deferred is no longer an instanceof Promise, but it
still (temporarily) implements Thenable for API backwards compatibility.
  • Loading branch information
jleyba committed Oct 27, 2014
1 parent 446f654 commit 762a185
Show file tree
Hide file tree
Showing 5 changed files with 371 additions and 440 deletions.
33 changes: 16 additions & 17 deletions javascript/safari-driver/extension/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ safaridriver.extension.commands.getWindowHandles = function(session) {
* screenshot of the focused tab as a base64 encoded PNG.
*/
safaridriver.extension.commands.takeScreenshot = function(session) {
var response = new webdriver.promise.Deferred();
session.getCommandTab().visibleContentsAsDataURL(function(dataUrl) {
response.fulfill(dataUrl.substring('data:image/png;base64,'.length));
return new webdriver.promise.Promise(function(fulfill) {
session.getCommandTab().visibleContentsAsDataURL(function(dataUrl) {
fulfill(dataUrl.substring('data:image/png;base64,'.length));
});
});
return response.promise;
};


Expand All @@ -138,15 +138,14 @@ safaridriver.extension.commands.loadUrl = function(session, command) {
'http://code.google.com/p/selenium/issues/detail?id=3773');
}

var response = new webdriver.promise.Deferred();
var tab = session.getCommandTab();
tab.whenReady(function() {
var expectLoad = tab.loadsNewPage(uri);
safaridriver.extension.commands.sendNavigationCommand_(session, command,
expectLoad).then(response.fulfill, response.reject);
return new webdriver.promise.Promise(function(fulfill, reject) {
var tab = session.getCommandTab();
tab.whenReady(function() {
var expectLoad = tab.loadsNewPage(uri);
safaridriver.extension.commands.sendNavigationCommand_(
session, command, expectLoad).then(fulfill, reject);
});
});

return response.promise;
};


Expand All @@ -158,12 +157,12 @@ safaridriver.extension.commands.loadUrl = function(session, command) {
* the operation has completed.
*/
safaridriver.extension.commands.refresh = function(session, command) {
var response = new webdriver.promise.Deferred();
session.getCommandTab().whenReady(function() {
safaridriver.extension.commands.sendNavigationCommand_(session, command,
true).then(response.fulfill, response.reject);
return new webdriver.promise.Promise(function(fulfill, reject) {
session.getCommandTab().whenReady(function() {
safaridriver.extension.commands.sendNavigationCommand_(
session, command, true).then(fulfill, reject);
});
});
return response.promise;
};


Expand Down
38 changes: 18 additions & 20 deletions javascript/safari-driver/inject/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ goog.provide('safaridriver.inject.util');

goog.require('bot.response');
goog.require('safaridriver.message.LoadModule');
goog.require('webdriver.promise.Deferred');
goog.require('webdriver.promise.Promise');


/**
Expand All @@ -33,25 +33,23 @@ goog.require('webdriver.promise.Deferred');
* the module's source code.
*/
safaridriver.inject.util.loadModule = function(moduleId, target) {
var numAttempts = 0;
var d = new webdriver.promise.Deferred();
attemptLoad();
return d.promise;

function attemptLoad() {
var message = new safaridriver.message.LoadModule(moduleId);
var response = /** @type {bot.response.ResponseObject} */ (
message.sendSync(target));
try {
var src = bot.response.checkResponse(response)['value'];
d.fulfill(src);
} catch (ex) {
numAttempts += 1;
if (numAttempts == 3) {
d.reject(ex);
} else {
setTimeout(attemptLoad, 150);
return new webdriver.promise.Promise(function(fulfill, reject) {
var numAttempts = 0;
attemptLoad();
function attemptLoad() {
var message = new safaridriver.message.LoadModule(moduleId);
var response = /** @type {bot.response.ResponseObject} */ (
message.sendSync(target));
try {
fulfill(bot.response.checkResponse(response)['value']);
} catch (ex) {
numAttempts += 1;
if (numAttempts == 3) {
reject(ex);
} else {
setTimeout(attemptLoad, 150);
}
}
}
}
});
};
Loading

0 comments on commit 762a185

Please sign in to comment.