Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: cleanup unused waitFor properties #2716

Merged
merged 1 commit into from
Aug 6, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 11 additions & 28 deletions lighthouse-core/gather/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,11 +369,10 @@ class Driver {
* Returns a promise that resolves when the network has been idle (after DCL) for
* `networkQuietThresholdMs` ms and a method to cancel internal network listeners/timeout.
* @param {number} networkQuietThresholdMs
* @param {number} pauseAfterNetworkQuietMs
* @return {{promise: !Promise, cancel: function()}}
* @private
*/
_waitForNetworkIdle(networkQuietThresholdMs, pauseAfterNetworkQuietMs) {
_waitForNetworkIdle(networkQuietThresholdMs) {
let idleTimeout;
let cancel;

Expand Down Expand Up @@ -407,9 +406,6 @@ class Driver {
this._networkStatusMonitor.removeListener('network-2-busy', onBusy);
this._networkStatusMonitor.removeListener('network-2-idle', onIdle);
};
}).then(() => {
// Once idle has been determined wait another pauseAfterLoadMs
return new Promise(resolve => setTimeout(resolve, pauseAfterNetworkQuietMs));
});

return {
Expand All @@ -419,36 +415,23 @@ class Driver {
}

/**
* Installs a PerformanceObserver in the page to monitor for longtasks and resolves when there have
* been no long tasks for at least waitForCPUQuiet ms. The promise will not resolve until the
* `markAsResolvable` function on the return object has been called. This is to prevent promise resolution
* before some important point in time such as network quiet or document load.
* Resolves when there have been no long tasks for at least waitForCPUQuiet ms.
* @param {number} waitForCPUQuiet
* @return {{promise: !Promise, cancel: function(), markAsResolvable: function()}}
* @return {{promise: !Promise, cancel: function()}}
*/
_waitForCPUIdle(waitForCPUQuiet) {
if (!waitForCPUQuiet) {
return {
markAsResolvable: () => undefined,
promise: Promise.resolve(),
cancel: () => undefined,
};
}

let lastTimeout;
let cancelled = false;
let isResolvable = false;
function checkForQuiet(driver, resolve) {
if (cancelled) return;

const tryLater = timeToWait => {
lastTimeout = setTimeout(() => checkForQuiet(driver, resolve), timeToWait);
};

if (!isResolvable) {
return tryLater(1000);
}

return driver.evaluateAsync(`(${checkTimeSinceLastLongTask.toString()})()`)
.then(timeSinceLongTask => {
if (cancelled) return;
Expand All @@ -458,7 +441,8 @@ class Driver {
resolve();
} else {
log.verbose('Driver', `CPU has been idle for ${timeSinceLongTask} ms`);
tryLater(waitForCPUQuiet - timeSinceLongTask);
const timeToWait = waitForCPUQuiet - timeSinceLongTask;
lastTimeout = setTimeout(() => checkForQuiet(driver, resolve), timeToWait);
}
});
}
Expand All @@ -474,9 +458,6 @@ class Driver {
});

return {
markAsResolvable: () => {
isResolvable = true;
},
promise,
cancel,
};
Expand Down Expand Up @@ -535,15 +516,17 @@ class Driver {
// Network listener. Resolves when the network has been idle for networkQuietThresholdMs.
const waitForNetworkIdle = this._waitForNetworkIdle(networkQuietThresholdMs);
// CPU listener. Resolves when the CPU has been idle for cpuQuietThresholdMs after network idle.
const waitForCPUIdle = this._waitForCPUIdle(cpuQuietThresholdMs);
let waitForCPUIdle = null;

// Wait for both load promises. Resolves on cleanup function the clears load
// timeout timer.
const loadPromise = Promise.all([
waitForLoadEvent.promise,
waitForNetworkIdle.promise.then(waitForCPUIdle.markAsResolvable),
waitForCPUIdle.promise,
waitForNetworkIdle.promise,
]).then(() => {
waitForCPUIdle = this._waitForCPUIdle(cpuQuietThresholdMs);
return waitForCPUIdle.promise;
}).then(() => {
return function() {
log.verbose('Driver', 'loadEventFired and network considered idle');
clearTimeout(maxTimeoutHandle);
Expand All @@ -559,7 +542,7 @@ class Driver {
log.warn('Driver', 'Timed out waiting for page load. Moving on...');
waitForLoadEvent.cancel();
waitForNetworkIdle.cancel();
waitForCPUIdle.cancel();
waitForCPUIdle && waitForCPUIdle.cancel();
};
});

Expand Down