Reputation: 605
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
Upvotes: 0
Views: 2488
Reputation: 4266
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
};
Upvotes: 2