From 30adfeb4b9c3c27447c2cc9335a448662ed1eeda Mon Sep 17 00:00:00 2001 From: Graeme Yeates Date: Sun, 21 Sep 2014 13:51:44 -0400 Subject: [PATCH 1/2] Use the correct id when unsubbing a Topic --- src/core/Topic.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/core/Topic.js b/src/core/Topic.js index 937294f5e..75a50c13e 100644 --- a/src/core/Topic.js +++ b/src/core/Topic.js @@ -61,11 +61,10 @@ ROSLIB.Topic.prototype.subscribe = function(callback) { that.emit('message', message); }); - this.ros.idCounter++; - var subscribeId = 'subscribe:' + this.name + ':' + this.ros.idCounter; + this.subscribeId = 'subscribe:' + this.name + ':' + (++this.ros.idCounter); var call = { op : 'subscribe', - id : subscribeId, + id : this.subscribeId, type : this.messageType, topic : this.name, compression : this.compression, @@ -81,11 +80,10 @@ ROSLIB.Topic.prototype.subscribe = function(callback) { */ ROSLIB.Topic.prototype.unsubscribe = function() { this.ros.removeAllListeners([ this.name ]); - this.ros.idCounter++; - var unsubscribeId = 'unsubscribe:' + this.name + ':' + this.ros.idCounter; + var call = { op : 'unsubscribe', - id : unsubscribeId, + id : this.subscribeId, topic : this.name }; this.ros.callOnConnection(call); @@ -119,10 +117,9 @@ ROSLIB.Topic.prototype.unadvertise = function() { if (!this.isAdvertised) { return; } - var unadvertiseId = this.advertiseId; var call = { op : 'unadvertise', - id : unadvertiseId, + id : this.advertiseId, topic : this.name }; this.ros.callOnConnection(call); From 6c230e06ada923f1fb20380af9bf52c6e8c05247 Mon Sep 17 00:00:00 2001 From: Graeme Yeates Date: Sun, 21 Sep 2014 13:53:24 -0400 Subject: [PATCH 2/2] Simplify core/Topic --- src/core/Topic.js | 89 +++++++++++++++++++++-------------------------- 1 file changed, 39 insertions(+), 50 deletions(-) diff --git a/src/core/Topic.js b/src/core/Topic.js index 75a50c13e..7806f40c1 100644 --- a/src/core/Topic.js +++ b/src/core/Topic.js @@ -29,7 +29,8 @@ ROSLIB.Topic = function(options) { this.queue_size = options.queue_size || 100; // Check for valid compression types - if (this.compression && this.compression !== 'png' && this.compression !== 'none') { + if (this.compression && this.compression !== 'png' && this.compression !== + 'none') { this.emit('warning', this.compression + ' compression is not supported. No compression will be used.'); } @@ -51,27 +52,21 @@ ROSLIB.Topic.prototype.__proto__ = EventEmitter2.prototype; */ ROSLIB.Topic.prototype.subscribe = function(callback) { var that = this; - - this.on('message', function(message) { - callback(message); - }); - + this.on('message', callback); this.ros.on(this.name, function(data) { var message = new ROSLIB.Message(data); that.emit('message', message); }); - this.subscribeId = 'subscribe:' + this.name + ':' + (++this.ros.idCounter); - var call = { - op : 'subscribe', - id : this.subscribeId, - type : this.messageType, - topic : this.name, - compression : this.compression, - throttle_rate : this.throttle_rate - }; - - this.ros.callOnConnection(call); + this.id = 'subscribe:' + this.name + ':' + (++this.ros.idCounter); + this.ros.callOnConnection({ + op: 'subscribe', + id: this.id, + type: this.messageType, + topic: this.name, + compression: this.compression, + throttle_rate: this.throttle_rate + }); }; /** @@ -79,14 +74,12 @@ ROSLIB.Topic.prototype.subscribe = function(callback) { * all subscribe callbacks. */ ROSLIB.Topic.prototype.unsubscribe = function() { - this.ros.removeAllListeners([ this.name ]); - - var call = { - op : 'unsubscribe', - id : this.subscribeId, - topic : this.name - }; - this.ros.callOnConnection(call); + this.ros.removeAllListeners([this.name]); + this.ros.callOnConnection({ + op: 'unsubscribe', + id: this.subscribeId, + topic: this.name + }); }; /** @@ -96,17 +89,15 @@ ROSLIB.Topic.prototype.advertise = function() { if (this.isAdvertised) { return; } - this.ros.idCounter++; - this.advertiseId = 'advertise:' + this.name + ':' + this.ros.idCounter; - var call = { - op : 'advertise', - id : this.advertiseId, - type : this.messageType, - topic : this.name, - latch : this.latch, - queue_size : this.queue_size - }; - this.ros.callOnConnection(call); + this.advertiseId = 'advertise:' + this.name + ':' + (++this.ros.idCounter); + this.ros.callOnConnection({ + op: 'advertise', + id: this.advertiseId, + type: this.messageType, + topic: this.name, + latch: this.latch, + queue_size: this.queue_size + }); this.isAdvertised = true; }; @@ -117,12 +108,11 @@ ROSLIB.Topic.prototype.unadvertise = function() { if (!this.isAdvertised) { return; } - var call = { - op : 'unadvertise', - id : this.advertiseId, - topic : this.name - }; - this.ros.callOnConnection(call); + this.ros.callOnConnection({ + op: 'unadvertise', + id: this.advertiseId, + topic: this.name + }); this.isAdvertised = false; }; @@ -133,17 +123,16 @@ ROSLIB.Topic.prototype.unadvertise = function() { */ ROSLIB.Topic.prototype.publish = function(message) { if (!this.isAdvertised) { - this.advertise(); + this.advertise(); } this.ros.idCounter++; - var publishId = 'publish:' + this.name + ':' + this.ros.idCounter; var call = { - op : 'publish', - id : publishId, - topic : this.name, - msg : message, - latch : this.latch + op: 'publish', + id: 'publish:' + this.name + ':' + this.ros.idCounter, + topic: this.name, + msg: message, + latch: this.latch }; this.ros.callOnConnection(call); -}; +}; \ No newline at end of file