diff --git a/lib/Client.js b/lib/Client.js index 839aec1..6255a7f 100644 --- a/lib/Client.js +++ b/lib/Client.js @@ -53,6 +53,7 @@ Client.prototype.resume = function() { this._udpNavdatasStream .on('error', this._maybeEmitError.bind(this)) .on('data', this._handleNavdata.bind(this)); + return this; }; Client.prototype._handleNavdata = function(navdata) { @@ -134,30 +135,39 @@ Client.prototype._sendCommands = function() { Client.prototype.disableEmergency = function() { this._disableEmergency = true; + return this; }; Client.prototype.takeoff = function() { this._ref.fly = true; - return true; + return this; }; Client.prototype.land = function() { this._ref.fly = false; - return true; + return this; }; Client.prototype.stop = function() { this._pcmd = {}; - return true; + return this; }; Client.prototype.config = function(key, value) { + if (typeof key === "object") { + for (var k in key) { + this.config(k, key[k]); + } + return this; + } + // @TODO Figure out if we can get a ACK for this, so we don't need to // repeat it blindly like this var self = this; this._repeat(10, function() { self._udpControl.config(key, value); }); + return this; }; Client.prototype.animate = function(animation, duration) { @@ -167,6 +177,7 @@ Client.prototype.animate = function(animation, duration) { this._repeat(10, function() { self._udpControl.animate(animation, duration); }); + return this; }; Client.prototype.animateLeds = function(animation, hz, duration) { @@ -176,6 +187,7 @@ Client.prototype.animateLeds = function(animation, hz, duration) { this._repeat(10, function() { self._udpControl.animateLeds(animation, hz, duration); }); + return this; }; Client.prototype._repeat = function(times, fn) { @@ -196,7 +208,7 @@ pcmdOptions.forEach(function(pair) { this._pcmd[pair[0]] = speed; delete this._pcmd[pair[1]]; - return speed; + return this; }; Client.prototype[pair[1]] = function(speed) { @@ -205,6 +217,6 @@ pcmdOptions.forEach(function(pair) { this._pcmd[pair[1]] = speed; delete this._pcmd[pair[0]]; - return speed; + return this; }; }); diff --git a/test/unit/test-Client.js b/test/unit/test-Client.js index 188dde7..b03e91f 100644 --- a/test/unit/test-Client.js +++ b/test/unit/test-Client.js @@ -351,6 +351,25 @@ test('Client', { assert.equal(args[1], 'bar'); }, + 'config(): with object sends config command 10 times': function() { + this.client.resume(); + this.client.config({'foo': 'bar'}); + + for (var i = 1; i <= 10; i++) { + this.clock.tick(30); + assert.equal(this.fakeUdpControl.config.callCount, i); + } + + // Stop repeating after 10 intervals + this.clock.tick(30); + assert.equal(this.fakeUdpControl.config.callCount, 10); + + // Check that the arguments were right + var args = this.fakeUdpControl.config.getCall(0).args; + assert.equal(args.length, 2); + assert.equal(args[0], 'foo'); + assert.equal(args[1], 'bar'); + }, 'animateLeds(): sends config command 10 times': function() { this.client.resume();