0

I'm trying to learn webpack and es6 at the same time and realized a need for some sort of event bus. I'm new to EventEmitters and constructors. I've read up on the docs and well as a bunch of examples, but I honestly don't get why the code below is behaving the way it does. There's only one instance of the emitter, so why issn't the counter or the removeListener able to find anything? If i have all the code in a single file, it works fine.

entry.js

import dVpEe from '../modules/eventEmitter';
const temp = new dVpEe();


var abc = function abc() {
    console.log('funciton a');
};

var b = function() {
    console.log('funciton b');
};

temp.evesdroppers('connection');
temp.hear('connection', abc);
temp.evesdroppers('connection');
temp.say('connection');
temp.hear('connection', b);
temp.evesdroppers('connection');
temp.say('connection');
temp.walk('connection', abc);
temp.say('connection');

eventEmitter.js

import events from 'events';
import util from 'util';

var EventEmitter = events.EventEmitter;

var dVpEe = function() {
    EventEmitter.call(this);
};

util.inherits(dVpEe, EventEmitter);

dVpEe.prototype.walk = function(event, cb, name) {
    console.log('%c' + (name || 'creeper') + '%c NOT listening for %c' + event, 'color: pink', 'color: white', 'color: pink');
    this.removeListener(event, cb);
};

dVpEe.prototype.say = function(event, name) {
    console.log('%c' + (name || 'someone') + '%c screamed %c' + event, 'color: pink', 'color: white', 'color: pink');
    this.emit(name);
};

dVpEe.prototype.evesdroppers = function(event) {
    var eventListeners = events.EventEmitter.listenerCount(this, event);
    console.log('%c' + eventListeners + '%c listner(s) for %c' + event, 'color: pink', 'color: white', 'color: pink');
};

dVpEe.prototype.hear = function(event, cb, name) {
    console.log('%c' + (name || 'creeper') + '%c listening for %c' + event, 'color: pink', 'color: white', 'color: pink');
    this.addListener(name, cb);
};

export default dVpEe;

ouput

0 listner(s) for connection
creeper listening for connection
0 listner(s) for connection
someone screamed connection
function a
creeper listening for connection
0 listner(s) for connection
someone screamed connection
function a
funciton b
creeper NOT listening for connection
someone screamed connection
funciton a
funciton b
Jeff
  • 607
  • 1
  • 7
  • 17

1 Answers1

2

It's just a typo in dVpEe.prototype.hear.

dVpEe.prototype.hear = function(event, cb, name) {
    console.log((name || 'creeper') + ' listening for "' + event + "'");
    this.addListener(name, cb); // ouch!
};

I would also suggest to replace deprecated EventEmitter.listenerCount to emitter.listenerCount:

dVpEe.prototype.evesdroppers = function(event) {
    var eventListeners = this.listenerCount(event);
};

Since You are using ES6, I suggest leverage class syntax, which is far more readable:

import { EventEmitter } from 'events';

export default class extends EventEmitter {
  walk(event, cb, name) {
      console.log((name || 'creeper') + ' NOT listening for "' + event + "'");
      this.removeListener(event, cb);
  }

  // and so on
};
  • 2
    You should add how to fix `this.addListener(name, cb);` instead of just noting "*ouch!*" – Bergi May 05 '16 at 22:15
  • omg... my first post on stack is a typo. I spent 2 hours refactoring this over and over. I'm gonna go sulk in the corner in embarrassment. Well at least that means that I grasped the concept correctly, hah. Thank you for pointing it out as well as the suggestions! Just one of those days... – Jeff May 05 '16 at 22:21
  • It's no big deal : ) And do not hesitate to upvote if the answer was helpful. – Роман Парадеев May 05 '16 at 22:26
  • I'll come back to it once I get some rep. – Jeff May 06 '16 at 15:15