Skip to content

Commit

Permalink
Led, Led.RGB: Move interval to private state
Browse files Browse the repository at this point in the history
  • Loading branch information
scottgonzalez committed May 25, 2015
1 parent 263f308 commit 397383f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 23 deletions.
16 changes: 7 additions & 9 deletions lib/led/led.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,14 @@ function Led(opts) {

Object.defineProperties(this, controller);

// LED instance properties
this.interval = null;

state = {
isOn: false,
isRunning: false,
value: null,
direction: 1,
mode: null,
isAnode: opts.isAnode
isAnode: opts.isAnode,
interval: null
};

priv.set(this, state);
Expand Down Expand Up @@ -233,7 +231,7 @@ Led.prototype.on = function() {
}

// ...there is no active interval
if (!this.interval) {
if (!state.interval) {
state.value = 255;
}

Expand Down Expand Up @@ -437,8 +435,8 @@ Led.prototype.strobe = function(rate, callback) {
var state = priv.get(this);

// Avoid traffic jams
if (this.interval) {
clearInterval(this.interval);
if (state.interval) {
clearInterval(state.interval);
}

if (typeof rate === "function") {
Expand All @@ -448,7 +446,7 @@ Led.prototype.strobe = function(rate, callback) {

state.isRunning = true;

this.interval = setInterval(function() {
state.interval = setInterval(function() {
this.toggle();
if (typeof callback === "function") {
callback();
Expand All @@ -467,7 +465,7 @@ Led.prototype.blink = Led.prototype.strobe;
Led.prototype.stop = function() {
var state = priv.get(this);

clearInterval(this.interval);
clearInterval(state.interval);

if (state.animation) {
state.animation.stop();
Expand Down
21 changes: 12 additions & 9 deletions lib/led/rgb.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,13 @@ var RGB = function(opts) {

Object.defineProperties(this, controller);

this.interval = null;

// The default color is #ffffff, but the light will be off
state = {
red: 255,
green: 255,
blue: 255,
isAnode: opts.isAnode || false
isAnode: opts.isAnode || false,
interval: null
};

priv.set(this, state);
Expand All @@ -171,7 +170,7 @@ var RGB = function(opts) {
},
isRunning: {
get: function() {
return !!this.interval;
return !!state.interval;
}
},
isAnode: {
Expand Down Expand Up @@ -323,12 +322,14 @@ RGB.prototype.off = function() {
* @return {Led}
*/
RGB.prototype.strobe = function(rate) {
var state = priv.get(this);

// Avoid traffic jams
if (this.interval) {
clearInterval(this.interval);
if (state.interval) {
clearInterval(state.interval);
}

this.interval = setInterval(this.toggle.bind(this), rate || 100);
state.interval = setInterval(this.toggle.bind(this), rate || 100);

return this;
};
Expand All @@ -340,8 +341,10 @@ RGB.prototype.toggle = function() {
};

RGB.prototype.stop = function() {
clearInterval(this.interval);
delete this.interval;
var state = priv.get(this);

clearInterval(state.interval);
delete state.interval;

return this;
};
Expand Down
6 changes: 1 addition & 5 deletions test/led.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ var instanceProperties = [{
name: "pin"
}, {
name: "value"
}, {
name: "interval"
}];

var rgbProtoProperties = [{
Expand All @@ -54,9 +52,7 @@ var rgbProtoProperties = [{
name: "stop"
}];

var rgbInstanceProperties = [{
name: "interval"
}];
var rgbInstanceProperties = [];

function newBoard() {
var io = new MockFirmata();
Expand Down

0 comments on commit 397383f

Please sign in to comment.