Skip to content

Commit

Permalink
core(gather): gatherFn refactor (#15830)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirish committed Feb 26, 2024
1 parent 8662fbe commit 54e843a
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 88 deletions.
75 changes: 37 additions & 38 deletions core/gather/navigation-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,44 +265,43 @@ async function navigationGather(page, requestor, options = {}) {
const isCallback = typeof requestor === 'function';

const runnerOptions = {resolvedConfig, computedCache};
const artifacts = await Runner.gather(
async () => {
const normalizedRequestor = isCallback ? requestor : UrlUtils.normalizeUrl(requestor);

/** @type {LH.Puppeteer.Browser|undefined} */
let lhBrowser = undefined;
/** @type {LH.Puppeteer.Page|undefined} */
let lhPage = undefined;

// For navigation mode, we shouldn't connect to a browser in audit mode,
// therefore we connect to the browser in the gatherFn callback.
if (!page) {
const {hostname = DEFAULT_HOSTNAME, port = DEFAULT_PORT} = flags;
lhBrowser = await puppeteer.connect({browserURL: `http://${hostname}:${port}`, defaultViewport: null});
lhPage = await lhBrowser.newPage();
page = lhPage;
}

const driver = new Driver(page);
const context = {
driver,
lhBrowser,
lhPage,
page,
resolvedConfig,
requestor: normalizedRequestor,
computedCache,
};
const {baseArtifacts} = await _setup(context);

const artifacts = await _navigation({...context, baseArtifacts});

await _cleanup(context);

return finalizeArtifacts(baseArtifacts, artifacts);
},
runnerOptions
);

const gatherFn = async () => {
const normalizedRequestor = isCallback ? requestor : UrlUtils.normalizeUrl(requestor);

/** @type {LH.Puppeteer.Browser|undefined} */
let lhBrowser = undefined;
/** @type {LH.Puppeteer.Page|undefined} */
let lhPage = undefined;

// For navigation mode, we shouldn't connect to a browser in audit mode,
// therefore we connect to the browser in the gatherFn callback.
if (!page) {
const {hostname = DEFAULT_HOSTNAME, port = DEFAULT_PORT} = flags;
lhBrowser = await puppeteer.connect({browserURL: `http://${hostname}:${port}`, defaultViewport: null});
lhPage = await lhBrowser.newPage();
page = lhPage;
}

const driver = new Driver(page);
const context = {
driver,
lhBrowser,
lhPage,
page,
resolvedConfig,
requestor: normalizedRequestor,
computedCache,
};
const {baseArtifacts} = await _setup(context);

const artifacts = await _navigation({...context, baseArtifacts});

await _cleanup(context);

return finalizeArtifacts(baseArtifacts, artifacts);
};
const artifacts = await Runner.gather(gatherFn, runnerOptions);
return {artifacts, runnerOptions};
}

Expand Down
50 changes: 25 additions & 25 deletions core/gather/snapshot-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,35 @@ async function snapshotGather(page, options = {}) {
const url = await driver.url();

const runnerOptions = {resolvedConfig, computedCache};
const artifacts = await Runner.gather(
async () => {
const baseArtifacts =

const gatherFn = async () => {
const baseArtifacts =
await getBaseArtifacts(resolvedConfig, driver, {gatherMode: 'snapshot'});
baseArtifacts.URL = {
finalDisplayedUrl: url,
};
baseArtifacts.URL = {
finalDisplayedUrl: url,
};

const artifactDefinitions = resolvedConfig.artifacts || [];
const artifactState = getEmptyArtifactState();
await collectPhaseArtifacts({
phase: 'getArtifact',
gatherMode: 'snapshot',
driver,
page,
baseArtifacts,
artifactDefinitions,
artifactState,
computedCache,
settings: resolvedConfig.settings,
});

const artifactDefinitions = resolvedConfig.artifacts || [];
const artifactState = getEmptyArtifactState();
await collectPhaseArtifacts({
phase: 'getArtifact',
gatherMode: 'snapshot',
driver,
page,
baseArtifacts,
artifactDefinitions,
artifactState,
computedCache,
settings: resolvedConfig.settings,
});
await driver.disconnect();

await driver.disconnect();
const artifacts = await awaitArtifacts(artifactState);
return finalizeArtifacts(baseArtifacts, artifacts);
};

const artifacts = await awaitArtifacts(artifactState);
return finalizeArtifacts(baseArtifacts, artifacts);
},
runnerOptions
);
const artifacts = await Runner.gather(gatherFn, runnerOptions);
return {artifacts, runnerOptions};
}

Expand Down
49 changes: 24 additions & 25 deletions core/gather/timespan-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,31 +72,30 @@ async function startTimespanGather(page, options = {}) {
const finalDisplayedUrl = await driver.url();

const runnerOptions = {resolvedConfig, computedCache};
const artifacts = await Runner.gather(
async () => {
baseArtifacts.URL = {finalDisplayedUrl};

await collectPhaseArtifacts({phase: 'stopSensitiveInstrumentation', ...phaseOptions});
await collectPhaseArtifacts({phase: 'stopInstrumentation', ...phaseOptions});

// bf-cache-failures can emit `Page.frameNavigated` at the end of the run.
// This can cause us to issue protocol commands after the target closes.
// We should disable our `Page.frameNavigated` handlers before that.
await disableAsyncStacks();

driver.defaultSession.off('Page.frameNavigated', onFrameNavigated);
if (pageNavigationDetected) {
baseArtifacts.LighthouseRunWarnings.push(str_(UIStrings.warningNavigationDetected));
}

await collectPhaseArtifacts({phase: 'getArtifact', ...phaseOptions});
await driver.disconnect();

const artifacts = await awaitArtifacts(artifactState);
return finalizeArtifacts(baseArtifacts, artifacts);
},
runnerOptions
);
const gatherFn = async () => {
baseArtifacts.URL = {finalDisplayedUrl};

await collectPhaseArtifacts({phase: 'stopSensitiveInstrumentation', ...phaseOptions});
await collectPhaseArtifacts({phase: 'stopInstrumentation', ...phaseOptions});

// bf-cache-failures can emit `Page.frameNavigated` at the end of the run.
// This can cause us to issue protocol commands after the target closes.
// We should disable our `Page.frameNavigated` handlers before that.
await disableAsyncStacks();

driver.defaultSession.off('Page.frameNavigated', onFrameNavigated);
if (pageNavigationDetected) {
baseArtifacts.LighthouseRunWarnings.push(str_(UIStrings.warningNavigationDetected));
}

await collectPhaseArtifacts({phase: 'getArtifact', ...phaseOptions});
await driver.disconnect();

const artifacts = await awaitArtifacts(artifactState);
return finalizeArtifacts(baseArtifacts, artifacts);
};

const artifacts = await Runner.gather(gatherFn, runnerOptions);
return {artifacts, runnerOptions};
},
};
Expand Down

0 comments on commit 54e843a

Please sign in to comment.