From dbde3d5b7d6ca12550532da4ab9fb071148567d7 Mon Sep 17 00:00:00 2001 From: Andrew Stewart Date: Mon, 9 Jun 2014 13:24:42 -0700 Subject: [PATCH] Remove circular reference from Robot --- lib/robot.js | 37 ++++++++++++++++++------------------- test/specs/robot.spec.js | 4 ---- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/lib/robot.js b/lib/robot.js index d0f0f0f..80e4940 100644 --- a/lib/robot.js +++ b/lib/robot.js @@ -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 diff --git a/test/specs/robot.spec.js b/test/specs/robot.spec.js index f0f6c94..a59c54e 100644 --- a/test/specs/robot.spec.js +++ b/test/specs/robot.spec.js @@ -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() {