Unconditionally update frame payload on navigation only. (#130)

This commit is contained in:
Pavel Feldman 2017-07-25 14:58:10 -07:00 committed by Andrey Lushnikov
parent eca0d7fe53
commit 4adf11427a

View File

@ -72,7 +72,7 @@ class FrameManager extends EventEmitter {
return; return;
console.assert(parentFrameId); console.assert(parentFrameId);
let parentFrame = this._frames.get(parentFrameId); let parentFrame = this._frames.get(parentFrameId);
let frame = new Frame(this._client, this._mouse, parentFrame, frameId, null); let frame = new Frame(this._client, this._mouse, parentFrame, frameId);
this._frames.set(frame._id, frame); this._frames.set(frame._id, frame);
this.emit(FrameManager.Events.FrameAttached, frame); this.emit(FrameManager.Events.FrameAttached, frame);
} }
@ -87,7 +87,7 @@ class FrameManager extends EventEmitter {
console.assert(!framePayload.parentId, 'Main frame shouldn\'t have parent frame id.'); console.assert(!framePayload.parentId, 'Main frame shouldn\'t have parent frame id.');
frame = this._mainFrame; frame = this._mainFrame;
} }
this._navigateFrame(frame, framePayload.id, framePayload); this._navigateFrame(frame, framePayload);
} }
/** /**
@ -109,17 +109,19 @@ class FrameManager extends EventEmitter {
/** /**
* @param {!Frame} frame * @param {!Frame} frame
* @param {string} newFrameId
* @param {?Object} newFramePayload * @param {?Object} newFramePayload
*/ */
_navigateFrame(frame, newFrameId, newFramePayload) { _navigateFrame(frame, newFramePayload) {
// Detach all child frames first. // Detach all child frames first.
for (let child of frame.childFrames()) for (let child of frame.childFrames())
this._removeFramesRecursively(child); this._removeFramesRecursively(child);
this._frames.delete(frame._id, frame); this._frames.delete(frame._id, frame);
frame._id = newFrameId;
frame._adoptPayload(newFramePayload); // Update frame id to retain frame identity.
this._frames.set(newFrameId, frame); frame._id = newFramePayload.id;
frame._navigated(newFramePayload);
this._frames.set(newFramePayload.id, frame);
this.emit(FrameManager.Events.FrameNavigated, frame); this.emit(FrameManager.Events.FrameNavigated, frame);
} }
@ -130,7 +132,8 @@ class FrameManager extends EventEmitter {
*/ */
_addFramesRecursively(parentFrame, frameTreePayload) { _addFramesRecursively(parentFrame, frameTreePayload) {
let framePayload = frameTreePayload.frame; let framePayload = frameTreePayload.frame;
let frame = new Frame(this._client, this._mouse, parentFrame, framePayload.id, framePayload); let frame = new Frame(this._client, this._mouse, parentFrame, framePayload.id);
frame._navigated(framePayload);
this._frames.set(frame._id, frame); this._frames.set(frame._id, frame);
for (let i = 0; frameTreePayload.childFrames && i < frameTreePayload.childFrames.length; ++i) for (let i = 0; frameTreePayload.childFrames && i < frameTreePayload.childFrames.length; ++i)
@ -166,9 +169,8 @@ class Frame {
* @param {!Mouse} mouse * @param {!Mouse} mouse
* @param {?Frame} parentFrame * @param {?Frame} parentFrame
* @param {string} frameId * @param {string} frameId
* @param {?Object} payload
*/ */
constructor(client, mouse, parentFrame, frameId, payload) { constructor(client, mouse, parentFrame, frameId) {
this._client = client; this._client = client;
this._mouse = mouse; this._mouse = mouse;
this._parentFrame = parentFrame; this._parentFrame = parentFrame;
@ -178,8 +180,6 @@ class Frame {
/** @type {!Set<!WaitTask>} */ /** @type {!Set<!WaitTask>} */
this._waitTasks = new Set(); this._waitTasks = new Set();
this._adoptPayload(payload);
/** @type {!Set<!Frame>} */ /** @type {!Set<!Frame>} */
this._childFrames = new Set(); this._childFrames = new Set();
if (this._parentFrame) if (this._parentFrame)
@ -301,12 +301,7 @@ class Frame {
const timeout = options.timeout || 30000; const timeout = options.timeout || 30000;
const waitForVisible = !!options.visible; const waitForVisible = !!options.visible;
const pageScript = helper.evaluationString(waitForSelectorPageFunction, selector, waitForVisible, timeout); const pageScript = helper.evaluationString(waitForSelectorPageFunction, selector, waitForVisible, timeout);
const waitTask = new WaitTask(this, pageScript, timeout); return new WaitTask(this, pageScript, timeout).promise;
this._waitTasks.add(waitTask);
let cleanup = () => this._waitTasks.delete(waitTask);
waitTask.promise.then(cleanup, cleanup);
return waitTask.promise;
} }
/** /**
@ -398,17 +393,13 @@ class Frame {
} }
/** /**
* @param {?Object} framePayload * @param {!Object} framePayload
*/ */
_adoptPayload(framePayload) { _navigated(framePayload) {
framePayload = framePayload || {
name: '',
url: '',
};
this._name = framePayload.name; this._name = framePayload.name;
this._url = framePayload.url; this._url = framePayload.url;
for (let waitTask of this._waitTasks) for (let waitTask of this._waitTasks)
waitTask.run(); waitTask.rerun();
} }
_detach() { _detach() {
@ -432,6 +423,7 @@ class WaitTask {
this._frame = frame; this._frame = frame;
this._pageScript = pageScript; this._pageScript = pageScript;
this._runningTask = null; this._runningTask = null;
frame._waitTasks.add(this);
this.promise = new Promise((resolve, reject) => { this.promise = new Promise((resolve, reject) => {
this._resolve = resolve; this._resolve = resolve;
this._reject = reject; this._reject = reject;
@ -439,7 +431,7 @@ class WaitTask {
// Since page navigation requires us to re-install the pageScript, we should track // Since page navigation requires us to re-install the pageScript, we should track
// timeout on our end. // timeout on our end.
this._timeoutTimer = setTimeout(() => this.terminate(new Error(`waiting failed: timeout ${timeout}ms exceeded`)), timeout); this._timeoutTimer = setTimeout(() => this.terminate(new Error(`waiting failed: timeout ${timeout}ms exceeded`)), timeout);
this.run(); this.rerun();
} }
/** /**
@ -450,7 +442,7 @@ class WaitTask {
this._cleanup(); this._cleanup();
} }
run() { rerun() {
let runningTask = this._frame._evaluateExpression(this._pageScript, true).then(finish.bind(this), finish.bind(this, false)); let runningTask = this._frame._evaluateExpression(this._pageScript, true).then(finish.bind(this), finish.bind(this, false));
this._runningTask = runningTask; this._runningTask = runningTask;
@ -474,6 +466,7 @@ class WaitTask {
_cleanup() { _cleanup() {
clearTimeout(this._timeoutTimer); clearTimeout(this._timeoutTimer);
this._frame._waitTasks.delete(this);
this._runningTask = null; this._runningTask = null;
} }
} }