Skip to content

Commit

Permalink
polish: listen for network idle after DCL (#2271)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce authored and brendankenny committed May 24, 2017
1 parent d706429 commit 131df27
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions lighthouse-core/gather/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,9 +382,8 @@ class Driver {
}

/**
* Returns a promise that resolves when the network has been idle for
* `networkQuietThresholdMs` ms and a method to cancel internal network listeners and
* timeout.
* 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()}}
Expand All @@ -409,17 +408,21 @@ class Driver {
clearTimeout(idleTimeout);
};

const domContentLoadedListener = () => {
if (this._networkStatusMonitor.is2Idle()) {
onIdle();
} else {
onBusy();
}
};

this.once('Page.domContentEventFired', domContentLoadedListener);
cancel = () => {
clearTimeout(idleTimeout);
this.off('Page.domContentEventFired', domContentLoadedListener);
this._networkStatusMonitor.removeListener('network-2-busy', onBusy);
this._networkStatusMonitor.removeListener('network-2-idle', onIdle);
};

if (this._networkStatusMonitor.is2Idle()) {
onIdle();
} else {
onBusy();
}
}).then(() => {
// Once idle has been determined wait another pauseAfterLoadMs
return new Promise(resolve => setTimeout(resolve, pauseAfterNetworkQuietMs));
Expand Down

0 comments on commit 131df27

Please sign in to comment.