feat: worker convenience methods (#2677)

This patch:
- adds `worker.evaluate` and `worker.evaluateHandle` methods as a shortcut to their execution context equivalents.
- makes the error messages a bit nicer when interacting with a closed worker (as opposed to a closed page).
- moves the worker tests into their own spec file.
This commit is contained in:
Joel Einbinder
2018-06-06 18:16:17 -07:00
committed by Andrey Lushnikov
parent 3e82a54fd1
commit 2ff0adcad8
8 changed files with 100 additions and 41 deletions

View File

@@ -150,12 +150,12 @@ class Connection extends EventEmitter {
}
/**
* @param {string} targetId
* @param {Protocol.Target.TargetInfo} targetInfo
* @return {!Promise<!CDPSession>}
*/
async createSession(targetId) {
const {sessionId} = await this.send('Target.attachToTarget', {targetId});
const session = new CDPSession(this, targetId, sessionId);
async createSession(targetInfo) {
const {sessionId} = await this.send('Target.attachToTarget', {targetId: targetInfo.targetId});
const session = new CDPSession(this, targetInfo.type, sessionId);
this._sessions.set(sessionId, session);
return session;
}
@@ -164,16 +164,16 @@ class Connection extends EventEmitter {
class CDPSession extends EventEmitter {
/**
* @param {!Connection|!CDPSession} connection
* @param {string} targetId
* @param {string} targetType
* @param {string} sessionId
*/
constructor(connection, targetId, sessionId) {
constructor(connection, targetType, sessionId) {
super();
this._lastId = 0;
/** @type {!Map<number, {resolve: function, reject: function, error: !Error, method: string}>}*/
this._callbacks = new Map();
this._connection = connection;
this._targetId = targetId;
this._targetType = targetType;
this._sessionId = sessionId;
/** @type {!Map<string, !CDPSession>}*/
this._sessions = new Map();
@@ -186,7 +186,7 @@ class CDPSession extends EventEmitter {
*/
send(method, params = {}) {
if (!this._connection)
return Promise.reject(new Error(`Protocol error (${method}): Session closed. Most likely the page has been closed.`));
return Promise.reject(new Error(`Protocol error (${method}): Session closed. Most likely the ${this._targetType} has been closed.`));
const id = ++this._lastId;
const message = JSON.stringify({id, method, params});
debugSession('SEND ► ' + message);
@@ -245,11 +245,11 @@ class CDPSession extends EventEmitter {
}
/**
* @param {string} targetId
* @param {string} targetType
* @param {string} sessionId
*/
_createSession(targetId, sessionId) {
const session = new CDPSession(this, targetId, sessionId);
_createSession(targetType, sessionId) {
const session = new CDPSession(this, targetType, sessionId);
this._sessions.set(sessionId, session);
return session;
}