Skip to content

Commit

Permalink
refactor(firefox): introduce /lib/Events (#3949)
Browse files Browse the repository at this point in the history
Declare all events in `/lib/Events.js`, similarly to how
we do it in Puppeteer.
  • Loading branch information
aslushnikov authored Feb 8, 2019
1 parent 6b18e8c commit 72c0047
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 50 deletions.
31 changes: 9 additions & 22 deletions experimental/puppeteer-firefox/lib/Browser.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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() {
Expand All @@ -185,13 +186,6 @@ class Browser extends EventEmitter {
}
}

/** @enum {string} */
Browser.Events = {
TargetCreated: 'targetcreated',
TargetChanged: 'targetchanged',
TargetDestroyed: 'targetdestroyed'
}

class Target {
/**
*
Expand Down Expand Up @@ -305,11 +299,4 @@ class BrowserContext extends EventEmitter {
}
}

/** @enum {string} */
BrowserContext.Events = {
TargetCreated: 'targetcreated',
TargetChanged: 'targetchanged',
TargetDestroyed: 'targetdestroyed'
}

module.exports = {Browser, BrowserContext, Target};
7 changes: 2 additions & 5 deletions experimental/puppeteer-firefox/lib/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
const debugProtocol = require('debug')('hdfox:protocol');
const EventEmitter = require('events');
const {Events} = require('./Events');

/**
* @internal
Expand Down Expand Up @@ -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() {
Expand All @@ -93,10 +94,6 @@ class Connection extends EventEmitter {
}
}

Connection.Events = {
Disconnected: Symbol('Connection.Events.Disconnected'),
};

/**
* @param {!Error} error
* @param {string} method
Expand Down
29 changes: 29 additions & 0 deletions experimental/puppeteer-firefox/lib/Events.js
Original file line number Diff line number Diff line change
@@ -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};
34 changes: 11 additions & 23 deletions experimental/puppeteer-firefox/lib/Page.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand All @@ -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() {
Expand All @@ -179,21 +180,21 @@ 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) {
const frame = this._frames.get(params.frameId);
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(){
Expand Down Expand Up @@ -578,17 +579,17 @@ 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}) {
const frame = this._frames.get(frameId);
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);
}
}

Expand All @@ -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))));
}

/**
Expand All @@ -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
Expand Down

0 comments on commit 72c0047

Please sign in to comment.