From f455e3a1e4983f719642ec8208d4032d5ec00f51 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Tue, 25 Jul 2017 16:06:53 -0700 Subject: [PATCH] Remove NetworkManager usage from NavigatorWatcher (#134) --- lib/NavigatorWatcher.js | 16 +--------------- lib/Page.js | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/lib/NavigatorWatcher.js b/lib/NavigatorWatcher.js index 906ce840438..a8b4e4a2030 100644 --- a/lib/NavigatorWatcher.js +++ b/lib/NavigatorWatcher.js @@ -20,12 +20,10 @@ const helper = require('./helper'); class NavigatorWatcher { /** * @param {!Connection} client - * @param {!NetworkManager} networkManager * @param {!Object=} options */ - constructor(client, networkManager, options = {}) { + constructor(client, options = {}) { this._client = client; - this._networkManager = networkManager; this._timeout = typeof options['timeout'] === 'number' ? options['timeout'] : 30000; this._idleTime = typeof options['networkIdleTimeout'] === 'number' ? options['networkIdleTimeout'] : 1000; this._idleInflight = typeof options['networkIdleInflight'] === 'number' ? options['networkIdleInflight'] : 2; @@ -40,8 +38,6 @@ class NavigatorWatcher { async waitForNavigation() { this._inflightRequests = 0; this._requestIds = new Set(); - /** @type {!Map} */ - this._responses = new Map(); this._eventListeners = [ helper.addEventListener(this._client, 'Network.requestWillBeSent', this._onLoadingStarted.bind(this)), @@ -49,7 +45,6 @@ class NavigatorWatcher { helper.addEventListener(this._client, 'Network.loadingFailed', this._onLoadingCompleted.bind(this)), helper.addEventListener(this._client, 'Network.webSocketCreated', this._onLoadingStarted.bind(this)), helper.addEventListener(this._client, 'Network.webSocketClosed', this._onLoadingCompleted.bind(this)), - helper.addEventListener(this._networkManager, NetworkManager.Events.Response, this._onResponse.bind(this)), ]; let certificateError = new Promise(fulfill => { @@ -69,7 +64,6 @@ class NavigatorWatcher { const error = await Promise.race([certificateError, watchdog, this._waitUntil === 'load' ? loadEventFired : networkIdle]); if (error) throw new Error(error); - return this._responses; } finally { this._cleanup(); } @@ -112,14 +106,6 @@ class NavigatorWatcher { clearTimeout(this._idleTimer); clearTimeout(this._maximumTimer); - this._responses = new Map(); - } - - /** - * @param {!Response} response - */ - _onResponse(response) { - this._responses.set(response.url, response); } } diff --git a/lib/Page.js b/lib/Page.js index 37d1818f237..be9f9e5fa22 100644 --- a/lib/Page.js +++ b/lib/Page.js @@ -245,8 +245,11 @@ class Page extends EventEmitter { * @return {!Promise} */ async navigate(url, options) { - const watcher = new NavigatorWatcher(this._client, this._networkManager, options); + const watcher = new NavigatorWatcher(this._client, options); + const responses = new Map(); + const listener = helper.addEventListener(this._networkManager, NetworkManager.Events.Response, response => responses.set(response.url, response)); const result = watcher.waitForNavigation(); + const referrer = this._networkManager.httpHeaders()['referer']; try { // Await for the command to throw exception in case of illegal arguments. @@ -255,7 +258,8 @@ class Page extends EventEmitter { watcher.cancel(); throw e; } - const responses = await result; + await result; + helper.removeEventListeners([listener]); return responses.get(this.mainFrame().url()); } @@ -273,8 +277,13 @@ class Page extends EventEmitter { * @return {!Promise} */ async waitForNavigation(options) { - const watcher = new NavigatorWatcher(this._client, this._networkManager, options); - const responses = await watcher.waitForNavigation(); + const watcher = new NavigatorWatcher(this._client, options); + + const responses = new Map(); + const listener = helper.addEventListener(this._networkManager, NetworkManager.Events.Response, response => responses.set(response.url, response)); + await watcher.waitForNavigation(); + helper.removeEventListeners([listener]); + return responses.get(this.mainFrame().url()) || null; }