Skip to content

Better chaining API + object for config #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -205,6 +217,6 @@ pcmdOptions.forEach(function(pair) {
this._pcmd[pair[1]] = speed;
delete this._pcmd[pair[0]];

return speed;
return this;
};
});
19 changes: 19 additions & 0 deletions test/unit/test-Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down