Skip to content

Commit

Permalink
propagators no longer run unless all their input cells have values
Browse files Browse the repository at this point in the history
  • Loading branch information
eldritchreality committed Apr 29, 2016
1 parent 9bd8a20 commit 279d6e0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
13 changes: 10 additions & 3 deletions propagator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) )
Expand Down
19 changes: 19 additions & 0 deletions test/test-propagator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 279d6e0

Please sign in to comment.