Skip to content

Commit

Permalink
Fixed not recalculating computeds when values were changed
Browse files Browse the repository at this point in the history
  • Loading branch information
gerich-home committed Feb 2, 2016
1 parent 0632078 commit 19d61f5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "it-depends",
"version": "1.0.0",
"version": "1.0.1",
"description": "Lightweight dependency tracking library for JavaScript",
"main": "itDepends.js",
"dependencies": {},
Expand All @@ -21,7 +21,7 @@
},
"scripts": {
"test": "mocha",
"coverage": "mocha specs/index.js --require blanket -R html-cov > coverage.html"
"coverage": "mocha specs/index.js --require blanket -R html-cov > coverage.html"
},
"repository": {
"type": "git",
Expand All @@ -41,13 +41,13 @@
},
"homepage": "https://github.com/gerich-home/itDepends#readme",
"config": {
"blanket": {
"pattern": [
"./out/build/itDepends.js"
],
"data-cover-never": [
"node_modules"
]
}
}
"blanket": {
"pattern": [
"./out/build/itDepends.js"
],
"data-cover-never": [
"node_modules"
]
}
}
}
28 changes: 18 additions & 10 deletions src/itDepends.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@
var trackers = [nop];
var nextId = 0;

function notifyCurrentTracker(tracked) {
trackers[trackers.length - 1](tracked);
function notifyCurrentTracker(tracked, dependencyId) {
trackers[trackers.length - 1](tracked, dependencyId);
};

exports.value = function (initialValue) {
var id = ++nextId;
var currentValue = initialValue;

var getValue = function () {
notifyCurrentTracker(self);
notifyCurrentTracker(self, id);
return currentValue;
};

Expand All @@ -59,6 +60,7 @@

return self;
};

exports.computed = function (calculator) {
var id = ++nextId;
var currentValue;
Expand All @@ -72,8 +74,11 @@
};

var atLeastOneDependencyChanged = function (visitor) {
for (var i = 0; i < dependencies.length; i++) {
var dependency = dependencies[i];
for (var dependencyId in dependencies) {
if (!dependencies.hasOwnProperty(dependencyId))
continue;

var dependency = dependencies[dependencyId];

if (dependency.trackedValue.changedSince(dependency.capturedVersion, visitor)) {
return true;
Expand All @@ -92,13 +97,16 @@
var self = function () {
if (needRecalc({})) {
needRecalcCache = false;
dependencies = [];
dependencies = {};

trackers.push(function (trackedValue, dependencyId) {
if (dependencies[dependencyId])
return;

trackers.push(function (trackedValue) {
dependencies.push({
dependencies[dependencyId] ={
trackedValue: trackedValue,
capturedVersion: trackedValue.valueVersion
});
};
});

try {
Expand All @@ -108,7 +116,7 @@
}
}

notifyCurrentTracker(self);
notifyCurrentTracker(self, id);

return currentValue;
};
Expand Down

0 comments on commit 19d61f5

Please sign in to comment.