Add Dataflow.Graph.enforceSubjectPresence.

This commit is contained in:
Tony Garnock-Jones 2016-08-06 19:57:01 -04:00
parent 02c66c4bab
commit 8b63c68673
2 changed files with 5 additions and 0 deletions

View File

@ -9,6 +9,7 @@ function Graph() {
this.edgesReverse = Immutable.Map();
this.damagedNodes = Immutable.Set();
this.currentSubjectId = null;
this.enforceSubjectPresence = true;
this.observablePropertyCounter = 0;
}
@ -30,6 +31,8 @@ Graph.prototype.recordObservation = function (objectId) {
if (this.currentSubjectId) {
this.edgesForward = MapSet.add(this.edgesForward, objectId, this.currentSubjectId);
this.edgesReverse = MapSet.add(this.edgesReverse, this.currentSubjectId, objectId);
} else if (this.enforceSubjectPresence) {
throw new Error('Attempt to observe ' + objectId + ' with no currentSubjectId');
}
};

View File

@ -50,6 +50,7 @@ describe('dataflow edges, damage and subjects', function () {
describe('DerivedCell', function () {
describe('simple case', function () {
var g = new Dataflow.Graph();
g.enforceSubjectPresence = false;
var c = DerivedCell(g, 'c', function () { return 123; });
var d = DerivedCell(g, 'd', function () { return c.value * 2; });
it('should be properly initialized', function () {
@ -78,6 +79,7 @@ describe('DerivedCell', function () {
describe('a more complex case', function () {
var g = new Dataflow.Graph();
g.enforceSubjectPresence = false;
function add(a, b) { return a + b; }
var xs = new Cell(g, Immutable.List.of(1, 2, 3, 4), 'xs');