Remove circular reference from Robot

This commit is contained in:
Andrew Stewart 2014-06-09 13:24:42 -07:00
parent b1d7dd8186
commit dbde3d5b7d
2 changed files with 18 additions and 23 deletions

View File

@ -67,7 +67,6 @@ module.exports = Robot = function Robot(opts) {
this[method] = Utils.bind(this[method], this);
}
this.robot = this;
this.name = opts.name || this.constructor.randomName();
this.connections = {};
@ -94,7 +93,7 @@ module.exports = Robot = function Robot(opts) {
var func = opts[n],
reserved = ['connection', 'connections', 'device', 'devices', 'work'];
if (reserved.indexOf(n) < 0) { this.robot[n] = func; }
if (reserved.indexOf(n) < 0) { this[n] = func; }
}
};
@ -191,14 +190,14 @@ Robot.prototype._createDevice = function(device) {
Robot.prototype.start = function() {
var self = this;
return this.startConnections(function() {
return self.robot.startDevices(function(err) {
return self.startDevices(function(err) {
if (err) {
throw err;
}else{
self.robot.work.call(self.robot, self.robot);
self.work.call(self, self);
self.running = true;
Logger.info("Working...");
self.robot.emit('working');
self.emit('working');
}
});
});
@ -216,7 +215,7 @@ Robot.prototype.startConnections = function(callback) {
for (var n in this.connections) {
var connection = this.connections[n];
this.robot[n] = connection;
this[n] = connection;
starters[n] = connection.connect;
}
@ -235,7 +234,7 @@ Robot.prototype.startDevices = function(callback) {
for (var n in this.devices) {
var device = this.devices[n];
this.robot[n] = device;
this[n] = device;
starters[n] = device.start;
}
@ -261,14 +260,14 @@ Robot.prototype.halt = function() {
Robot.prototype.initAdaptor = function(adaptorName, connection, opts) {
if (opts == null) { opts = {}; }
var adaptor = this.robot.requireAdaptor(adaptorName, opts).adaptor({
var adaptor = this.requireAdaptor(adaptorName, opts).adaptor({
name: adaptorName,
connection: connection,
extraParams: opts
});
if (config.testing_mode) {
var testAdaptor = this.robot.requireAdaptor('test').adaptor({
var testAdaptor = this.requireAdaptor('test').adaptor({
name: adaptorName,
connection: connection,
extraParams: opts
@ -285,12 +284,12 @@ Robot.prototype.initAdaptor = function(adaptorName, connection, opts) {
//
// Returns the module for the adaptor
Robot.prototype.requireAdaptor = function(adaptorName, opts) {
if (this.robot.adaptors[adaptorName] == null) {
if (this.adaptors[adaptorName] == null) {
var moduleName = opts.module || adaptorName;
this.robot.registerAdaptor("cylon-" + moduleName, adaptorName);
this.robot.adaptors[adaptorName].register(this);
this.registerAdaptor("cylon-" + moduleName, adaptorName);
this.adaptors[adaptorName].register(this);
}
return this.robot.adaptors[adaptorName];
return this.adaptors[adaptorName];
};
// Public: Registers an Adaptor with the Robot
@ -323,14 +322,14 @@ Robot.prototype.registerAdaptor = function(moduleName, adaptorName) {
Robot.prototype.initDriver = function(driverName, device, opts) {
if (opts == null) { opts = {}; }
var driver = this.robot.requireDriver(driverName).driver({
var driver = this.requireDriver(driverName).driver({
name: driverName,
device: device,
extraParams: opts
});
if (config.testing_mode) {
var testDriver = this.robot.requireDriver('test').driver({
var testDriver = this.requireDriver('test').driver({
name: driverName,
device: device,
extraParams: opts
@ -348,11 +347,11 @@ Robot.prototype.initDriver = function(driverName, device, opts) {
//
// Returns the module for driver
Robot.prototype.requireDriver = function(driverName) {
if (this.robot.drivers[driverName] == null) {
this.robot.registerDriver("cylon-" + driverName, driverName);
this.robot.drivers[driverName].register(this);
if (this.drivers[driverName] == null) {
this.registerDriver("cylon-" + driverName, driverName);
this.drivers[driverName].register(this);
}
return this.robot.drivers[driverName];
return this.drivers[driverName];
};
// Public: Registers an Driver with the Robot

View File

@ -20,10 +20,6 @@ describe("Robot", function() {
});
describe("constructor", function() {
it("sets a @robot variable as a circular reference to the robot", function() {
expect(robot.robot).to.be.eql(robot);
});
describe("name", function() {
context("if provided", function() {
it("is set to the passed value", function() {