i have this node js script:
var EventEmitter = require('events').EventEmitter,
util = require('util');
var pfioObject = function () {
this.init = function () {
console.log("started server");
};
this.deinit = function () {
console.log("stopped server");
};
this.read_input = function () {
return 0;
};
};
console.log(util.inspect(EventEmitter, false, null)); //<--- this shows no method emit either
var pfio = new pfioObject();
var pfioServer = function () {
this.prev_state = 0;
this.start = function () {
pfio.init();
this.watchInputs();
};
this.stop = function () {
pfio.deinit();
};
}
util.inherits(pfioServer, EventEmitter);
// add some event emitting methods to it
pfioServer.prototype.watchInputs = function () {
var state = pfio.read_input();
if (state !== this.prev_state) {
this.emit('pfio.inputs.changed', state, this.prev_state);
this.prev_state = state;
}
setTimeout(this.watchInputs, 10); // going to put this on the event loop next event, more efficient
};
// export the object as a module
module.exports = new pfioServer();
For some reason the node error says that there is no such object as emit, i have done npm install events
to see if that will fix it, it didn't. I'm unsure as to why I'm getting this error.
I think somewhere along my code there is an error but I can't see anything.
To run this code I have another script which does this:
var pfioServer = require('./pfioServer'),
util = require('util');
console.log(util.inspect(pfioServer, false, null)); //<--- this line shows the pfioServer with out the prototype function watchInputs
pfioServer.start();
EDIT
I think I may have missed out on some vital bits of code around the event emitter stuff, am looking into instantiation of the event emitter class
Slight Change
So instead of EventEmitter
being inherited, i instantiated it by doing var emitter = new EventEmitter()
Then after that I got errors in the util.inspect
about the object having to be an object or be null.
No success yet.