Simplify lib/Navigator.js

This commit is contained in:
Andrey Lushnikov 2017-06-29 12:28:05 -07:00
parent 4aa74cc7f8
commit 3dfa688920

View File

@ -14,8 +14,6 @@
* limitations under the License. * limitations under the License.
*/ */
const VALID_WAIT_CONDITIONS = ['load', 'networkidle'];
class Navigator { class Navigator {
/** /**
* @param {!Connection} client * @param {!Connection} client
@ -30,14 +28,14 @@ class Navigator {
this._waitFor = typeof options.waitFor === 'string' ? options.waitFor : 'load'; this._waitFor = typeof options.waitFor === 'string' ? options.waitFor : 'load';
this._inflightRequests = 0; this._inflightRequests = 0;
console.assert(VALID_WAIT_CONDITIONS.includes(this._waitFor)); console.assert(this._waitFor === 'load' || this._waitFor === 'networkidle', 'Unknown value for options.waitFor: ' + this._waitFor);
if (this._waitFor === 'networkidle') { if (this._waitFor === 'networkidle') {
client.on('Network.requestWillBeSent', event => this._onRequestWillBeSent(event)); client.on('Network.requestWillBeSent', event => this._onLoadingStarted(event));
client.on('Network.loadingFinished', event => this._onLoadingFinished(event)); client.on('Network.loadingFinished', event => this._onLoadingCompleted(event));
client.on('Network.loadingFailed', event => this._onLoadingFailed(event)); client.on('Network.loadingFailed', event => this._onLoadingCompleted(event));
client.on('Network.webSocketCreated', event => this._onWebSocketCreated(event)); client.on('Network.webSocketCreated', event => this._onLoadingStarted(event));
client.on('Network.webSocketClosed', event => this._onWebSocketClosed(event)); client.on('Network.webSocketClosed', event => this._onLoadingCompleted(event));
} }
} }
@ -53,16 +51,10 @@ class Navigator {
let navigationComplete; let navigationComplete;
let navigationFailure = new Promise(fulfill => this._client.once('Security.certificateError', fulfill)).then(() => false); let navigationFailure = new Promise(fulfill => this._client.once('Security.certificateError', fulfill)).then(() => false);
switch (this._waitFor) { if (this._waitFor === 'load')
case 'load':
navigationComplete = new Promise(fulfill => this._client.once('Page.loadEventFired', fulfill)); navigationComplete = new Promise(fulfill => this._client.once('Page.loadEventFired', fulfill));
break; else
case 'networkidle':
navigationComplete = new Promise(fulfill => this._navigationLoadCallback = fulfill); navigationComplete = new Promise(fulfill => this._navigationLoadCallback = fulfill);
break;
default:
throw new Error(`Unrecognized wait condition: ${this._waitFor}`);
}
this._inflightRequests = 0; this._inflightRequests = 0;
@ -85,41 +77,6 @@ class Navigator {
}); });
} }
/**
* @param {!Object} event
*/
_onRequestWillBeSent(event) {
this._onLoadingStarted(event);
}
/**
* @param {!Object} event
*/
_onWebSocketCreated(event) {
this._onLoadingStarted(event);
}
/**
* @param {!Object} event
*/
_onWebSocketClosed(event) {
this._onLoadingCompleted(event);
}
/**
* @param {!Object} event
*/
_onLoadingFinished(event) {
this._onLoadingCompleted(event);
}
/**
* @param {!Object} event
*/
_onLoadingFailed(event) {
this._onLoadingCompleted(event);
}
/** /**
* @param {!Object} event * @param {!Object} event
*/ */