From 279d6e0e656705a35337b9a58c2e9aa1ddefac42 Mon Sep 17 00:00:00 2001 From: eldritchreality Date: Fri, 29 Apr 2016 15:36:51 +0000 Subject: [PATCH] propagators no longer run unless all their input cells have values --- propagator.js | 13 ++++++++++--- test/test-propagator.js | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/propagator.js b/propagator.js index cf33ca4..c8570bc 100644 --- a/propagator.js +++ b/propagator.js @@ -19,16 +19,23 @@ function Propagator(func,input,output) { this.output = output; function applyFuncToInputs() { - var inputValues = self.input.map((cell) => cell.getContents()) + var inputs = self.input.map((cell) => cell.getContents()) + var output = {"value" : undefined, "usable" : false} - return func.apply(null,inputValues); + inputs.fullyPopulated = inputs.every((value) => typeof value !== "undefined") + + if(inputs.fullyPopulated) { + output.value = func.apply(null,inputs); + output.usable = true; + } + return output; } this.propagate = function propagate() { var outputValues = applyFuncToInputs(); - self.output.update(outputValues, self) + if (outputValues.usable) self.output.update(outputValues.value, self) } this.input.forEach( (cell) => cell.addListener(self.propagate) ) diff --git a/test/test-propagator.js b/test/test-propagator.js index 513a13c..2a541d1 100644 --- a/test/test-propagator.js +++ b/test/test-propagator.js @@ -48,6 +48,25 @@ describe("The propagator constructor",function() { expect(mockOutputCell.getContents()).to.equal(addValuesTogether(10,1,1)) }) + it("should not update an output cell unless all of its input cells have values",function(){ + var mockInputCell1 = Propagator.makeCell() + var mockInputCell2 = Propagator.makeCell() + var mockInputCell3 = Propagator.makeCell() + var mockOutputCell = Propagator.makeCell() + var addValuesTogether = ((x,y,z) => x+y+z) + + var testPropagator = new Propagator(addValuesTogether,[mockInputCell1,mockInputCell2,mockInputCell3],mockOutputCell); + + mockInputCell1.update(1) + expect(mockOutputCell.getContents()).to.be.undefined; + + mockInputCell2.update(10) + expect(mockOutputCell.getContents()).to.be.undefined; + + mockInputCell3.update(100) + expect(mockOutputCell.getContents()).to.equal(addValuesTogether(1,10,100)) + }) + it("should throw an error when the constructor is called with no arguments",function(){ expect( () => new Propagator() ).to.throw(Error)