mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
refactor: migrate NavigatorWatcher to lifecycle events (#1141)
This patch: - migrates navigation watcher to use protocol-issued lifecycle events. - removes `networkIdleTimeout` and `networkIdleInflight` options for `page.goto` method - adds a new `networkidle0` value to the waitUntil option of navigation methods References #728. BREAKING CHANGE: As an implication of this new approach, the `networkIdleTimeout` and `networkIdleInflight` options are no longer supported. Interested clients should implement the behavior themselves using the `request` and `response` events.
This commit is contained in:
@@ -19,25 +19,28 @@ const {helper} = require('./helper');
|
||||
class NavigatorWatcher {
|
||||
/**
|
||||
* @param {!Puppeteer.Session} client
|
||||
* @param {string} frameId
|
||||
* @param {boolean} ignoreHTTPSErrors
|
||||
* @param {!Object=} options
|
||||
*/
|
||||
constructor(client, ignoreHTTPSErrors, options = {}) {
|
||||
constructor(client, frameId, ignoreHTTPSErrors, options = {}) {
|
||||
console.assert(options.networkIdleTimeout === undefined, 'ERROR: networkIdleTimeout option is no longer supported.');
|
||||
console.assert(options.networkIdleInflight === undefined, 'ERROR: networkIdleInflight option is no longer supported.');
|
||||
console.assert(options.waitUntil !== 'networkidle', 'ERROR: "networkidle" option is no longer supported. Use "networkidle2" instead');
|
||||
this._client = client;
|
||||
this._frameId = frameId;
|
||||
this._ignoreHTTPSErrors = ignoreHTTPSErrors;
|
||||
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;
|
||||
this._waitUntil = typeof options['waitUntil'] === 'string' ? options['waitUntil'] : 'load';
|
||||
console.assert(this._waitUntil === 'load' || this._waitUntil === 'networkidle', 'Unknown value for options.waitUntil: ' + this._waitUntil);
|
||||
const waitUntil = typeof options['waitUntil'] === 'string' ? options['waitUntil'] : 'load';
|
||||
const isAllowedWaitUntil = waitUntil === 'networkidle0' || waitUntil === 'networkidle2' || waitUntil === 'load';
|
||||
console.assert(isAllowedWaitUntil, 'Unknown value for options.waitUntil: ' + waitUntil);
|
||||
this._pendingEvents = new Set([waitUntil]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {!Promise<?Error>}
|
||||
*/
|
||||
async waitForNavigation() {
|
||||
this._requestIds = new Set();
|
||||
|
||||
this._eventListeners = [];
|
||||
|
||||
const navigationPromises = [];
|
||||
@@ -54,58 +57,43 @@ class NavigatorWatcher {
|
||||
navigationPromises.push(certificateError);
|
||||
}
|
||||
|
||||
if (this._waitUntil === 'load') {
|
||||
const loadEventFired = new Promise(fulfill => {
|
||||
this._eventListeners.push(helper.addEventListener(this._client, 'Page.loadEventFired', fulfill));
|
||||
}).then(() => null);
|
||||
navigationPromises.push(loadEventFired);
|
||||
} else {
|
||||
this._eventListeners.push(...[
|
||||
helper.addEventListener(this._client, 'Network.requestWillBeSent', this._onLoadingStarted.bind(this)),
|
||||
helper.addEventListener(this._client, 'Network.loadingFinished', 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.webSocketClosed', this._onLoadingCompleted.bind(this)),
|
||||
]);
|
||||
const networkIdle = new Promise(fulfill => this._networkIdleCallback = fulfill).then(() => null);
|
||||
navigationPromises.push(networkIdle);
|
||||
}
|
||||
this._eventListeners.push(helper.addEventListener(this._client, 'Page.lifecycleEvent', this._onLifecycleEvent.bind(this)));
|
||||
const pendingEventsFired = new Promise(fulfill => this._pendingEventsCallback = fulfill);
|
||||
navigationPromises.push(pendingEventsFired);
|
||||
|
||||
const error = await Promise.race(navigationPromises);
|
||||
this._cleanup();
|
||||
return error ? new Error(error) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {!{frameId: string, name: string}} event
|
||||
*/
|
||||
_onLifecycleEvent(event) {
|
||||
if (event.frameId !== this._frameId)
|
||||
return;
|
||||
const pptrName = protocolLifecycleToPuppeteer[event.name];
|
||||
if (!pptrName)
|
||||
return;
|
||||
this._pendingEvents.delete(pptrName);
|
||||
if (this._pendingEvents.size === 0)
|
||||
this._pendingEventsCallback();
|
||||
}
|
||||
|
||||
cancel() {
|
||||
this._cleanup();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {!Object} event
|
||||
*/
|
||||
_onLoadingStarted(event) {
|
||||
this._requestIds.add(event.requestId);
|
||||
if (this._requestIds.size > this._idleInflight) {
|
||||
clearTimeout(this._idleTimer);
|
||||
this._idleTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {!Object} event
|
||||
*/
|
||||
_onLoadingCompleted(event) {
|
||||
this._requestIds.delete(event.requestId);
|
||||
if (this._requestIds.size <= this._idleInflight && !this._idleTimer)
|
||||
this._idleTimer = setTimeout(this._networkIdleCallback, this._idleTime);
|
||||
}
|
||||
|
||||
_cleanup() {
|
||||
helper.removeEventListeners(this._eventListeners);
|
||||
|
||||
clearTimeout(this._idleTimer);
|
||||
clearTimeout(this._maximumTimer);
|
||||
}
|
||||
}
|
||||
|
||||
const protocolLifecycleToPuppeteer = {
|
||||
'load': 'load',
|
||||
'networkIdle': 'networkidle0',
|
||||
'networkAlmostIdle': 'networkidle2'
|
||||
};
|
||||
|
||||
module.exports = NavigatorWatcher;
|
||||
|
||||
@@ -452,7 +452,8 @@ class Page extends EventEmitter {
|
||||
* @return {!Promise<?Response>}
|
||||
*/
|
||||
async goto(url, options) {
|
||||
const watcher = new NavigatorWatcher(this._client, this._ignoreHTTPSErrors, options);
|
||||
const mainFrame = this._frameManager.mainFrame();
|
||||
const watcher = new NavigatorWatcher(this._client, mainFrame._id, this._ignoreHTTPSErrors, options);
|
||||
const responses = new Map();
|
||||
const listener = helper.addEventListener(this._networkManager, NetworkManager.Events.Response, response => responses.set(response.url, response));
|
||||
const navigationPromise = watcher.waitForNavigation();
|
||||
@@ -492,7 +493,8 @@ class Page extends EventEmitter {
|
||||
* @return {!Promise<!Response>}
|
||||
*/
|
||||
async waitForNavigation(options) {
|
||||
const watcher = new NavigatorWatcher(this._client, this._ignoreHTTPSErrors, options);
|
||||
const mainFrame = this._frameManager.mainFrame();
|
||||
const watcher = new NavigatorWatcher(this._client, mainFrame._id, this._ignoreHTTPSErrors, options);
|
||||
|
||||
const responses = new Map();
|
||||
const listener = helper.addEventListener(this._networkManager, NetworkManager.Events.Response, response => responses.set(response.url, response));
|
||||
|
||||
Reference in New Issue
Block a user