Skip to content

Commit

Permalink
Merge pull request #109 from megawac/multi-subs
Browse files Browse the repository at this point in the history
ImproveTopic support for multiple subscriptions
  • Loading branch information
rctoris committed Oct 1, 2014
2 parents 4894b08 + c896c17 commit b17fd6a
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/core/Topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ function Topic(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 Message(data));
};
}
Topic.prototype.__proto__ = EventEmitter2.prototype;

/**
Expand All @@ -55,13 +59,10 @@ Topic.prototype.__proto__ = EventEmitter2.prototype;
* * message - the published message
*/
Topic.prototype.subscribe = function(callback) {
var that = this;
this.on('message', callback);
this.ros.on(this.name, function(data) {
var message = new 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 @@ -78,12 +79,14 @@ Topic.prototype.subscribe = function(callback) {
* all subscribe callbacks.
*/
Topic.prototype.unsubscribe = function() {
this.ros.removeAllListeners([this.name]);
if (!this.subscribeId) { return; }
this.ros.off(this.name, this._messageCallback);
this.ros.callOnConnection({
op: 'unsubscribe',
id: this.subscribeId,
topic: this.name
});
this.subscribeId = null;
};

/**
Expand Down

0 comments on commit b17fd6a

Please sign in to comment.