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 {
/**
* @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<string, !Response>} */
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);
}
}

View File

@ -245,8 +245,11 @@ class Page extends EventEmitter {
* @return {!Promise<!Response>}
*/
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<!Response>}
*/
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;
}