Remove NetworkManager usage from NavigatorWatcher (#134)

This commit is contained in:
Pavel Feldman 2017-07-25 16:06:53 -07:00 committed by Andrey Lushnikov
parent 64968862db
commit f455e3a1e4
2 changed files with 14 additions and 19 deletions

View File

@ -20,12 +20,10 @@ const helper = require('./helper');
class NavigatorWatcher { class NavigatorWatcher {
/** /**
* @param {!Connection} client * @param {!Connection} client
* @param {!NetworkManager} networkManager
* @param {!Object=} options * @param {!Object=} options
*/ */
constructor(client, networkManager, options = {}) { constructor(client, options = {}) {
this._client = client; this._client = client;
this._networkManager = networkManager;
this._timeout = typeof options['timeout'] === 'number' ? options['timeout'] : 30000; this._timeout = typeof options['timeout'] === 'number' ? options['timeout'] : 30000;
this._idleTime = typeof options['networkIdleTimeout'] === 'number' ? options['networkIdleTimeout'] : 1000; this._idleTime = typeof options['networkIdleTimeout'] === 'number' ? options['networkIdleTimeout'] : 1000;
this._idleInflight = typeof options['networkIdleInflight'] === 'number' ? options['networkIdleInflight'] : 2; this._idleInflight = typeof options['networkIdleInflight'] === 'number' ? options['networkIdleInflight'] : 2;
@ -40,8 +38,6 @@ class NavigatorWatcher {
async waitForNavigation() { async waitForNavigation() {
this._inflightRequests = 0; this._inflightRequests = 0;
this._requestIds = new Set(); this._requestIds = new Set();
/** @type {!Map<string, !Response>} */
this._responses = new Map();
this._eventListeners = [ this._eventListeners = [
helper.addEventListener(this._client, 'Network.requestWillBeSent', this._onLoadingStarted.bind(this)), 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.loadingFailed', this._onLoadingCompleted.bind(this)),
helper.addEventListener(this._client, 'Network.webSocketCreated', this._onLoadingStarted.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._client, 'Network.webSocketClosed', this._onLoadingCompleted.bind(this)),
helper.addEventListener(this._networkManager, NetworkManager.Events.Response, this._onResponse.bind(this)),
]; ];
let certificateError = new Promise(fulfill => { let certificateError = new Promise(fulfill => {
@ -69,7 +64,6 @@ class NavigatorWatcher {
const error = await Promise.race([certificateError, watchdog, this._waitUntil === 'load' ? loadEventFired : networkIdle]); const error = await Promise.race([certificateError, watchdog, this._waitUntil === 'load' ? loadEventFired : networkIdle]);
if (error) if (error)
throw new Error(error); throw new Error(error);
return this._responses;
} finally { } finally {
this._cleanup(); this._cleanup();
} }
@ -112,14 +106,6 @@ class NavigatorWatcher {
clearTimeout(this._idleTimer); clearTimeout(this._idleTimer);
clearTimeout(this._maximumTimer); clearTimeout(this._maximumTimer);
this._responses = new Map();
}
/**
* @param {!Response} response
*/
_onResponse(response) {
this._responses.set(response.url, response);
} }
} }

View File

@ -245,8 +245,11 @@ class Page extends EventEmitter {
* @return {!Promise<!Response>} * @return {!Promise<!Response>}
*/ */
async navigate(url, options) { 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 result = watcher.waitForNavigation();
const referrer = this._networkManager.httpHeaders()['referer']; const referrer = this._networkManager.httpHeaders()['referer'];
try { try {
// Await for the command to throw exception in case of illegal arguments. // Await for the command to throw exception in case of illegal arguments.
@ -255,7 +258,8 @@ class Page extends EventEmitter {
watcher.cancel(); watcher.cancel();
throw e; throw e;
} }
const responses = await result; await result;
helper.removeEventListeners([listener]);
return responses.get(this.mainFrame().url()); return responses.get(this.mainFrame().url());
} }
@ -273,8 +277,13 @@ class Page extends EventEmitter {
* @return {!Promise<!Response>} * @return {!Promise<!Response>}
*/ */
async waitForNavigation(options) { async waitForNavigation(options) {
const watcher = new NavigatorWatcher(this._client, this._networkManager, options); const watcher = new NavigatorWatcher(this._client, options);
const responses = await watcher.waitForNavigation();
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; return responses.get(this.mainFrame().url()) || null;
} }