Skip to content

Commit 90927b7

Browse files
committed
Nested digest! Closes #18
1 parent 145a510 commit 90927b7

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,16 @@ function ReactiveFunction(options){
121121

122122
// Propagates changes through the dependency graph.
123123
ReactiveFunction.digest = function (){
124+
var changedIds = Object.keys(changed);
125+
changed = {};
124126
graph
125-
.topologicalSort(Object.keys(changed), false)
127+
.topologicalSort(changedIds, false)
126128
.map(function (id){
127129
return properties[id];
128130
})
129131
.forEach(function (property){
130132
property.evaluate();
131133
});
132-
133134
changed = {};
134135
};
135136

test.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function output(name){
2020

2121
describe("ReactiveFunction", function() {
2222

23-
describe("Core Functionality", function() {
23+
describe("Core Functionality", function() {
2424
it("Should depend on two reactive properties.", function () {
2525

2626
var firstName = ReactiveProperty("Jane");
@@ -430,6 +430,37 @@ describe("ReactiveFunction", function() {
430430
});
431431
});
432432

433+
it("Should execute a nested digest.", function (){
434+
var a = ReactiveProperty(5);
435+
var b = ReactiveProperty();
436+
var c = ReactiveProperty();
437+
438+
var rf1 = ReactiveFunction({
439+
inputs: [a],
440+
callback: function (a){
441+
for(var i = 0; i < a; i++){
442+
b(i);
443+
ReactiveFunction.digest();
444+
assert.equal(c(), i / 2);
445+
}
446+
}
447+
});
448+
449+
var rf2 = ReactiveFunction({
450+
inputs: [b],
451+
output: c,
452+
callback: function (b){
453+
return b / 2;
454+
}
455+
});
456+
457+
ReactiveFunction.digest();
458+
459+
rf1.destroy();
460+
rf2.destroy();
461+
462+
});
463+
433464
});
434465

435466
describe("Cleanup", function (){
@@ -536,7 +567,7 @@ describe("ReactiveFunction", function() {
536567

537568
// These tests may easily break if upstream tests are modified.
538569
// Fix by changing the value of initialId.
539-
var initialId = 56;
570+
var initialId = 60;
540571

541572
it("Should serialize the data flow graph.", function (){
542573

0 commit comments

Comments
 (0)