Skip to content

Commit

Permalink
cleanup: Use ES6 default params (#447)
Browse files Browse the repository at this point in the history
  • Loading branch information
ebidel authored and aslushnikov committed Aug 21, 2017
1 parent 271fd09 commit 5d6d3e0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 34 deletions.
22 changes: 9 additions & 13 deletions lib/Input.js
Expand Up @@ -31,17 +31,17 @@ class Keyboard {
* @param {{text: (string|undefined)}} options
* @return {!Promise}
*/
async down(key, options) {
let {text} = options || {};
let autoRepeat = this._pressedKeys.has(key);
async down(key, options = {}) {
const text = options.text;
const autoRepeat = this._pressedKeys.has(key);
this._pressedKeys.add(key);
this._modifiers |= this._modifierBit(key);
await this._client.send('Input.dispatchKeyEvent', {
type: text ? 'keyDown' : 'rawKeyDown',
modifiers: this._modifiers,
windowsVirtualKeyCode: codeForKey(key),
key: key,
text: text,
key,
text,
unmodifiedText: text,
autoRepeat
});
Expand Down Expand Up @@ -127,20 +127,18 @@ class Mouse {
* @param {number} y
* @param {!Object=} options
*/
async click(x, y, options) {
async click(x, y, options = {}) {
this.move(x, y);
this.down(options);
if (options && options.delay)
if (typeof options.delay === 'number')
await new Promise(f => setTimeout(f, options.delay));
await this.up(options);
}

/**
* @param {!Object=} options
*/
async down(options) {
if (!options)
options = {};
async down(options = {}) {
this._button = (options.button || 'left');
await this._client.send('Input.dispatchMouseEvent', {
type: 'mousePressed',
Expand All @@ -155,9 +153,7 @@ class Mouse {
/**
* @param {!Object=} options
*/
async up(options) {
if (!options)
options = {};
async up(options = {}) {
this._button = 'none';
await this._client.send('Input.dispatchMouseEvent', {
type: 'mouseReleased',
Expand Down
27 changes: 13 additions & 14 deletions lib/Launcher.js
Expand Up @@ -47,55 +47,54 @@ const DEFAULT_ARGS = [

class Launcher {
/**
* @param {!Object} options
* @param {!Object=} options
* @return {!Promise<!Browser>}
*/
static async launch(options) {
options = options || {};
let userDataDir = [
static async launch(options = {}) {
const userDataDir = [
CHROME_PROFILE_PATH,
process.pid,
++browserId,
crypto.randomBytes(8 / 2).toString('hex') // add random salt 8 characters long.
].join('-');

let chromeArguments = DEFAULT_ARGS.concat([
const chromeArguments = DEFAULT_ARGS.concat([
`--user-data-dir=${userDataDir}`,
]);
if (typeof options.headless !== 'boolean' || options.headless) {
chromeArguments.push(
`--headless`,
`--disable-gpu`,
`--hide-scrollbars`,
'--headless',
'--disable-gpu',
'--hide-scrollbars',
'--mute-audio'
);
}
let chromeExecutable = options.executablePath;
if (typeof chromeExecutable !== 'string') {
let revisionInfo = Downloader.revisionInfo(Downloader.currentPlatform(), ChromiumRevision);
const revisionInfo = Downloader.revisionInfo(Downloader.currentPlatform(), ChromiumRevision);
console.assert(revisionInfo.downloaded, `Chromium revision is not downloaded. Run "npm install"`);
chromeExecutable = revisionInfo.executablePath;
}
if (Array.isArray(options.args))
chromeArguments.push(...options.args);

let chromeProcess = childProcess.spawn(chromeExecutable, chromeArguments, {});
const chromeProcess = childProcess.spawn(chromeExecutable, chromeArguments, {});
if (options.dumpio) {
chromeProcess.stdout.pipe(process.stdout);
chromeProcess.stderr.pipe(process.stderr);
}

// Cleanup as processes exit.
let listeners = [
const listeners = [
helper.addEventListener(process, 'exit', killChromeAndCleanup),
helper.addEventListener(chromeProcess, 'exit', killChromeAndCleanup),
];
if (options.handleSIGINT !== false)
listeners.push(helper.addEventListener(process, 'SIGINT', killChromeAndCleanup));
try {
let connectionDelay = options.slowMo || 0;
let browserWSEndpoint = await waitForWSEndpoint(chromeProcess, options.timeout || 30 * 1000);
let connection = await Connection.create(browserWSEndpoint, connectionDelay);
const connectionDelay = options.slowMo || 0;
const browserWSEndpoint = await waitForWSEndpoint(chromeProcess, options.timeout || 30 * 1000);
const connection = await Connection.create(browserWSEndpoint, connectionDelay);
return new Browser(connection, !!options.ignoreHTTPSErrors, killChromeAndCleanup);
} catch (e) {
killChromeAndCleanup();
Expand Down
7 changes: 2 additions & 5 deletions lib/Page.js
Expand Up @@ -427,8 +427,7 @@ class Page extends EventEmitter {
* @param {!Object=} options
* @return {!Promise<!Buffer>}
*/
async screenshot(options) {
options = options || {};
async screenshot(options = {}) {
let screenshotType = null;
if (options.path) {
let mimeType = mime.lookup(options.path);
Expand Down Expand Up @@ -506,9 +505,7 @@ class Page extends EventEmitter {
* @param {!Object=} options
* @return {!Promise<!Buffer>}
*/
async pdf(options) {
options = options || {};

async pdf(options = {}) {
let scale = options.scale || 1;
let displayHeaderFooter = !!options.displayHeaderFooter;
let printBackground = !!options.printBackground;
Expand Down
3 changes: 1 addition & 2 deletions phantom_shim/WebPage.js
Expand Up @@ -28,11 +28,10 @@ class WebPage {
* @param {string} scriptPath
* @param {!Object=} options
*/
constructor(browser, scriptPath, options) {
constructor(browser, scriptPath, options = {}) {
this._page = await(browser.newPage());
this.settings = new WebPageSettings(this._page);

options = options || {};
options.settings = options.settings || {};
if (options.settings.userAgent)
this.settings.userAgent = options.settings.userAgent;
Expand Down

0 comments on commit 5d6d3e0

Please sign in to comment.