Skip to content

Commit

Permalink
alternative approach to #1399
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed May 2, 2018
1 parent 058e1bd commit 9d6d996
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions store.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,31 +49,30 @@ assign(Store.prototype, {
_sortComputedProperties: function() {
var computed = this._computed;
var sorted = this._sortedComputedProperties = [];
var cycles;
var visited = blankObject();
var lookup = blankObject();

function visit(key, rootKey) {
function visit(key) {
if (visited[key]) return;

if (cycles[key]) {
throw new Error('Cyclical dependency detected. key: ' + key + ' rootKey: ' + rootKey);
}
visited[key] = true;

var c = computed[key];

if (c) {
cycles[key] = true;
c.deps.forEach(function(dep) {
visit(dep, rootKey)
if (!lookup[key]) lookup[key] = blankObject();
c.deps.forEach(dep => {
if (lookup[dep] && lookup[dep][key]) {
throw new Error(`Cyclical dependency detected between ${dep} <-> ${key}`);
}
lookup[key][dep] = true;
visit(dep);
});
sorted.push(c);
visited[key] = true;
}
}

for (var key in this._computed) {
cycles = blankObject();
visit(key, key);
visit(key);
}
},

Expand Down

0 comments on commit 9d6d996

Please sign in to comment.