Skip to content

Commit

Permalink
remove self and bind
Browse files Browse the repository at this point in the history
  • Loading branch information
LinusU committed Feb 15, 2016
1 parent c0360c1 commit 59b9911
Show file tree
Hide file tree
Showing 14 changed files with 444 additions and 476 deletions.
4 changes: 2 additions & 2 deletions lib/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Tessel.list = function(opts) {
.catch(function(err) {
if (!(err instanceof controller.HeuristicAmbiguityError)) {
return controller.closeTesselConnections(foundTessels)
.then(reject.bind(this, err));
.then(() => reject(err));
}
})
.then(function() {
Expand Down Expand Up @@ -239,7 +239,7 @@ Tessel.get = function(opts) {

} else {
controller.closeTesselConnections(tessels)
.then(reject.bind(this, err));
.then(() => reject(err));
}
});
});
Expand Down
33 changes: 15 additions & 18 deletions lib/dfu.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,32 +90,30 @@ DFU.prototype.dnloadChunk = function(blockNum, data, callback) {
};

DFU.prototype.dnload = function(data, callback, statuscb) {
var self = this;
var pos = 0;
var seq = 0;

function step() {
var step = () => {

if (typeof statuscb === 'function') {
statuscb(pos, data.length);
}

self.dnloadChunk(seq, data.slice(pos, pos + self.transferSize), function(error) {
this.dnloadChunk(seq, data.slice(pos, pos + this.transferSize), (error) => {
if (error) {
console.log('chunk error', error);
if (error) {
return self.handleError(error, callback);
return this.handleError(error, callback);
}
}

self.getStatus(function(error) {
this.getStatus((error) => {
if (error) {
console.log('status error', error);
return callback(error);
}

seq += 1;
pos += self.transferSize;
pos += this.transferSize;

if (pos < data.length) {
step();
Expand All @@ -125,12 +123,12 @@ DFU.prototype.dnload = function(data, callback, statuscb) {
statuscb(data.length, data.length);
}

self.dnloadChunk(seq, new Buffer(0), function(error) {
this.dnloadChunk(seq, new Buffer(0), (error) => {
if (error) {
return callback(error);
}

self.getStatus(function(error) {
this.getStatus((error) => {
if (error) {
return callback(error);
}
Expand All @@ -140,7 +138,8 @@ DFU.prototype.dnload = function(data, callback, statuscb) {
}
});
});
}
};

step();
};

Expand All @@ -149,27 +148,25 @@ DFU.prototype.uploadChunk = function(blockNum, length, callback) {
};

DFU.prototype.upload = function(callback) {
var self = this;
var buffers = [];
var seq = 0;

function step() {
self.uploadChunk(seq, self.transferSize, function(error, data) {

var step = () => {
this.uploadChunk(seq, this.transferSize, (error, data) => {
if (error) {
return self.handleError(error, callback);
return this.handleError(error, callback);
}

buffers.push(data);

if (data.length === self.transferSize) {
if (data.length === this.transferSize) {
seq += 1;
step();
} else {
callback(null, Buffer.concat(buffers));
}
});
}
};

step();
};

Expand Down
90 changes: 41 additions & 49 deletions lib/discover.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,73 +23,37 @@ function TesselSeeker() {
util.inherits(TesselSeeker, Emitter);

TesselSeeker.prototype.start = function(opts) {
var self = this;

// Initialize the opts if it wasn't provided
opts = opts || {};

// An array of pending open connections
var pendingOpen = [];

// If the user explicitly specified the usb flag,
// then cli should not filter on authorization status.
if (opts.usb && !opts.lan) {
opts.authorized = undefined;
}

// If no connection preference was supplied
if (!opts.usb && !opts.lan) {
// Default to all connections
opts.usb = opts.lan = true;
}

// If the user has specifically requested lan devices
if (opts.lan) {
debug('Will scan for LAN devices');
// Start the scan
self.lanScan = lan.startScan();
// When we get a connection, handle it
self.lanScan.on('connection', connectionHandler.bind(self));
}

// If the user has explicitly requested a USB search
if (opts.usb) {
debug('Will scan for USB devices');
// Start the scan
self.usbScan = usb.startScan();
// When we get a connection, handle it
self.usbScan.on('connection', connectionHandler.bind(self));
}

function connectionHandler(conn) {
var connectionHandler = (conn) => {
var tessel = new Tessel(conn);
var opening = tessel.connection.open()
.then(function() {
.then(() => {
if (opts.authorized !== undefined) {
if (tessel.connection.authorized !== opts.authorized) {
debug('Kicking ' + conn.host.slice(0, -6) + ' due to authorized filter: ' + opts.authorized);
return Promise.reject();
} else {
return true;
}
} else {
return true;
}
/*if (opts.authorized && !tessel.connection.authorized) {
debug('Kicking ' + conn.host.slice(0, -6) + ' due to authorized filter: ' + opts.authorized);
return;
}*/
})
.then(function() {
.then(() => {
debug('Connection opened:', tessel.connection.host);
if (conn.connectionType === 'LAN' && !conn.authorized) {
tessel.name = conn.host.slice(0, -6);
self.emit('tessel', tessel);
this.emit('tessel', tessel);
} else {
debug('Fetching name:', tessel.connection.host);
return tessel.getName()
.then(function() {
self.emit('tessel', tessel);
.then(() => {
this.emit('tessel', tessel);
return Promise.resolve();
});
}
Expand All @@ -101,24 +65,54 @@ TesselSeeker.prototype.start = function(opts) {
});
// Push this Promise into the pending array
pendingOpen.push(opening);
};

// If the user explicitly specified the usb flag,
// then cli should not filter on authorization status.
if (opts.usb && !opts.lan) {
opts.authorized = undefined;
}

// If no connection preference was supplied
if (!opts.usb && !opts.lan) {
// Default to all connections
opts.usb = opts.lan = true;
}

// If the user has specifically requested lan devices
if (opts.lan) {
debug('Will scan for LAN devices');
// Start the scan
this.lanScan = lan.startScan();
// When we get a connection, handle it
this.lanScan.on('connection', connectionHandler);
}

// If the user has explicitly requested a USB search
if (opts.usb) {
debug('Will scan for USB devices');
// Start the scan
this.usbScan = usb.startScan();
// When we get a connection, handle it
this.usbScan.on('connection', connectionHandler);
}

// If a timeout was provided
if (opts.timeout && typeof opts.timeout === 'number') {
// Set a timeout function
self.scanTimeout = setTimeout(function() {
this.scanTimeout = setTimeout(() => {
debug('Timeout hit! Waiting for pending to finish...');
// Once all the pending open commands complete
Promise.all(pendingOpen)
// Stop the scan (which emits 'end')
.then(function() {
.then(() => {
debug('Done! Stopping.');
self.stop();
this.stop();
});
}, opts.timeout);
}

return self;
return this;
};

TesselSeeker.prototype.stop = function() {
Expand All @@ -140,9 +134,7 @@ TesselSeeker.prototype.stop = function() {
// Clear that timeout
clearTimeout(this.scanTimeout);
// Emit that this scan stopped
setImmediate(function() {
this.emit('end');
}.bind(this));
setImmediate(() => this.emit('end'));
}

return this;
Expand Down
52 changes: 24 additions & 28 deletions lib/lan_connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,36 +80,34 @@ LAN.Connection.prototype.exec = function(command, options, callback) {
};

LAN.Connection.prototype.end = function() {
var self = this;
return new Promise(function(resolve) {
return new Promise((resolve) => {
// End the SSH connection
self.ssh.end();
this.ssh.end();
resolve();
});
};

LAN.Connection.prototype.open = function() {
var self = this;
return new Promise(function(resolve) {
return new Promise((resolve) => {
// Create a new SSH client connection object
self.ssh = new ssh.Client();
this.ssh = new ssh.Client();
// Open up an SSH Connection
self.ssh.on('ready', function() {
debug('Device ready:', self.host);
this.ssh.on('ready', () => {
debug('Device ready:', this.host);
// Tessel allows connection
self.authorized = true;
this.authorized = true;
// Resolve with connection
resolve(self);
resolve(this);
});
self.ssh.on('error', function() {
debug('Device open error:', self.host);
this.ssh.on('error', () => {
debug('Device open error:', this.host);
// Tessel does not allow connection
self.authorized = false;
this.authorized = false;
// Reject with error
resolve(self);
}).connect(self.auth);
self.ssh.once('close', function() {
self.closed = true;
resolve(this);
}).connect(this.auth);
this.ssh.once('close', () => {
this.closed = true;
});
});
};
Expand Down Expand Up @@ -155,35 +153,33 @@ LAN.Scanner = function() {
util.inherits(LAN.Scanner, Emitter);

LAN.Scanner.prototype.start = function() {
var self = this;

global.setImmediate(function startScanning() {
self.browser = mdns.createBrowser('_tessel._tcp');
setImmediate(() => {
this.browser = mdns.createBrowser('_tessel._tcp');

// When the browser becomes ready
self.browser.once('ready', function() {
this.browser.once('ready', () => {
try {
// Start discovering Tessels
self.browser.discover();
this.browser.discover();
} catch (err) {
self.emit('error', err);
this.emit('error', err);
}
});

// When the browser finds a new device
self.browser.on('update', function(data) {
this.browser.on('update', (data) => {
try {
debug('Device found:', data.fullname);
// Check if it's a Tessel
if (self.discovered.indexOf(data) > -1) {
if (this.discovered.indexOf(data) > -1) {
return;
}

var connection = new LAN.Connection(data);

self.emit('connection', connection);
this.emit('connection', connection);
} catch (err) {
self.emit('error', err);
this.emit('error', err);
}
});
});
Expand Down

0 comments on commit 59b9911

Please sign in to comment.