mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
refactor(firefox): introduce /lib/Events (#3949)
Declare all events in `/lib/Events.js`, similarly to how we do it in Puppeteer.
This commit is contained in:
parent
6b18e8cef5
commit
72c00474e7
@ -1,5 +1,6 @@
|
|||||||
const {helper, assert} = require('./helper');
|
const {helper, assert} = require('./helper');
|
||||||
const {Page} = require('./Page');
|
const {Page} = require('./Page');
|
||||||
|
const {Events} = require('./Events');
|
||||||
const EventEmitter = require('events');
|
const EventEmitter = require('events');
|
||||||
|
|
||||||
class Browser extends EventEmitter {
|
class Browser extends EventEmitter {
|
||||||
@ -109,14 +110,14 @@ class Browser extends EventEmitter {
|
|||||||
return existingTarget;
|
return existingTarget;
|
||||||
let resolve;
|
let resolve;
|
||||||
const targetPromise = new Promise(x => resolve = x);
|
const targetPromise = new Promise(x => resolve = x);
|
||||||
this.on(Browser.Events.TargetCreated, check);
|
this.on(Events.Browser.TargetCreated, check);
|
||||||
this.on('targetchanged', check);
|
this.on('targetchanged', check);
|
||||||
try {
|
try {
|
||||||
if (!timeout)
|
if (!timeout)
|
||||||
return await targetPromise;
|
return await targetPromise;
|
||||||
return await helper.waitWithTimeout(targetPromise, 'target', timeout);
|
return await helper.waitWithTimeout(targetPromise, 'target', timeout);
|
||||||
} finally {
|
} finally {
|
||||||
this.removeListener(Browser.Events.TargetCreated, check);
|
this.removeListener(Events.Browser.TargetCreated, check);
|
||||||
this.removeListener('targetchanged', check);
|
this.removeListener('targetchanged', check);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,22 +162,22 @@ class Browser extends EventEmitter {
|
|||||||
const context = browserContextId ? this._contexts.get(browserContextId) : this._defaultContext;
|
const context = browserContextId ? this._contexts.get(browserContextId) : this._defaultContext;
|
||||||
const target = new Target(this._connection, this, context, pageId, url);
|
const target = new Target(this._connection, this, context, pageId, url);
|
||||||
this._pageTargets.set(pageId, target);
|
this._pageTargets.set(pageId, target);
|
||||||
this.emit(Browser.Events.TargetCreated, target);
|
this.emit(Events.Browser.TargetCreated, target);
|
||||||
context.emit(BrowserContext.Events.TargetCreated, target);
|
context.emit(Events.BrowserContext.TargetCreated, target);
|
||||||
}
|
}
|
||||||
|
|
||||||
_onTabClosed({pageId}) {
|
_onTabClosed({pageId}) {
|
||||||
const target = this._pageTargets.get(pageId);
|
const target = this._pageTargets.get(pageId);
|
||||||
this._pageTargets.delete(pageId);
|
this._pageTargets.delete(pageId);
|
||||||
this.emit(Browser.Events.TargetDestroyed, target);
|
this.emit(Events.Browser.TargetDestroyed, target);
|
||||||
target.browserContext().emit(BrowserContext.Events.TargetDestroyed, target);
|
target.browserContext().emit(Events.BrowserContext.TargetDestroyed, target);
|
||||||
}
|
}
|
||||||
|
|
||||||
_onTabNavigated({pageId, url}) {
|
_onTabNavigated({pageId, url}) {
|
||||||
const target = this._pageTargets.get(pageId);
|
const target = this._pageTargets.get(pageId);
|
||||||
target._url = url;
|
target._url = url;
|
||||||
this.emit(Browser.Events.TargetChanged, target);
|
this.emit(Events.Browser.TargetChanged, target);
|
||||||
target.browserContext().emit(BrowserContext.Events.TargetChanged, target);
|
target.browserContext().emit(Events.BrowserContext.TargetChanged, target);
|
||||||
}
|
}
|
||||||
|
|
||||||
async close() {
|
async close() {
|
||||||
@ -185,13 +186,6 @@ class Browser extends EventEmitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @enum {string} */
|
|
||||||
Browser.Events = {
|
|
||||||
TargetCreated: 'targetcreated',
|
|
||||||
TargetChanged: 'targetchanged',
|
|
||||||
TargetDestroyed: 'targetdestroyed'
|
|
||||||
}
|
|
||||||
|
|
||||||
class Target {
|
class Target {
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -305,11 +299,4 @@ class BrowserContext extends EventEmitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @enum {string} */
|
|
||||||
BrowserContext.Events = {
|
|
||||||
TargetCreated: 'targetcreated',
|
|
||||||
TargetChanged: 'targetchanged',
|
|
||||||
TargetDestroyed: 'targetdestroyed'
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {Browser, BrowserContext, Target};
|
module.exports = {Browser, BrowserContext, Target};
|
||||||
|
@ -15,6 +15,7 @@
|
|||||||
*/
|
*/
|
||||||
const debugProtocol = require('debug')('hdfox:protocol');
|
const debugProtocol = require('debug')('hdfox:protocol');
|
||||||
const EventEmitter = require('events');
|
const EventEmitter = require('events');
|
||||||
|
const {Events} = require('./Events');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
@ -84,7 +85,7 @@ class Connection extends EventEmitter {
|
|||||||
for (const callback of this._callbacks.values())
|
for (const callback of this._callbacks.values())
|
||||||
callback.reject(rewriteError(callback.error, `Protocol error (${callback.method}): Target closed.`));
|
callback.reject(rewriteError(callback.error, `Protocol error (${callback.method}): Target closed.`));
|
||||||
this._callbacks.clear();
|
this._callbacks.clear();
|
||||||
this.emit(Connection.Events.Disconnected);
|
this.emit(Events.Connection.Disconnected);
|
||||||
}
|
}
|
||||||
|
|
||||||
dispose() {
|
dispose() {
|
||||||
@ -93,10 +94,6 @@ class Connection extends EventEmitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Connection.Events = {
|
|
||||||
Disconnected: Symbol('Connection.Events.Disconnected'),
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {!Error} error
|
* @param {!Error} error
|
||||||
* @param {string} method
|
* @param {string} method
|
||||||
|
29
experimental/puppeteer-firefox/lib/Events.js
Normal file
29
experimental/puppeteer-firefox/lib/Events.js
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
const Events = {
|
||||||
|
Page: {
|
||||||
|
Close: 'close',
|
||||||
|
Console: 'console',
|
||||||
|
Dialog: 'dialog',
|
||||||
|
DOMContentLoaded: 'domcontentloaded',
|
||||||
|
FrameAttached: 'frameattached',
|
||||||
|
FrameDetached: 'framedetached',
|
||||||
|
FrameNavigated: 'framenavigated',
|
||||||
|
Load: 'load',
|
||||||
|
PageError: 'pageerror',
|
||||||
|
},
|
||||||
|
Browser: {
|
||||||
|
TargetCreated: 'targetcreated',
|
||||||
|
TargetChanged: 'targetchanged',
|
||||||
|
TargetDestroyed: 'targetdestroyed',
|
||||||
|
},
|
||||||
|
BrowserContext: {
|
||||||
|
TargetCreated: 'targetcreated',
|
||||||
|
TargetChanged: 'targetchanged',
|
||||||
|
TargetDestroyed: 'targetdestroyed',
|
||||||
|
},
|
||||||
|
|
||||||
|
Connection: {
|
||||||
|
Disconnected: Symbol('Events.Connection.Disconnected'),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {Events};
|
@ -8,6 +8,7 @@ const mime = require('mime');
|
|||||||
const util = require('util');
|
const util = require('util');
|
||||||
const EventEmitter = require('events');
|
const EventEmitter = require('events');
|
||||||
const {JSHandle, createHandle} = require('./JSHandle');
|
const {JSHandle, createHandle} = require('./JSHandle');
|
||||||
|
const {Events} = require('./Events');
|
||||||
|
|
||||||
const writeFileAsync = util.promisify(fs.writeFile);
|
const writeFileAsync = util.promisify(fs.writeFile);
|
||||||
const readFileAsync = util.promisify(fs.readFile);
|
const readFileAsync = util.promisify(fs.readFile);
|
||||||
@ -90,7 +91,7 @@ class Page extends EventEmitter {
|
|||||||
_onUncaughtError(params) {
|
_onUncaughtError(params) {
|
||||||
let error = new Error(params.message);
|
let error = new Error(params.message);
|
||||||
error.stack = params.stack;
|
error.stack = params.stack;
|
||||||
this.emit(Page.Events.PageError, error);
|
this.emit(Events.Page.PageError, error);
|
||||||
}
|
}
|
||||||
|
|
||||||
viewport() {
|
viewport() {
|
||||||
@ -154,7 +155,7 @@ class Page extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_onDialogOpened(params) {
|
_onDialogOpened(params) {
|
||||||
this.emit(Page.Events.Dialog, new Dialog(this._session, params));
|
this.emit(Events.Page.Dialog, new Dialog(this._session, params));
|
||||||
}
|
}
|
||||||
|
|
||||||
_onFrameAttached(params) {
|
_onFrameAttached(params) {
|
||||||
@ -168,7 +169,7 @@ class Page extends EventEmitter {
|
|||||||
this._mainFrame = frame;
|
this._mainFrame = frame;
|
||||||
}
|
}
|
||||||
this._frames.set(params.frameId, frame);
|
this._frames.set(params.frameId, frame);
|
||||||
this.emit(Page.Events.FrameAttached, frame);
|
this.emit(Events.Page.FrameAttached, frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
mainFrame() {
|
mainFrame() {
|
||||||
@ -179,7 +180,7 @@ class Page extends EventEmitter {
|
|||||||
const frame = this._frames.get(params.frameId);
|
const frame = this._frames.get(params.frameId);
|
||||||
this._frames.delete(params.frameId);
|
this._frames.delete(params.frameId);
|
||||||
frame._detach();
|
frame._detach();
|
||||||
this.emit(Page.Events.FrameDetached, frame);
|
this.emit(Events.Page.FrameDetached, frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
_onNavigationCommitted(params) {
|
_onNavigationCommitted(params) {
|
||||||
@ -187,13 +188,13 @@ class Page extends EventEmitter {
|
|||||||
frame._navigated(params.url, params.name, params.navigationId);
|
frame._navigated(params.url, params.name, params.navigationId);
|
||||||
frame._DOMContentLoadedFired = false;
|
frame._DOMContentLoadedFired = false;
|
||||||
frame._loadFired = false;
|
frame._loadFired = false;
|
||||||
this.emit(Page.Events.FrameNavigated, frame);
|
this.emit(Events.Page.FrameNavigated, frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
_onSameDocumentNavigation(params) {
|
_onSameDocumentNavigation(params) {
|
||||||
const frame = this._frames.get(params.frameId);
|
const frame = this._frames.get(params.frameId);
|
||||||
frame._url = params.url;
|
frame._url = params.url;
|
||||||
this.emit(Page.Events.FrameNavigated, frame);
|
this.emit(Events.Page.FrameNavigated, frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
get keyboard(){
|
get keyboard(){
|
||||||
@ -578,7 +579,7 @@ class Page extends EventEmitter {
|
|||||||
_onClosed() {
|
_onClosed() {
|
||||||
this._isClosed = true;
|
this._isClosed = true;
|
||||||
helper.removeEventListeners(this._eventListeners);
|
helper.removeEventListeners(this._eventListeners);
|
||||||
this.emit(Page.Events.Close);
|
this.emit(Events.Page.Close);
|
||||||
}
|
}
|
||||||
|
|
||||||
_onEventFired({frameId, name}) {
|
_onEventFired({frameId, name}) {
|
||||||
@ -586,9 +587,9 @@ class Page extends EventEmitter {
|
|||||||
frame._firedEvents.add(name.toLowerCase());
|
frame._firedEvents.add(name.toLowerCase());
|
||||||
if (frame === this._mainFrame) {
|
if (frame === this._mainFrame) {
|
||||||
if (name === 'load')
|
if (name === 'load')
|
||||||
this.emit(Page.Events.Load);
|
this.emit(Events.Page.Load);
|
||||||
else if (name === 'DOMContentLoaded')
|
else if (name === 'DOMContentLoaded')
|
||||||
this.emit(Page.Events.DOMContentLoaded);
|
this.emit(Events.Page.DOMContentLoaded);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -599,7 +600,7 @@ class Page extends EventEmitter {
|
|||||||
|
|
||||||
_onConsole({type, args, frameId}) {
|
_onConsole({type, args, frameId}) {
|
||||||
const frame = this._frames.get(frameId);
|
const frame = this._frames.get(frameId);
|
||||||
this.emit(Page.Events.Console, new ConsoleMessage(type, args.map(arg => createHandle(frame, arg))));
|
this.emit(Events.Page.Console, new ConsoleMessage(type, args.map(arg => createHandle(frame, arg))));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -610,19 +611,6 @@ class Page extends EventEmitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @enum {string} */
|
|
||||||
Page.Events = {
|
|
||||||
Close: 'close',
|
|
||||||
Console: 'console',
|
|
||||||
Dialog: 'dialog',
|
|
||||||
DOMContentLoaded: 'domcontentloaded',
|
|
||||||
FrameAttached: 'frameattached',
|
|
||||||
FrameDetached: 'framedetached',
|
|
||||||
FrameNavigated: 'framenavigated',
|
|
||||||
Load: 'load',
|
|
||||||
PageError: 'pageerror'
|
|
||||||
}
|
|
||||||
|
|
||||||
class ConsoleMessage {
|
class ConsoleMessage {
|
||||||
/**
|
/**
|
||||||
* @param {string} type
|
* @param {string} type
|
||||||
|
Loading…
Reference in New Issue
Block a user