Skip to content

Commit

Permalink
Merge pull request #310 from koush/master
Browse files Browse the repository at this point in the history
bug fixes in events
  • Loading branch information
agsh authored Feb 26, 2024
2 parents 7ed460e + 5641b57 commit de0000d
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@ module.exports = function(Cam) {
try {
urlAddress = this.events.subscription.subscriptionReference.address;
} catch (e) {
throw new Error('You should create pull-point subscription first!');
if (callback && callback.call) {
callback.call(this, new Error('You should create pull-point subscription first!'));
}
return;
}

try {
Expand Down Expand Up @@ -225,7 +228,10 @@ module.exports = function(Cam) {
try {
urlAddress = this.events.subscription.subscriptionReference.address;
} catch (e) {
throw new Error('You should create pull-point subscription first!');
if (callback && callback.call) {
callback.call(this, new Error('You should create pull-point subscription first!'));
}
return;
}

try {
Expand Down Expand Up @@ -267,13 +273,16 @@ module.exports = function(Cam) {
* @param {Cam~PullMessagesResponse} callback
* @throws {Error} {@link Cam#events.subscription} must exists
*/
Cam.prototype.unsubscribe = function(callback) {
Cam.prototype.unsubscribe = function(callback, preserveListeners) {
let urlAddress = null;
let subscriptionId = null;
try {
urlAddress = this.events.subscription.subscriptionReference.address;
} catch (e) {
throw new Error('You should create pull-point subscription first!');
if (callback && callback.call) {
callback.call(this, new Error('You should create pull-point subscription first!'));
}
return;
}

try {
Expand Down Expand Up @@ -303,7 +312,9 @@ module.exports = function(Cam) {
body: sendXml
}, function(err, res, xml) {
if (!err) {
this.removeAllListeners('event'); // We can subscribe again only if there is no 'event' listener
if (!preserveListeners) {
this.removeAllListeners('event'); // We can subscribe again only if there is no 'event' listener
}
var data = linerase(res).unsubscribeResponse;
}
if (callback && callback.call) {
Expand Down Expand Up @@ -383,10 +394,10 @@ module.exports = function(Cam) {
});
} else {
// there was an error pulling the message
this.unsubscribe({}, function(_err,_data,_xml) {
this.unsubscribe(function(_err,_data,_xml) {
// once the unsubsribe has completed (even if it failed), go around the loop again
this._eventRequest();
});
}, true);
}
}.bind(this));
} else {
Expand Down

0 comments on commit de0000d

Please sign in to comment.