Skip to content

Commit 7194756

Browse files
committed
[js] Reduce the API on promise.Thenable for compatibility with native Promises.
#2969
1 parent ce01bba commit 7194756

File tree

14 files changed

+338
-266
lines changed

14 files changed

+338
-266
lines changed

javascript/node/selenium-webdriver/CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
### API Changes
1717

1818
* Removed `safari.Options#useLegacyDriver()`
19+
* Reduced the API on `promise.Thenable` for compatibility with native promises:
20+
- Removed `#isPending()`
21+
- Removed `#cancel()`
22+
- Removed `#finally()`
1923

2024

2125
## v3.0.0-beta-3

javascript/node/selenium-webdriver/edge.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,10 @@ class Driver extends webdriver.WebDriver {
278278
var driver = webdriver.WebDriver.createSession(executor, caps, opt_flow);
279279
super(driver.getSession(), executor, driver.controlFlow());
280280

281-
var boundQuit = this.quit.bind(this);
282-
283281
/** @override */
284-
this.quit = function() {
285-
return boundQuit().finally(service.kill.bind(service));
282+
this.quit = () => {
283+
return /** @type {!promise.Thenable} */(
284+
promise.finally(super.quit(), service.kill.bind(service)));
286285
};
287286
}
288287

javascript/node/selenium-webdriver/firefox/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,8 @@ class Driver extends webdriver.WebDriver {
622622

623623
/** @override */
624624
this.quit = () => {
625-
return super.quit().finally(spec.onQuit);
625+
return /** @type {!promise.Thenable} */(
626+
promise.finally(super.quit(), spec.onQuit));
626627
};
627628
}
628629

javascript/node/selenium-webdriver/http/util.js

Lines changed: 95 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -61,38 +61,54 @@ exports.getStatus = getStatus;
6161
* Waits for a WebDriver server to be healthy and accepting requests.
6262
* @param {string} url Base URL of the server to query.
6363
* @param {number} timeout How long to wait for the server.
64-
* @return {!promise.Promise} A promise that will resolve when the
65-
* server is ready.
64+
* @param {Promise=} opt_cancelToken A promise used as a cancellation signal:
65+
* if resolved before the server is ready, the wait will be terminated
66+
* early with a {@link promise.CancellationError}.
67+
* @return {!Promise} A promise that will resolve when the server is ready, or
68+
* if the wait is cancelled.
6669
*/
67-
exports.waitForServer = function(url, timeout) {
68-
var ready = promise.defer(),
69-
start = Date.now();
70-
checkServerStatus();
71-
return ready.promise;
72-
73-
function checkServerStatus() {
74-
return getStatus(url).then(status => ready.fulfill(status), onError);
75-
}
76-
77-
function onError(e) {
78-
// Some servers don't support the status command. If they are able to
79-
// response with an error, then can consider the server ready.
80-
if (e instanceof error.UnsupportedOperationError) {
81-
ready.fulfill();
82-
return;
70+
exports.waitForServer = function(url, timeout, opt_cancelToken) {
71+
return new Promise((onResolve, onReject) => {
72+
let start = Date.now();
73+
74+
let done = false;
75+
let resolve = (status) => {
76+
done = true;
77+
onResolve(status);
78+
};
79+
let reject = (err) => {
80+
done = true;
81+
onReject(err);
82+
};
83+
84+
if (opt_cancelToken) {
85+
opt_cancelToken.then(_ => reject(new promise.CancellationError));
8386
}
8487

85-
if (Date.now() - start > timeout) {
86-
ready.reject(
87-
Error('Timed out waiting for the WebDriver server at ' + url));
88-
} else {
89-
setTimeout(function() {
90-
if (ready.promise.isPending()) {
91-
checkServerStatus();
92-
}
93-
}, 50);
88+
checkServerStatus();
89+
function checkServerStatus() {
90+
return getStatus(url).then(status => resolve(status), onError);
9491
}
95-
}
92+
93+
function onError(e) {
94+
// Some servers don't support the status command. If they are able to
95+
// response with an error, then can consider the server ready.
96+
if (e instanceof error.UnsupportedOperationError) {
97+
resolve({});
98+
return;
99+
}
100+
101+
if (Date.now() - start > timeout) {
102+
reject(Error('Timed out waiting for the WebDriver server at ' + url));
103+
} else {
104+
setTimeout(function() {
105+
if (!done) {
106+
checkServerStatus();
107+
}
108+
}, 50);
109+
}
110+
}
111+
});
96112
};
97113

98114

@@ -101,39 +117,59 @@ exports.waitForServer = function(url, timeout) {
101117
* timeout expires.
102118
* @param {string} url The URL to poll.
103119
* @param {number} timeout How long to wait, in milliseconds.
104-
* @return {!promise.Promise} A promise that will resolve when the
105-
* URL responds with 2xx.
120+
* @param {Promise=} opt_cancelToken A promise used as a cancellation signal:
121+
* if resolved before the a 2xx response is received, the wait will be
122+
* terminated early with a {@link promise.CancellationError}.
123+
* @return {!Promise} A promise that will resolve when a 2xx is received from
124+
* the given URL, or if the wait is cancelled.
106125
*/
107-
exports.waitForUrl = function(url, timeout) {
108-
var client = new HttpClient(url),
109-
request = new HttpRequest('GET', ''),
110-
ready = promise.defer(),
111-
start = Date.now();
112-
testUrl();
113-
return ready.promise;
114-
115-
function testUrl() {
116-
client.send(request).then(onResponse, onError);
117-
}
118-
119-
function onError() {
120-
if (Date.now() - start > timeout) {
121-
ready.reject(Error(
122-
'Timed out waiting for the URL to return 2xx: ' + url));
123-
} else {
124-
setTimeout(function() {
125-
if (ready.promise.isPending()) {
126-
testUrl();
127-
}
128-
}, 50);
126+
exports.waitForUrl = function(url, timeout, opt_cancelToken) {
127+
return new Promise((onResolve, onReject) => {
128+
let client = new HttpClient(url);
129+
let request = new HttpRequest('GET', '');
130+
let start = Date.now();
131+
132+
let done = false;
133+
let resolve = () => {
134+
done = true;
135+
onResolve();
136+
};
137+
let reject = (err) => {
138+
done = true;
139+
onReject(err);
140+
};
141+
142+
if (opt_cancelToken) {
143+
opt_cancelToken.then(_ => reject(new promise.CancellationError));
144+
}
145+
146+
testUrl();
147+
148+
function testUrl() {
149+
client.send(request).then(onResponse, onError);
150+
}
151+
152+
function onError() {
153+
if (Date.now() - start > timeout) {
154+
reject(Error('Timed out waiting for the URL to return 2xx: ' + url));
155+
} else {
156+
setTimeout(function() {
157+
if (!done) {
158+
testUrl();
159+
}
160+
}, 50);
161+
}
129162
}
130-
}
131163

132-
function onResponse(response) {
133-
if (!ready.promise.isPending()) return;
134-
if (response.status > 199 && response.status < 300) {
135-
return ready.fulfill();
164+
function onResponse(response) {
165+
if (done) {
166+
return;
167+
}
168+
if (response.status > 199 && response.status < 300) {
169+
resolve();
170+
return;
171+
}
172+
onError();
136173
}
137-
onError();
138-
}
174+
});
139175
};

javascript/node/selenium-webdriver/ie.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -420,11 +420,10 @@ class Driver extends webdriver.WebDriver {
420420

421421
super(driver.getSession(), executor, driver.controlFlow());
422422

423-
let boundQuit = this.quit.bind(this);
424-
425423
/** @override */
426-
this.quit = function() {
427-
return boundQuit().finally(service.kill.bind(service));
424+
this.quit = () => {
425+
return /** @type {!promise.Thenable} */(
426+
promise.finally(super.quit(), service.kill.bind(service)));
428427
};
429428
}
430429

0 commit comments

Comments
 (0)