Clean up Browser annotations. (#138)

This commit is contained in:
Alexei Filippov 2017-07-25 20:39:50 -07:00 committed by Andrey Lushnikov
parent aa28eb80f0
commit 83f4e43564

View File

@ -58,11 +58,11 @@ class Browser {
`--user-data-dir=${this._userDataDir}`, `--user-data-dir=${this._userDataDir}`,
]); ]);
if (typeof options.headless !== 'boolean' || options.headless) { if (typeof options.headless !== 'boolean' || options.headless) {
this._chromeArguments.push(...[ this._chromeArguments.push(
`--headless`, `--headless`,
`--disable-gpu`, `--disable-gpu`,
`--hide-scrollbars`, `--hide-scrollbars`
]); );
} }
if (typeof options.executablePath === 'string') { if (typeof options.executablePath === 'string') {
this._chromeExecutable = options.executablePath; this._chromeExecutable = options.executablePath;
@ -96,7 +96,7 @@ class Browser {
} }
/** /**
* @return {string} * @return {!Promise<string>}
*/ */
async version() { async version() {
await this._ensureChromeIsRunning(); await this._ensureChromeIsRunning();
@ -104,12 +104,18 @@ class Browser {
return version.Browser; return version.Browser;
} }
/**
* @return {!Promise}
*/
async _ensureChromeIsRunning() { async _ensureChromeIsRunning() {
if (!this._launchPromise) if (!this._launchPromise)
this._launchPromise = this._launchChrome(); this._launchPromise = this._launchChrome();
return this._launchPromise; return this._launchPromise;
} }
/**
* @return {!Promise}
*/
async _launchChrome() { async _launchChrome() {
this._chromeProcess = childProcess.spawn(this._chromeExecutable, this._chromeArguments, {}); this._chromeProcess = childProcess.spawn(this._chromeExecutable, this._chromeArguments, {});
let stderr = ''; let stderr = '';
@ -146,24 +152,27 @@ class Browser {
module.exports = Browser; module.exports = Browser;
helper.tracePublicAPI(Browser); helper.tracePublicAPI(Browser);
/**
* @param {!ChildProcess} chromeProcess
* @return {!Promise<number>}
*/
function waitForRemoteDebuggingPort(chromeProcess) { function waitForRemoteDebuggingPort(chromeProcess) {
const rl = readline.createInterface({ input: chromeProcess.stderr }); return new Promise(fulfill => {
let fulfill; const rl = readline.createInterface({ input: chromeProcess.stderr });
let promise = new Promise(x => fulfill = x); rl.on('line', onLine);
rl.on('line', onLine); rl.once('close', () => fulfill(-1));
rl.once('close', () => fulfill(-1));
return promise;
/** /**
* @param {string} line * @param {string} line
*/ */
function onLine(line) { function onLine(line) {
const match = line.match(/^DevTools listening on .*:([\d]+)$/); const match = line.match(/^DevTools listening on .*:(\d+)$/);
if (!match) if (!match)
return; return;
rl.removeListener('line', onLine); rl.removeListener('line', onLine);
fulfill(Number.parseInt(match[1], 10)); fulfill(Number.parseInt(match[1], 10));
} }
});
} }
class TaskQueue { class TaskQueue {
@ -172,11 +181,11 @@ class TaskQueue {
} }
/** /**
* @param {function():!Promise} task * @param {function()} task
* @return {!Promise} * @return {!Promise}
*/ */
postTask(task) { postTask(task) {
let result = this._chain.then(task); const result = this._chain.then(task);
this._chain = result.catch(() => {}); this._chain = result.catch(() => {});
return result; return result;
} }