Skip to content

Commit

Permalink
unsubscribe should not remove other topic's listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
gyeates committed Sep 30, 2014
1 parent 9759ea7 commit 6faea60
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/core/Topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ ROSLIB.Topic = function(options) {
this.emit('warning', this.throttle_rate + ' is not allowed. Set to 0');
this.throttle_rate = 0;
}

var that = this;
this._messageCallback = function(data) {
that.emit('message', new ROSLIB.Message(data));
};
};
ROSLIB.Topic.prototype.__proto__ = EventEmitter2.prototype;

Expand All @@ -51,14 +56,10 @@ ROSLIB.Topic.prototype.__proto__ = EventEmitter2.prototype;
* * message - the published message
*/
ROSLIB.Topic.prototype.subscribe = function(callback) {
var that = this;
this.on('message', callback);
this.ros.on(this.name, function(data) {
var message = new ROSLIB.Message(data);
that.emit('message', message);
});

if (!this.subscribeId) { return; }
this.ros.on(this.name, this._messageCallback);
this.subscribeId = 'subscribe:' + this.name + ':' + (++this.ros.idCounter);
this.ros.callOnConnection({
op: 'subscribe',
Expand All @@ -76,7 +77,7 @@ ROSLIB.Topic.prototype.subscribe = function(callback) {
*/
ROSLIB.Topic.prototype.unsubscribe = function() {
if (!this.subscribeId) { return; }
this.ros.removeAllListeners([this.name]);
this.ros.off(this.name, this._messageCallback);
this.ros.callOnConnection({
op: 'unsubscribe',
id: this.subscribeId,
Expand Down

0 comments on commit 6faea60

Please sign in to comment.