Issue with EventEmitter inheritance - node.js

I'm having a small issue, playing around with module patterns. I'm trying to attach an eventemitter to my library, but it doesn't seem to work and I get :
cmd.on('message',function(msg){
^
TypeError: undefined is not a function
My lib looks like :
var util = require('util');
var EventEmitter = require("events").EventEmitter;
var TestLib = function() {
var self = this;
function sendRandom(cb){
self.emit('message','whatever');
cb(null,0);
}
return {
init: function(cb) {
console.log('init');
cb(null);
},
run: function(params,cb){
console.log('running ',params);
sendRandom(function(err,res){
if(err){
cb(new Error(err));
}else{
cb(null,res);
}
});
},
close: function(cb) {}
};
};
util.inherits(TestLib, EventEmitter);
module.exports = TestLib;
And I call it as such :
var cmd = require(__dirname+'/bin/testlib.js')();
cmd.on('message',function(msg){
log(msg);
});
cmd.init(function(err){
if(err){
log(err);
}else{
cmd.run(line,function(err,res){
if(err){
log(err);
}else{
log(res);
}
});
}
});
I'm sure I'm overlooking something simple, but what?

see whether the example below helps you.
testLib.js
var EventEmitter = require('events').EventEmitter;
var util = require('util');
function TestLib(ms) {
var self = this;
EventEmitter.call(this);
this.random = function() {
//emitting 'random' event randomly
var ms = Math.random() * 100000 % 3000;
console.log('emitting random event in ' + ms + ' milliseconds');
setTimeout(function() {
self.emit('random', ms);
self.random();
}, ms);
return self;
}
}
util.inherits(TestLib, EventEmitter);
module.exports = TestLib;
test.js
var TestLib = require('./testLib');
new TestLib()
.random()
.on('random', function(ms) {
console.log('random event emitted after ' + ms + ' milliseconds');
});
to run it, execute node test.js

Related

Extending Node's EventEmitter in ES5

I'm learning JavaScript/Nodejs and I've decided to start with ES5 before looking at ES6. I'm looking at prototypical inheritance and the EventEmitter.
I want to extend EventEmitter with something simple, my code sample is below. However I receive the error:
this.emit('messageRead', message);
TypeError: this.emit is not a function
What am I doing wrong?
var EventEmitter = require('events').EventEmitter;
var MyEmitter = function() {
EventEmitter.call(this);
this.messages = []
}
MyEmitter.prototype = Object.create(EventEmitter.prototype);
MyEmitter.prototype.constructor = MyEmitter;
MyEmitter.prototype.addMessage = function(message) {
this.messages.push(message)
this.emit('messageAdded', message);
return this;
}
MyEmitter.prototype.readMessages = function() {
this.messages.forEach(function(message){
this.emit('messageRead', message);
});
return this;
}
var emitter1 = new MyEmitter();
emitter1
.addMessage('hello')
.addMessage('goodbye')
.on('messageAdded', function(message) { console.log('message added: ' + message)})
.on('messageRead', function(message) { console.log('message read: ' + message)})
.readMessages();
You must inherit from EventEmitter, not only run constructor in MyEmitter instance scope
const util = require('util');
...
util.inherits(MyEmitter, EventEmitter);
The issue with my code was with this and closures. See the updated readMessages below.
But thanks to #Yarsolav who has probably pointed at a better solution.
var EventEmitter = require('events').EventEmitter;
var MyEmitter = function() {
EventEmitter.call(this);
this.messages = []
}
MyEmitter.prototype = Object.create(EventEmitter.prototype);
MyEmitter.prototype.constructor = MyEmitter;
MyEmitter.prototype.addMessage = function(message) {
this.messages.push(message)
this.emit('messageAdded', message);
return this;
}
MyEmitter.prototype.readMessages = function() {
var that = this;
this.messages.forEach(function(message){
that.emit('messageRead', message);
});
return this;
}
var emitter1 = new MyEmitter();
emitter1
.on('messageAdded', function(message) { console.log('message added: ' + message)})
.on('messageRead', function(message) { console.log('message read: ' + message)})
.addMessage('hello')
.addMessage('goodbye')
.readMessages();

Using the node-robosmart calls

I have been using the code from github sandeepmistry/node-robosmart. Specifically I am doing this:
var async = require('async');
var RoboSmart = require('./index');
var found =0;
RoboSmart.discover(function(roboSmart) {
async.series([
function(callback) {
console.log('connect');
roboSmart.connect(callback);
},
function(callback) {
console.log('discoverServicesAndCharacteristics');
roboSmart.discoverServicesAndCharacteristics(callback);
},
function(callback) {
roboSmart.getLightName(function(lightName) {
console.log('light name = ' + lightName);
if(lightName = 'XXXX'){
found=1;
console.log(found);
}
callback();
});
},
function(callback) {
console.log('disconnect');
roboSmart.disconnect(callback);
}
],
function() {
process.exit(0);
});
});
The code works great and I am able to do other stuff. What I am having trouble with is using the RoboSmart.discoverAll(timeout, callback(devices)); // Returns array of devices discovered within command.
When I use this code:
var async = require('async');
var RoboSmart = require('./index');
var found =0;
RoboSmart.discoverAll(2000,function(devices) {
async.series([
function(callback) {
}
],
function() {
process.exit(0);
});
});
~
It Hangs. Can someone help me with using this function?

Different results using Node.js' module.exports in Yeoman

I am receiving different results with module.exports and would appreciate someone helping me shore up the obvious hole in my knowledge.
From the following code, I receive the result listed below it.
var generators = require('yeoman-generator');
var MyBase = generators.Base.extend({
helper: function() {
console.log('this is a helper method');
}
});
module.exports = MyBase.extend({
method1: function() {
console.log('method 1 just ran');
}
});
Result:
method 1 just ran
But if I place module.exports on its own line, and assign MyBase to it, I get the following result. Here is the code:
var generators = require('yeoman-generator');
var MyBase = generators.Base.extend({
helper: function() {
console.log('this is a helper method');
}
});
MyBase.extend({
method1: function() {
console.log('method 1 just ran');
}
});
module.exports = MyBase
Result:
this is a helper method
What is causing the difference in outputs?
I haven't been able to completely reproduce your issue, but the problem is almost certainly the fact that calling .extend on a generator returns a new generator with both the current and extended properties.
var generators = require('yeoman-generator');
var MyBase = generators.Base.extend({
helper: function() {
console.log('this is a helper method');
}
});
// Capture the output of the .extend function
var MyBase2 = MyBase.extend({
method1: function() {
console.log('method 1 just ran');
}
});
module.exports = MyBase2
Alternatively, you could just define multiple properties in one go
var MyBase = generators.Base.extend({
helper: function() {
console.log('this is a helper method');
},
method1: function() {
console.log('method 1 just ran');
}
});

How to transition from async.parallel & series to composable Promises?

I am trying to transition from async to promises and this is what I have. If the code looks contrived it's because I simplified it from what I'm working on to make it easier to grasp. I'm struggling to get the Promise.all to execute.
I commented out the async code that I want to implement in promises:
var Promise = require('bluebird');
var fs = Promise.promisifyAll(require('fs-extra'));
var path = require('path');
var tar = require('tar-fs');
module.exports = Archive;
function Archive() {
var self = this;
var self.base_dir = '/bar/baz',
var self.file1 = 'foo/file1',
var self.file2 = 'foo/file2',
var self.file3 = 'foo/file3',
var self.file4 = 'foo/file4'
}
Archive.prototype.make = function(done) {
var self = this;
// async.series([
// function(next) {
// self._prepareFilesDir(next);
// },
// function(next) {
// self._copyFiles(next);
// },
// function(next) {
// self._writeArchive(next);
// }
// ], done)
self._prepareFilesDir().bind(self)
.then(self._copyFiles.bind(self))
.then(self._writeArchive.bind(self))
.catch(function(e) {
return done(e);
});
};
// ********************************
// * Private functions
// ********************************
Archive.prototype._prepareFilesDir = function() {
var self = this;
return fs.emptyDirAsync(self.base_dir);
};
Archive.prototype._copyFiles = function() {
var self = this;
var sources = {
file1: path.resolve('baz', 'file1'),
file2: path.resolve('baz', 'file2')
file3: path.resolve('baz', 'file3')
file4: path.resolve('baz', 'file4')
file5: path.resolve('baz', 'file5')
};
var destinations = {
file1: path.resolve(self.base_dir, self.file1),
file2: path.resolve(self.base_dir, self.file2),
file3: path.resolve(self.base_dir, self.file3),
file4: path.resolve(self.base_dir, self.file4),
file5: path.resolve(self.base_dir, self.file5)
};
var filters = {
qux: /^qux/,
bru: /^bru/,
blerg: /blerg$/
};
function copyFile1() {
console.log('hello world');
return fs.copyAsync(sources.file2, destinations.file1, { filter: filters.qux });
};
function copyFile2() {
return fs.copyAsync(sources.file2, destinations.file2);
};
function copyFile3() {
return fs.copyAsync(sources.file3, destinations.file3, { filter: filters.bru });
};
function copyFile4() {
return fs.copyAsync(sources.file4, destinations.file4, { filter: filters.blerg });
};
return Promise.all([
copyFile1,
copyFile2,
copyFile3,
copyFile4
]);
// async.parallel([
// copyFile1(next),
// copyFile2(next),
// copyFile3(next),
// copyFile4(next)
// ], function(err) {
// if (err) return done(err);
// done(null);
// })
};
Archive.prototype._writeArchive = function() {
var self = this;
var archive_dir_path = path.resolve(self.base_dir, '..');
var tarPromise = function() {
return new Promise(function(resolve, reject) {
tar.pack(self.files_path)
.pipe(fs.createWriteStream(archive_dir_path + '.tar'))
.on('error', reject)
.on('finish', resolve)
});
};
fs.ensureDirAsync(archive_dir_path)
.then(tarPromise);
};
I must be doing something wrong because the 'hello world' is never printed. I think the stream is promisified correctly but I'm not so sure either. I based my conversion on the promise-nuggets.github.io snippets.
How do I have to do the Promise.all? I'd like to keep separate functions as I think it helps understanding the code better.
Thanks,
the mistakes that I found:
in make method, done would be called only in case of error, suggestion remove done callback, just return promise
again in make, you are doing _prepareFilesDir().bind(self), for staters bind at that point is redundant, it should have been call/apply at that point.
in _writeArchive, you need to return promise, else it ll return undefined and assume that the async function is finished.
updated code in fiddle
There were several issues which #mido22 fixed. There is one more issue though, in Pormise.all(), the functions shouldn't be passed as references but rather executed.
var Promise = require('bluebird');
var fs = Promise.promisifyAll(require('fs-extra'));
var path = require('path');
var tar = require('tar-fs');
module.exports = Archive;
function Archive() {
var self = this;
var self.base_dir = '/bar/baz',
var self.file1 = 'foo/file1',
var self.file2 = 'foo/file2',
var self.file3 = 'foo/file3',
var self.file4 = 'foo/file4'
}
Archive.prototype.make = function() { // CHANGED
var self = this;
// async.series([
// function(next) {
// self._prepareFilesDir(next);
// },
// function(next) {
// self._copyFiles(next);
// },
// function(next) {
// self._writeArchive(next);
// }
// ], done)
return self._prepareFilesDir() // CHANGED
.then(self._copyFiles.bind(self))
.then(self._writeArchive.bind(self)); // CHANGED
};
// ********************************
// * Private functions
// ********************************
Archive.prototype._prepareFilesDir = function() {
var self = this;
return fs.emptyDirAsync(self.base_dir);
};
Archive.prototype._copyFiles = function() {
var self = this;
var sources = {
file1: path.resolve('baz', 'file1'),
file2: path.resolve('baz', 'file2')
file3: path.resolve('baz', 'file3')
file4: path.resolve('baz', 'file4')
file5: path.resolve('baz', 'file5')
};
var destinations = {
file1: path.resolve(self.base_dir, self.file1),
file2: path.resolve(self.base_dir, self.file2),
file3: path.resolve(self.base_dir, self.file3),
file4: path.resolve(self.base_dir, self.file4),
file5: path.resolve(self.base_dir, self.file5)
};
var filters = {
qux: /^qux/,
bru: /^bru/,
blerg: /blerg$/
};
function copyFile1() {
console.log('hello world');
return fs.copyAsync(sources.file2, destinations.file1, { filter: filters.qux });
};
function copyFile2() {
return fs.copyAsync(sources.file2, destinations.file2);
};
function copyFile3() {
return fs.copyAsync(sources.file3, destinations.file3, { filter: filters.bru });
};
function copyFile4() {
return fs.copyAsync(sources.file4, destinations.file4, { filter: filters.blerg });
};
return Promise.all([
copyFile1(), // execute functions
copyFile2(), // idem
copyFile3(), // idem
copyFile4() // idem
]);
// async.parallel([
// copyFile1(next),
// copyFile2(next),
// copyFile3(next),
// copyFile4(next)
// ], function(err) {
// if (err) return done(err);
// done(null);
// })
};
Archive.prototype._writeArchive = function() {
var self = this;
var archive_dir_path = path.resolve(self.base_dir, '..');
var tarPromise = function() {
return new Promise(function(resolve, reject) {
tar.pack(self.files_path)
.pipe(fs.createWriteStream(archive_dir_path + '.tar'))
.on('error', reject)
.on('finish', resolve)
});
};
return fs.ensureDirAsync(archive_dir_path)
.then(tarPromise); // CHANGED,
};

making event in module.exports functions

I'm trying to make a module with an even that I could call from the index file (after a require).
My code includes var events = require("events"); and I've wrote only the tricky part here.
index.js:
var reqw = require('./module.js');
reqw.on('data', function(d) {
console.log(d);
});
module.js:
module.exports = {
listaccts: function() {
events.EventEmitter.call(this);
}
}
util.inherits(exports.listaccts, events.EventEmitter);
exports.listaccts.prototype.listme = function() {
thisList = this;
var req = https.request(requestOptions, function(res) {
res.on('data', function(chuck) {
store = chuck;
});
res.on('end', function(d) {
thisList.emit("data", store.toString());
});
});
}
Searched the whole we and yet to find a proper answer..
Modified your code slightly :
module.js
function listaccts(){
}
util.inherits(listaccts, EventEmitter);
listaccts.prototype.listMe = function(){
var self = this;
var store = [];
console.log('here');
var req = https.request(requestOptions, function(res) {
res.on('data', function(chuck) {
console.log('data');
store.push(chuck);
});
res.on('end', function() {
console.log('end');
self.emit("data", store);
});
});
req.end();
};
module.exports = listaccts;
index.js
var reqw = require('./module');
var obj = new reqw();
obj.listMe();
obj.on('data', function(err, data) {
console.log(err);
});
req.end is important, I have forgot to include and got a never-ending cycle.
Created instance for binding this, so no need for EventEmitter.call.
Maybe you want to the listMe function to inside your constructor.
Hope this help.

Resources