Provide backing variables instead of functions
This commit is contained in:
parent
20a67ef590
commit
5120c35f7c
49
lib/cylon.js
49
lib/cylon.js
|
@ -8,7 +8,6 @@
|
|||
|
||||
"use strict";
|
||||
|
||||
|
||||
require('./config');
|
||||
require('./utils');
|
||||
require('./logger');
|
||||
|
@ -35,18 +34,18 @@ var Cylon = (function() {
|
|||
// The Master class is our puppeteer that manages all the robots, as well as
|
||||
// starting them and the API.
|
||||
var Master = (function() {
|
||||
var robots = [],
|
||||
api = null,
|
||||
api_config = { host: '127.0.0.1', port: '3000' };
|
||||
|
||||
// Public: Creates a new Master
|
||||
//
|
||||
// Returns a Master instance
|
||||
function Master() {
|
||||
this.robot = bind(this.robot, this);
|
||||
|
||||
this.self = this;
|
||||
|
||||
this.api_instance = null;
|
||||
this.api_config = { host: '127.0.0.1', port: '3000' };
|
||||
this.robots = [];
|
||||
|
||||
this.robot = bind(this.robot, this);
|
||||
|
||||
if (process.platform === "win32") {
|
||||
var readline = require("readline"),
|
||||
io = { input: process.stdin, output: process.stdout }
|
||||
|
@ -80,17 +79,10 @@ var Cylon = (function() {
|
|||
|
||||
var robot = new Robot(opts);
|
||||
|
||||
robots.push(robot);
|
||||
this.robots.push(robot);
|
||||
return robot;
|
||||
};
|
||||
|
||||
// Public: Returns all Robots the Master knows about
|
||||
//
|
||||
// Returns an array of all Robot instances
|
||||
Master.prototype.robots = function() {
|
||||
return robots;
|
||||
};
|
||||
|
||||
// Public: Configures the API host and port based on passed options
|
||||
//
|
||||
// opts - object containing API options
|
||||
|
@ -101,10 +93,10 @@ var Cylon = (function() {
|
|||
Master.prototype.api = function(opts) {
|
||||
if (opts == null) { opts = {}; }
|
||||
|
||||
api_config.host = opts.host || "127.0.0.1";
|
||||
api_config.port = opts.port || "3000";
|
||||
this.api_config.host = opts.host || "127.0.0.1";
|
||||
this.api_config.port = opts.port || "3000";
|
||||
|
||||
return api_config;
|
||||
return this.api_config;
|
||||
};
|
||||
|
||||
// Public: Finds a particular robot by name
|
||||
|
@ -117,8 +109,8 @@ var Cylon = (function() {
|
|||
var error,
|
||||
robot = null;
|
||||
|
||||
for (var i = 0; i < robots.length; i++) {
|
||||
var bot = robots[i];
|
||||
for (var i = 0; i < this.robots.length; i++) {
|
||||
var bot = this.robots[i];
|
||||
if (bot.name === name) { robot = bot; }
|
||||
}
|
||||
|
||||
|
@ -183,8 +175,8 @@ var Cylon = (function() {
|
|||
Master.prototype.start = function() {
|
||||
this.startAPI();
|
||||
|
||||
for (var i = 0; i < robots.length; i++) {
|
||||
var robot = robots[i];
|
||||
for (var i = 0; i < this.robots.length; i++) {
|
||||
var robot = this.robots[i];
|
||||
robot.start();
|
||||
}
|
||||
};
|
||||
|
@ -193,8 +185,8 @@ var Cylon = (function() {
|
|||
//
|
||||
// Returns nothing
|
||||
Master.prototype.stop = function() {
|
||||
for (var i = 0; i < robots.length; i++) {
|
||||
var robot = robots[i];
|
||||
for (var i = 0; i < this.robots.length; i++) {
|
||||
var robot = this.robots[i];
|
||||
robot.stop();
|
||||
}
|
||||
};
|
||||
|
@ -205,8 +197,13 @@ var Cylon = (function() {
|
|||
// Returns an Cylon.ApiServer instance
|
||||
Master.prototype.startAPI = function() {
|
||||
var Server = require('./api');
|
||||
api_config.master = this.self;
|
||||
return api != null ? api : api = new Server(api_config);
|
||||
this.api_config.master = this.self;
|
||||
|
||||
if (this.api_instance === null) {
|
||||
this.api_instance = new Server(this.api_config);
|
||||
}
|
||||
|
||||
return this.api_instance;
|
||||
};
|
||||
|
||||
return Master;
|
||||
|
|
|
@ -44,7 +44,7 @@ describe("Cylon", function() {
|
|||
describe("#robots", function() {
|
||||
it("returns an array of all robots", function() {
|
||||
var robot, robots, _i, _len, _results;
|
||||
robots = Cylon.robots();
|
||||
robots = Cylon.robots;
|
||||
assert(robots instanceof Array);
|
||||
_results = [];
|
||||
for (_i = 0, _len = robots.length; _i < _len; _i++) {
|
||||
|
|
Loading…
Reference in New Issue