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:
Andrey Lushnikov 2019-02-07 18:03:48 -08:00 committed by GitHub
parent 6b18e8cef5
commit 72c00474e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 50 deletions

View File

@ -1,5 +1,6 @@
const {helper, assert} = require('./helper');
const {Page} = require('./Page');
const {Events} = require('./Events');
const EventEmitter = require('events');
class Browser extends EventEmitter {
@ -109,14 +110,14 @@ class Browser extends EventEmitter {
return existingTarget;
let resolve;
const targetPromise = new Promise(x => resolve = x);
this.on(Browser.Events.TargetCreated, check);
this.on(Events.Browser.TargetCreated, check);
this.on('targetchanged', check);
try {
if (!timeout)
return await targetPromise;
return await helper.waitWithTimeout(targetPromise, 'target', timeout);
} finally {
this.removeListener(Browser.Events.TargetCreated, check);
this.removeListener(Events.Browser.TargetCreated, check);
this.removeListener('targetchanged', check);
}
@ -161,22 +162,22 @@ class Browser extends EventEmitter {
const context = browserContextId ? this._contexts.get(browserContextId) : this._defaultContext;
const target = new Target(this._connection, this, context, pageId, url);
this._pageTargets.set(pageId, target);
this.emit(Browser.Events.TargetCreated, target);
context.emit(BrowserContext.Events.TargetCreated, target);
this.emit(Events.Browser.TargetCreated, target);
context.emit(Events.BrowserContext.TargetCreated, target);
}
_onTabClosed({pageId}) {
const target = this._pageTargets.get(pageId);
this._pageTargets.delete(pageId);
this.emit(Browser.Events.TargetDestroyed, target);
target.browserContext().emit(BrowserContext.Events.TargetDestroyed, target);
this.emit(Events.Browser.TargetDestroyed, target);
target.browserContext().emit(Events.BrowserContext.TargetDestroyed, target);
}
_onTabNavigated({pageId, url}) {
const target = this._pageTargets.get(pageId);
target._url = url;
this.emit(Browser.Events.TargetChanged, target);
target.browserContext().emit(BrowserContext.Events.TargetChanged, target);
this.emit(Events.Browser.TargetChanged, target);
target.browserContext().emit(Events.BrowserContext.TargetChanged, target);
}
async close() {
@ -185,13 +186,6 @@ class Browser extends EventEmitter {
}
}
/** @enum {string} */
Browser.Events = {
TargetCreated: 'targetcreated',
TargetChanged: 'targetchanged',
TargetDestroyed: 'targetdestroyed'
}
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};

View File

@ -15,6 +15,7 @@
*/
const debugProtocol = require('debug')('hdfox:protocol');
const EventEmitter = require('events');
const {Events} = require('./Events');
/**
* @internal
@ -84,7 +85,7 @@ class Connection extends EventEmitter {
for (const callback of this._callbacks.values())
callback.reject(rewriteError(callback.error, `Protocol error (${callback.method}): Target closed.`));
this._callbacks.clear();
this.emit(Connection.Events.Disconnected);
this.emit(Events.Connection.Disconnected);
}
dispose() {
@ -93,10 +94,6 @@ class Connection extends EventEmitter {
}
}
Connection.Events = {
Disconnected: Symbol('Connection.Events.Disconnected'),
};
/**
* @param {!Error} error
* @param {string} method

View 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};

View File

@ -8,6 +8,7 @@ const mime = require('mime');
const util = require('util');
const EventEmitter = require('events');
const {JSHandle, createHandle} = require('./JSHandle');
const {Events} = require('./Events');
const writeFileAsync = util.promisify(fs.writeFile);
const readFileAsync = util.promisify(fs.readFile);
@ -90,7 +91,7 @@ class Page extends EventEmitter {
_onUncaughtError(params) {
let error = new Error(params.message);
error.stack = params.stack;
this.emit(Page.Events.PageError, error);
this.emit(Events.Page.PageError, error);
}
viewport() {
@ -154,7 +155,7 @@ class Page extends EventEmitter {
}
_onDialogOpened(params) {
this.emit(Page.Events.Dialog, new Dialog(this._session, params));
this.emit(Events.Page.Dialog, new Dialog(this._session, params));
}
_onFrameAttached(params) {
@ -168,7 +169,7 @@ class Page extends EventEmitter {
this._mainFrame = frame;
}
this._frames.set(params.frameId, frame);
this.emit(Page.Events.FrameAttached, frame);
this.emit(Events.Page.FrameAttached, frame);
}
mainFrame() {
@ -179,7 +180,7 @@ class Page extends EventEmitter {
const frame = this._frames.get(params.frameId);
this._frames.delete(params.frameId);
frame._detach();
this.emit(Page.Events.FrameDetached, frame);
this.emit(Events.Page.FrameDetached, frame);
}
_onNavigationCommitted(params) {
@ -187,13 +188,13 @@ class Page extends EventEmitter {
frame._navigated(params.url, params.name, params.navigationId);
frame._DOMContentLoadedFired = false;
frame._loadFired = false;
this.emit(Page.Events.FrameNavigated, frame);
this.emit(Events.Page.FrameNavigated, frame);
}
_onSameDocumentNavigation(params) {
const frame = this._frames.get(params.frameId);
frame._url = params.url;
this.emit(Page.Events.FrameNavigated, frame);
this.emit(Events.Page.FrameNavigated, frame);
}
get keyboard(){
@ -578,7 +579,7 @@ class Page extends EventEmitter {
_onClosed() {
this._isClosed = true;
helper.removeEventListeners(this._eventListeners);
this.emit(Page.Events.Close);
this.emit(Events.Page.Close);
}
_onEventFired({frameId, name}) {
@ -586,9 +587,9 @@ class Page extends EventEmitter {
frame._firedEvents.add(name.toLowerCase());
if (frame === this._mainFrame) {
if (name === 'load')
this.emit(Page.Events.Load);
this.emit(Events.Page.Load);
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}) {
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 {
/**
* @param {string} type