Closed norgen closed 8 years ago
Hi @norgen, it seems like you're not using the api correctly.
When I tried to test it, first I needed to make MyObject
inherit EventEmitter
like this:
var util = require('util')
var MyObject = function() {
EventEmitter.call(this)
};
util.inherits(MyObject, EventEmitter)
or else I would have got TypeError: object1.emit is not a function
exception.
Secondly, it seems like you're trying to merge object1
and object2
events to base
:
merge.to(base).emitters([object1, object2])
Since object1
and object2
have no events registered to them, base
ends up 'inheriting' nothing.
Thirdly, after you merge object1
and object2
events to base
, you're still emitting to object1
and object2
instead of emitting to base
:
object1.emit('test')
object2.emit('test')
instead of:
base.emit('test')
Fourthly (is that a word?), pay attention to what you're emitting; instead of emitting test
, you're emitting Test
, which is a whole other event:
base.on('test', function() {
console.log('Test!'); // Here not call
});
// [code...]
object1.emit('Test') // no such event as 'Test'.
object2.emit('Test') // though 'test' event do exists.
Fifthly (this is getting absurd), you're merging the event emitters before you event initiate their events:
var object1 = new MyObject()
var object2 = new MyObject()
var base = new EventEmitter()
merge.to(base).emitters([object1, object2]) // merging
base.on('test', function() { // then initiating?
console.log('Test!') // Here not call
})
Okay, so combining it all, you get:
var EventEmitter = require('events').EventEmitter
var merge = require('events-merge')
var util = require('util')
var MyObject = function() {
EventEmitter.call(this)
};
util.inherits(MyObject, EventEmitter)
var object1 = new MyObject()
var object2 = new MyObject()
var base = new EventEmitter()
object1.on('test', function() {
console.log('Test!')
});
object2.on('test', function() {
console.log('Test!')
});
merge.to(base).emitters([object1, object2])
base.emit('test')
which prints:
Test!
Test!
Process finished with exit code 0
Or another option (if that what you've meant to achieve):
var EventEmitter = require('events').EventEmitter
var merge = require('events-merge')
var util = require('util')
var MyObject = function() {
EventEmitter.call(this)
};
util.inherits(MyObject, EventEmitter)
var object1 = new MyObject()
var object2 = new MyObject()
var base = new EventEmitter()
base.on('test', function() {
console.log('Test!')
});
merge.to(object1).emitters(base)
merge.to(object2).emitters(base)
object1.emit('test')
object2.emit('test')
which also prints:
Test!
Test!
Process finished with exit code 0
Hope this clears things out. Sorry for my late response, I was very busy and didn't notice your issue. I'll keep it open for discussion.
Oh, inconsiderate... Thanks!
No problems.
Why?