From 4ec19961f83d310eddbdcfc1b39eb0439d92173d Mon Sep 17 00:00:00 2001 From: deadprogram Date: Tue, 1 Sep 2015 18:12:39 -0700 Subject: [PATCH] Devices will only be started up once, and can be added dynamically --- lib/robot.js | 6 ++++++ spec/lib/robot.spec.js | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/lib/robot.js b/lib/robot.js index 00e8b95..20fd764 100644 --- a/lib/robot.js +++ b/lib/robot.js @@ -319,6 +319,10 @@ Robot.prototype.startDevices = function(callback) { this[name] = device; return function(cb) { + if (device.started === true) { + return cb.call(device); + } + var str = "Starting device '" + name + "'"; if (device.pin) { @@ -326,6 +330,8 @@ Robot.prototype.startDevices = function(callback) { } log(str + "."); + device.started = true; + return device.start.call(device, cb); }; }, this); diff --git a/spec/lib/robot.spec.js b/spec/lib/robot.spec.js index 2d8ef2b..c71f517 100644 --- a/spec/lib/robot.spec.js +++ b/spec/lib/robot.spec.js @@ -442,6 +442,25 @@ describe("Robot", function() { expect(bot.devices.alpha.start).to.be.called; expect(bot.devices.bravo.start).to.be.called; }); + + it("runs #start on each device only once", function() { + bot.startDevices(); + bot.startDevices(); + + expect(bot.devices.alpha.start).to.be.called.once; + expect(bot.devices.bravo.start).to.be.called.once; + }); + + it("runs #start on a newly added device", function() { + bot.startDevices(); + bot.device("charlie", { driver: "ping" }); + stub(bot.devices.charlie, "start").returns(true); + bot.startDevices(); + + expect(bot.devices.alpha.start).to.be.called.once; + expect(bot.devices.bravo.start).to.be.called.once; + expect(bot.devices.charlie.start).to.be.called.once; + }); }); describe("#halt", function() {