refactor: drop object factory from execution context (#3099)

This patch:
- merges `ElementHandle` into `ExecutionContext` (for simplicity; there's no good reason to have them in separate files).
- removes the necessity to pass handle factory when creating `ExecutionContext`

This makes it easier to create execution contexts out of payloads.

References #1215
This commit is contained in:
Andrey Lushnikov
2018-08-16 16:16:27 -07:00
committed by GitHub
parent 73c43bebd8
commit d6eb3b4d52
6 changed files with 419 additions and 431 deletions

View File

@@ -17,8 +17,7 @@
const fs = require('fs');
const EventEmitter = require('events');
const {helper, assert} = require('./helper');
const {ExecutionContext, JSHandle} = require('./ExecutionContext');
const {ElementHandle} = require('./ElementHandle');
const {ExecutionContext} = require('./ExecutionContext');
const {TimeoutError} = require('./Errors');
const readFileAsync = helper.promisify(fs.readFile);
@@ -87,6 +86,13 @@ class FrameManager extends EventEmitter {
this._handleFrameTree(child);
}
/**
* @return {!Puppeteer.Page}
*/
page() {
return this._page;
}
/**
* @return {!Frame}
*/
@@ -119,7 +125,7 @@ class FrameManager extends EventEmitter {
return;
assert(parentFrameId);
const parentFrame = this._frames.get(parentFrameId);
const frame = new Frame(this._client, parentFrame, frameId);
const frame = new Frame(this, this._client, parentFrame, frameId);
this._frames.set(frame._id, frame);
this.emit(FrameManager.Events.FrameAttached, frame);
}
@@ -146,7 +152,7 @@ class FrameManager extends EventEmitter {
frame._id = framePayload.id;
} else {
// Initial main frame navigation.
frame = new Frame(this._client, null, framePayload.id);
frame = new Frame(this, this._client, null, framePayload.id);
}
this._frames.set(framePayload.id, frame);
this._mainFrame = frame;
@@ -184,7 +190,7 @@ class FrameManager extends EventEmitter {
const frameId = contextPayload.auxData ? contextPayload.auxData.frameId : null;
const frame = this._frames.get(frameId) || null;
/** @type {!ExecutionContext} */
const context = new ExecutionContext(this._client, contextPayload, obj => this.createJSHandle(context, obj), frame);
const context = new ExecutionContext(this._client, contextPayload, frame);
this._contextIdToContext.set(contextPayload.id, context);
if (frame)
frame._addExecutionContext(context);
@@ -220,17 +226,6 @@ class FrameManager extends EventEmitter {
return context;
}
/**
* @param {!ExecutionContext} context
* @param {!Protocol.Runtime.RemoteObject} remoteObject
* @return {!JSHandle}
*/
createJSHandle(context, remoteObject) {
if (remoteObject.subtype === 'node')
return new ElementHandle(context, this._client, remoteObject, this._page, this);
return new JSHandle(context, this._client, remoteObject);
}
/**
* @param {!Frame} frame
*/
@@ -259,17 +254,19 @@ FrameManager.Events = {
*/
class Frame {
/**
* @param {!FrameManager} frameManager
* @param {!Puppeteer.CDPSession} client
* @param {?Frame} parentFrame
* @param {string} frameId
*/
constructor(client, parentFrame, frameId) {
constructor(frameManager, client, parentFrame, frameId) {
this._frameManager = frameManager;
this._client = client;
this._parentFrame = parentFrame;
this._url = '';
this._id = frameId;
/** @type {?Promise<!ElementHandle>} */
/** @type {?Promise<!Puppeteer.ElementHandle>} */
this._documentPromise = null;
/** @type {?Promise<!ExecutionContext>} */
this._contextPromise = null;
@@ -350,7 +347,7 @@ class Frame {
/**
* @param {string} selector
* @return {!Promise<?ElementHandle>}
* @return {!Promise<?Puppeteer.ElementHandle>}
*/
async $(selector) {
const document = await this._document();
@@ -359,7 +356,7 @@ class Frame {
}
/**
* @return {!Promise<!ElementHandle>}
* @return {!Promise<!Puppeteer.ElementHandle>}
*/
async _document() {
if (this._documentPromise)
@@ -373,7 +370,7 @@ class Frame {
/**
* @param {string} expression
* @return {!Promise<!Array<!ElementHandle>>}
* @return {!Promise<!Array<!Puppeteer.ElementHandle>>}
*/
async $x(expression) {
const document = await this._document();
@@ -406,7 +403,7 @@ class Frame {
/**
* @param {string} selector
* @return {!Promise<!Array<!ElementHandle>>}
* @return {!Promise<!Array<!Puppeteer.ElementHandle>>}
*/
async $$(selector) {
const document = await this._document();
@@ -476,7 +473,7 @@ class Frame {
/**
* @param {Object} options
* @return {!Promise<!ElementHandle>}
* @return {!Promise<!Puppeteer.ElementHandle>}
*/
async addScriptTag(options) {
if (typeof options.url === 'string') {
@@ -542,7 +539,7 @@ class Frame {
/**
* @param {Object} options
* @return {!Promise<!ElementHandle>}
* @return {!Promise<!Puppeteer.ElementHandle>}
*/
async addStyleTag(options) {
if (typeof options.url === 'string') {
@@ -878,7 +875,7 @@ class WaitTask {
async rerun() {
const runCount = ++this._runCount;
/** @type {?JSHandle} */
/** @type {?Puppeteer.JSHandle} */
let success = null;
let error = null;
try {