Skip to content

Commit

Permalink
Merge pull request #181 from agsh/revert-event-emitter
Browse files Browse the repository at this point in the history
Make Cam works properly like EventEmitter inheritor
  • Loading branch information
agsh authored Jan 14, 2021
2 parents fe5d7f3 + 0904014 commit 38a75a9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 58 deletions.
70 changes: 15 additions & 55 deletions lib/cam.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const http = require('http')
, crypto = require('crypto')
, events = require('events')
, url = require('url')
, util = require('util')
, linerase = require('./utils').linerase
, parseSOAPString = require('./utils').parseSOAPString
, emptyFn = function() {}
Expand Down Expand Up @@ -68,6 +69,7 @@ const http = require('http')
* });
*/
var Cam = function(options, callback) {
events.EventEmitter.call(this);
callback = callback || emptyFn;
this.hostname = options.hostname;
this.username = options.username;
Expand All @@ -83,69 +85,27 @@ var Cam = function(options, callback) {
this.preserveAddress = options.preserveAddress || false;

this.events = {};
/**
* Bind event handling to the `event` event
*/
this.on('newListener', function(name) {
// if this is the first listener, start pulling subscription
if (name === 'event' && this.listeners(name).length === 0) {
setImmediate(function() {
this._eventRequest();
}.bind(this));
}
}.bind(this));

if (options.autoconnect !== false) {
setImmediate(function() {
this.connect(callback);
}.bind(this));
}
this.eventEmitter = new events.EventEmitter();
this.eventEmitter.on('newListener', function(name) {
// if this is the first listener, start pulling
if (name === 'event' && this.eventEmitter.listeners(name).length === 0) {
// setImmediate needed because this.eventEmitter.listeners('event').length is used in _eventRequest but
// is increased AFTER 'newListener' event is executed
setImmediate(this._eventRequest.bind(this));
}
}.bind(this));
};

// events.EventEmitter inheritance
// util.inherits(Cam, events.EventEmitter); // Do not inherit! Because the EventEmitter becomes static (same for all instances of Cam object)
Cam.prototype.addListener = function(eventName, listener) {
return this.eventEmitter.addListener(eventName, listener);
}
Cam.prototype.emit = function(event,...data) {
return this.eventEmitter.emit(event,...data);
}
Cam.prototype.eventNames = function() {
return this.eventEmitter.eventNames();
}
Cam.prototype.getMaxListeners = function() {
return this.eventEmitter.getMaxListeners();
}
Cam.prototype.listenerCount = function(eventName) {
return this.eventEmitter.listenerCount(eventName);
}
Cam.prototype.listeners = function(eventName) {
return this.eventEmitter.listeners(eventName);
}
Cam.prototype.off = function(eventName, listener) {
return this.eventEmitter.off(eventName, listener);
}
Cam.prototype.on = function(eventName,listener) {
return this.eventEmitter.on(eventName,listener);
}
Cam.prototype.once = function(eventName, listener) {
return this.eventEmitter.once(eventName, listener);
}
Cam.prototype.prependListener = function(eventName, listener) {
return this.eventEmitter.prependListener(eventName, listener);
}
Cam.prototype.prependOnceListener = function(eventName, listener) {
return this.eventEmitter.prependOnceListener(eventName, listener);
}
Cam.prototype.removeAllListeners = function(eventName) {
return this.eventEmitter.removeAllListeners(eventName);
}
Cam.prototype.removeListener = function(eventName, listener) {
return this.eventEmitter.removeListener(eventName,listener);
}
Cam.prototype.setMaxListeners = function(n) {
return this.eventEmitter.setMaxListeners(n);
}
Cam.prototype.rawListeners = function(eventName) {
return this.eventEmitter.rawListeners(eventName);
}
util.inherits(Cam, events.EventEmitter);

/**
* Connect to the camera and fill device information properties
Expand Down
6 changes: 3 additions & 3 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ module.exports = function(Cam) {
body: sendXml
}, function(err, res, xml) {
if (!err) {
this.eventEmitter.removeAllListeners('event'); // We can subscribe again only if there is no 'event' listener
this.removeAllListeners('event'); // We can subscribe again only if there is no 'event' listener
var data = linerase(res).unsubscribeResponse;
}
if (callback) {
Expand All @@ -283,7 +283,7 @@ module.exports = function(Cam) {
* @private
*/
Cam.prototype._eventRequest = function() {
if (this.eventEmitter.listeners('event').length) { // check for event listeners, if zero, stop pulling
if (this.listeners('event').length) { // check for event listeners, if zero, stop pulling
this.events.timeout = this.events.timeout || 30000; // setting timeout
this.events.messageLimit = this.events.messageLimit || 10; // setting message limit
if (!this.events.subscription || !this.events.terminationTime || (this.events.terminationTime < Date.now() + this.events.timeout)) {
Expand All @@ -305,7 +305,7 @@ module.exports = function(Cam) {
* @throws {Error} {@link Cam#events.subscription} must exists
*/
Cam.prototype._eventPull = function() {
if (this.eventEmitter.listeners('event').length) { // check for event listeners, if zero, stop pulling
if (this.listeners('event').length) { // check for event listeners, if zero, stop pulling
this.pullMessages({
messageLimit: this.events.messageLimit
}, function(err, data, xml) {
Expand Down

0 comments on commit 38a75a9

Please sign in to comment.