Skip to content

Commit

Permalink
AwaitEventEmitter: drop-in replacement for EventEmitter
Browse files Browse the repository at this point in the history
It respects Promise pattern by callin "await" internally (for listeners that return a Promise).
  • Loading branch information
macbre committed Feb 1, 2019
1 parent e5ea316 commit f16bdeb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
26 changes: 26 additions & 0 deletions lib/AwaitEventEmitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var debug = require('debug')('phantomas:AwaitEventEmitter:emit');

// https://github.com/Psychopoulet/asynchronous-eventemitter/blob/master/lib/main.js
module.exports = class AwaitEventEmitter extends require("events") {
emit (eventName, ...args) {
var eventPromises = [];

if (eventName !== 'scopeMessage') {
debug(eventName);
}

this.listeners(eventName).forEach(fn => {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncFunction
const ret = fn(...args);

if (ret instanceof Promise) {
eventPromises.push(ret);
}

// console.log(fn, fn.toString(), ret, ret instanceof Promise);
});

// console.log('%s Promise.all', eventName);
return Promise.all(eventPromises);
}
}
30 changes: 2 additions & 28 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'use strict';

var Browser = require('./browser'),
EventEmitter = require('events').EventEmitter,
EventEmitter = require('./AwaitEventEmitter'),
debug = require('debug')('phantomas:core'),
loader = require('./loader'),
puppeteer = require("puppeteer"),
Expand Down Expand Up @@ -36,16 +36,6 @@ function phantomas(url, opts) {

debug('Options: %s', JSON.stringify(options));

// log emitted events
events._emit = events.emit;
events.log = require('debug')('phantomas:events');
events.emit = function() {
const eventName = arguments[0];
if (eventName != 'scopeMessage') this.log('emit: %s', eventName);

this._emit.apply(this, arguments);
}

events.setMaxListeners(100); // MaxListenersExceededWarning: Possible EventEmitter memory leak detected.

var results = new Results();
Expand Down Expand Up @@ -157,23 +147,7 @@ function phantomas(url, opts) {
loader.loadModules(scope);

// browser's scope and modules are set up, you can now use it in your modules
//events.emit('init', browser, page);

// TODO: introduce awaitEmit
var eventPromises = [];

events.listeners('init').forEach(async fn => {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncFunction
const ret = fn(browser, page);

if (ret instanceof Promise) {
eventPromises.push(ret);
}

// console.log(fn, fn.toString(), ret, ret instanceof Promise);
});

await Promise.all(eventPromises);
await events.emit('init', browser, page);

// https://github.com/GoogleChrome/puppeteer/blob/v1.11.0/docs/api.md#pagegotourl-options
const waitUntil = options['wait-for-network-idle'] ? 'networkidle0' : undefined,
Expand Down

0 comments on commit f16bdeb

Please sign in to comment.