From 3dfa688920a6196920d1c920ec30de52ec4a1a8e Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Thu, 29 Jun 2017 12:28:05 -0700 Subject: [PATCH] Simplify lib/Navigator.js --- lib/Navigator.js | 63 ++++++++---------------------------------------- 1 file changed, 10 insertions(+), 53 deletions(-) diff --git a/lib/Navigator.js b/lib/Navigator.js index f09fafea8e7..587af1011b7 100644 --- a/lib/Navigator.js +++ b/lib/Navigator.js @@ -14,8 +14,6 @@ * limitations under the License. */ -const VALID_WAIT_CONDITIONS = ['load', 'networkidle']; - class Navigator { /** * @param {!Connection} client @@ -30,14 +28,14 @@ class Navigator { this._waitFor = typeof options.waitFor === 'string' ? options.waitFor : 'load'; 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') { - client.on('Network.requestWillBeSent', event => this._onRequestWillBeSent(event)); - client.on('Network.loadingFinished', event => this._onLoadingFinished(event)); - client.on('Network.loadingFailed', event => this._onLoadingFailed(event)); - client.on('Network.webSocketCreated', event => this._onWebSocketCreated(event)); - client.on('Network.webSocketClosed', event => this._onWebSocketClosed(event)); + client.on('Network.requestWillBeSent', event => this._onLoadingStarted(event)); + client.on('Network.loadingFinished', event => this._onLoadingCompleted(event)); + client.on('Network.loadingFailed', event => this._onLoadingCompleted(event)); + client.on('Network.webSocketCreated', event => this._onLoadingStarted(event)); + client.on('Network.webSocketClosed', event => this._onLoadingCompleted(event)); } } @@ -53,16 +51,10 @@ class Navigator { let navigationComplete; let navigationFailure = new Promise(fulfill => this._client.once('Security.certificateError', fulfill)).then(() => false); - switch (this._waitFor) { - case 'load': - navigationComplete = new Promise(fulfill => this._client.once('Page.loadEventFired', fulfill)); - break; - case 'networkidle': - navigationComplete = new Promise(fulfill => this._navigationLoadCallback = fulfill); - break; - default: - throw new Error(`Unrecognized wait condition: ${this._waitFor}`); - } + if (this._waitFor === 'load') + navigationComplete = new Promise(fulfill => this._client.once('Page.loadEventFired', fulfill)); + else + navigationComplete = new Promise(fulfill => this._navigationLoadCallback = fulfill); 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 */