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

View File

@ -20,10 +20,6 @@ describe("Robot", function() {
}); });
describe("constructor", 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() { describe("name", function() {
context("if provided", function() { context("if provided", function() {
it("is set to the passed value", function() { it("is set to the passed value", function() {