Skip to content

Commit

Permalink
core(driver): add sendCommandAndIgnore (#15913)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirish authored Apr 10, 2024
1 parent 9bd33f8 commit 7fa8370
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 5 deletions.
1 change: 1 addition & 0 deletions core/gather/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const throwingSession = {
once: throwNotConnectedFn,
off: throwNotConnectedFn,
sendCommand: throwNotConnectedFn,
sendCommandAndIgnore: throwNotConnectedFn,
dispose: throwNotConnectedFn,
};

Expand Down
2 changes: 1 addition & 1 deletion core/gather/driver/target-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class TargetManager extends ProtocolEventEmitter {
throw err;
} finally {
// Resume the target if it was paused, but if it's unnecessary, we don't care about the error.
await newSession.sendCommand('Runtime.runIfWaitingForDebugger').catch(() => {});
await newSession.sendCommandAndIgnore('Runtime.runIfWaitingForDebugger');
}
}

Expand Down
5 changes: 2 additions & 3 deletions core/gather/driver/wait-for-condition.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,9 +498,8 @@ async function waitForFullyLoaded(session, networkMonitor, options) {
if (await isPageHung(session)) {
log.warn('waitFor', 'Page appears to be hung, killing JavaScript...');
// We don't await these, as we want to exit with PAGE_HUNG
void session.sendCommand('Emulation.setScriptExecutionDisabled', {value: true})
.catch(_ => {});
void session.sendCommand('Runtime.terminateExecution').catch(_ => {});
void session.sendCommandAndIgnore('Emulation.setScriptExecutionDisabled', {value: true});
void session.sendCommandAndIgnore('Runtime.terminateExecution');
throw new LighthouseError(LighthouseError.errors.PAGE_HUNG);
}

Expand Down
12 changes: 12 additions & 0 deletions core/gather/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ class ProtocolSession extends CrdpEventEmitter {
});
}

/**
* Send and if there's an error response, do not reject.
* @template {keyof LH.CrdpCommands} C
* @param {C} method
* @param {LH.CrdpCommands[C]['paramsType']} params
* @return {Promise<void>}
*/
sendCommandAndIgnore(method, ...params) {
return this.sendCommand(method, ...params)
.catch(e => log.verbose('session', method, e.message)).then(_ => void 0);
}

/**
* Disposes of a session so that it can no longer talk to Chrome.
* @return {Promise<void>}
Expand Down
4 changes: 3 additions & 1 deletion core/test/gather/mock-driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ import {NetworkMonitor} from '../../gather/driver/network-monitor.js';
/** @typedef {import('../../gather/driver/execution-context.js')} ExecutionContext */

function createMockSession() {
const mockSendCommand = createMockSendCommandFn();
return {
setTargetInfo: fnAny(),
sendCommand: createMockSendCommandFn(),
sendCommand: mockSendCommand,
sendCommandAndIgnore: mockSendCommand,
setNextProtocolTimeout: fnAny(),
hasNextProtocolTimeout: fnAny(),
getNextProtocolTimeout: fnAny(),
Expand Down
1 change: 1 addition & 0 deletions types/gatherer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ declare module Gatherer {
once<TEvent extends keyof CrdpEvents>(event: TEvent, callback: (...args: CrdpEvents[TEvent]) => void): void;
off<TEvent extends keyof CrdpEvents>(event: TEvent, callback: (...args: CrdpEvents[TEvent]) => void): void;
sendCommand<TMethod extends keyof CrdpCommands>(method: TMethod, ...params: CrdpCommands[TMethod]['paramsType']): Promise<CrdpCommands[TMethod]['returnType']>;
sendCommandAndIgnore<TMethod extends keyof CrdpCommands>(method: TMethod, ...params: CrdpCommands[TMethod]['paramsType']): Promise<void>;
dispose(): Promise<void>;
}

Expand Down

0 comments on commit 7fa8370

Please sign in to comment.