diff --git a/src/core/Topic.js b/src/core/Topic.js index 937294f5e..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,28 +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.ros.idCounter++; - var subscribeId = 'subscribe:' + this.name + ':' + this.ros.idCounter; - var call = { - op : 'subscribe', - id : 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 + }); }; /** @@ -80,15 +74,12 @@ ROSLIB.Topic.prototype.subscribe = function(callback) { * all subscribe callbacks. */ 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, - topic : this.name - }; - this.ros.callOnConnection(call); + this.ros.removeAllListeners([this.name]); + this.ros.callOnConnection({ + op: 'unsubscribe', + id: this.subscribeId, + topic: this.name + }); }; /** @@ -98,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; }; @@ -119,13 +108,11 @@ ROSLIB.Topic.prototype.unadvertise = function() { if (!this.isAdvertised) { return; } - var unadvertiseId = this.advertiseId; - var call = { - op : 'unadvertise', - id : unadvertiseId, - topic : this.name - }; - this.ros.callOnConnection(call); + this.ros.callOnConnection({ + op: 'unadvertise', + id: this.advertiseId, + topic: this.name + }); this.isAdvertised = false; }; @@ -136,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