From 4d8090aa158bc369fc679baccb29d7e843bd5ce6 Mon Sep 17 00:00:00 2001 From: Brian Takita Date: Tue, 1 May 2018 21:20:05 -0400 Subject: [PATCH] Fix https://github.com/sveltejs/svelte/issues/1399 Store - Cyclical Dependency Detected when child computed property defined before parent & grand-parent computed proprety --- store.js | 6 ++--- .../store-computed-compute-graph/_config.js | 22 +++++++++++++++++++ .../store-computed-compute-graph/main.html | 0 3 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 test/runtime/samples/store-computed-compute-graph/_config.js create mode 100644 test/runtime/samples/store-computed-compute-graph/main.html diff --git a/store.js b/store.js index 42f17c9bb87b..678c73b1b3c4 100644 --- a/store.js +++ b/store.js @@ -53,19 +53,19 @@ assign(Store.prototype, { var visited = blankObject(); function visit(key) { + if (visited[key]) return; + if (cycles[key]) { throw new Error('Cyclical dependency detected'); } - if (visited[key]) return; - visited[key] = true; - var c = computed[key]; if (c) { cycles[key] = true; c.deps.forEach(visit); sorted.push(c); + visited[key] = true; } } diff --git a/test/runtime/samples/store-computed-compute-graph/_config.js b/test/runtime/samples/store-computed-compute-graph/_config.js new file mode 100644 index 000000000000..ba7ac02f8dea --- /dev/null +++ b/test/runtime/samples/store-computed-compute-graph/_config.js @@ -0,0 +1,22 @@ +import { Store } from '../../../../store.js'; + +const store = new Store(); + +export default { + store, + + test(assert, component, target) { + store.compute('dep4', ['dep1', 'dep2', 'dep3'], (...args) => ['dep4'].concat(...args)); + store.compute('dep1', ['source'], (...args) => ['dep1'].concat(...args)); + store.compute('dep2', ['dep1'], (...args) => ['dep2'].concat(...args)); + store.compute('dep3', ['dep1', 'dep2'], (...args) => ['dep3'].concat(...args)); + store.set({source: 'source'}); + assert.equal(JSON.stringify(store.get().dep4), JSON.stringify([ + 'dep4', + 'dep1', 'source', + 'dep2', 'dep1', 'source', + 'dep3', 'dep1', 'source', + 'dep2', 'dep1', 'source' + ])); + } +}; diff --git a/test/runtime/samples/store-computed-compute-graph/main.html b/test/runtime/samples/store-computed-compute-graph/main.html new file mode 100644 index 000000000000..e69de29bb2d1