alternative approach to #1399

pull/1405/head
Rich Harris 7 years ago
parent 058e1bddf3
commit 9d6d996866

@ -49,31 +49,30 @@ assign(Store.prototype, {
_sortComputedProperties: function() { _sortComputedProperties: function() {
var computed = this._computed; var computed = this._computed;
var sorted = this._sortedComputedProperties = []; var sorted = this._sortedComputedProperties = [];
var cycles;
var visited = blankObject(); var visited = blankObject();
var lookup = blankObject();
function visit(key, rootKey) { function visit(key) {
if (visited[key]) return; if (visited[key]) return;
visited[key] = true;
if (cycles[key]) {
throw new Error('Cyclical dependency detected. key: ' + key + ' rootKey: ' + rootKey);
}
var c = computed[key]; var c = computed[key];
if (c) { if (c) {
cycles[key] = true; if (!lookup[key]) lookup[key] = blankObject();
c.deps.forEach(function(dep) { c.deps.forEach(dep => {
visit(dep, rootKey) if (lookup[dep] && lookup[dep][key]) {
throw new Error(`Cyclical dependency detected between ${dep} <-> ${key}`);
}
lookup[key][dep] = true;
visit(dep);
}); });
sorted.push(c); sorted.push(c);
visited[key] = true;
} }
} }
for (var key in this._computed) { for (var key in this._computed) {
cycles = blankObject(); visit(key);
visit(key, key);
} }
}, },

Loading…
Cancel
Save