Skip to content
Permalink
Browse files

Additional optimization

Added logic to retrieve only the items enabled
added and newly disabled during a turn. 
Introduces a bug related to enabledCells being
wrong when "Stop" is clicked. Bug fix will be
delayed by refactoring
  • Loading branch information...
Daniel Deming
Daniel Deming committed Mar 9, 2014
1 parent 3cbb462 commit b3b7c51c0bf1b4bdc4fc5154c915769ff85fe1f1
Showing with 95 additions and 12 deletions.
  1. +39 −0 spec/LifeSpec.js
  2. +42 −0 src/life.js
  3. +14 −12 src/lifedom.js
@@ -102,6 +102,45 @@ describe('Life class', function() {

});

describe('getNewEnabled', function() {
beforeEach(function() {
life.init(defaultInitArgs);
});

it('should be defined', function() {
expect(typeof life.getNewEnabled).toBe('function');
});

it('should return only the new enabled plots', function() {
life.doTurn();
life.doTurn();
expect(life.getNewEnabled()['x3'].length).toBe(2);
expect(life.getNewEnabled()['x3'][0]).toBe(2);
expect(life.getNewEnabled()['x3'][1]).toBe(4);
});

});

describe('getNewDisabled', function() {
beforeEach(function() {
life.init(defaultInitArgs);
});

it('should be defined', function() {
expect(typeof life.getNewDisabled).toBe('function');
});

it('should return only the new enabled plots', function() {
life.doTurn();
life.doTurn();
expect(life.getNewDisabled()['x2'].length).toBe(1);
expect(life.getNewDisabled()['x4'].length).toBe(1);
expect(life.getNewDisabled()['x2'][0]).toBe(3);
expect(life.getNewDisabled()['x4'][0]).toBe(3);
});

});

describe('doTurn', function() {
beforeEach(function() {
life.init(defaultInitArgs);
@@ -23,6 +23,48 @@ var life = (function life() {
return priorOn;
},

/**
* Returns only items present in currentOn that are missing
* from priorOn. These are items newly enabled.
*/
getNewEnabled: function() {
var newEnabled = {};

_.each(currentOn, function(yAxes, key) {
_.each(yAxes, function(yAxis) {
if (priorOn[key] && priorOn[key].indexOf(yAxis)!==-1) {
return;
} else {
if (!newEnabled[key]) newEnabled[key] = [];
newEnabled[key].push(yAxis);
}
});
});

return newEnabled;
},

/**
* Returns only items present in priorOn that are missing
* from currentOn. These are items newly disabled.
*/
getNewDisabled: function() {
var newDisabled = {};

_.each(priorOn, function(yAxes, key) {
_.each(yAxes, function(yAxis) {
if (currentOn[key] && currentOn[key].indexOf(yAxis)!==-1) {
return;
} else {
if (!newDisabled[key]) newDisabled[key] = [];
newDisabled[key].push(yAxis);
}
});
});

return newDisabled;
},

/**
* Counts the number of adjacent cells that are enabled,
* comparing the passed plot points against currentOn.
@@ -168,17 +168,17 @@ var lifedom = (function lifedom(life) {
playVal,
speed = $form.find('#speed').val();

if (!subsequentCall) {
if (!subsequentCall) { // initial call of play()
playVal = ($play.val() === 'Play') ? 'Stop' : 'Play';
$play.val(playVal);

if (playVal === 'Play') {
window.clearTimeout(this.playing);
return true;
}
}

this.syncDomToLife();
this.syncDomToLife();
}

this.playing = window.setTimeout(function() {
life.doTurn();
@@ -226,23 +226,25 @@ var lifedom = (function lifedom(life) {
* turned back on
*/
syncLifeToDom: function() {
var currentOn = life.getCurrentOn(),
var newEnabled = life.getNewEnabled(),
newDisabled = life.getNewDisabled(),
_this = this, d;

// Turn everything off
for (var i=enabledCells.length-1;i>-1;i--) {
d = this.getNode(enabledCells[i]);
if (d) d.style.backgroundColor = '';
}
enabledCells = [];
// Turn disabled cells off
_.each(newDisabled, function(yAxes, key) {
xAxis = parseInt(key.substr(1));
_.each(yAxes, function(yAxis) {
d = _this.getNode(xAxis+'x'+yAxis);
if (d) { d.style.backgroundColor = ''; }
});
});

// Turn enabled cells on
_.each(currentOn, function(yAxes, key) {
_.each(newEnabled, function(yAxes, key) {
xAxis = parseInt(key.substr(1));
_.each(yAxes, function(yAxis) {
d = _this.getNode(xAxis+'x'+yAxis);
if (d) { d.style.backgroundColor = ENABLED_COLOR; }
enabledCells.push(xAxis+'x'+yAxis);
});
});
}

0 comments on commit b3b7c51

Please sign in to comment.
You can’t perform that action at this time.