-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
computed store properties #954
Conversation
Codecov Report
@@ Coverage Diff @@
## master #954 +/- ##
==========================================
- Coverage 91.74% 91.73% -0.02%
==========================================
Files 109 109
Lines 4010 4051 +41
Branches 1287 1300 +13
==========================================
+ Hits 3679 3716 +37
- Misses 148 150 +2
- Partials 183 185 +2
Continue to review full report at Codecov.
|
Improved the implementation a bit — no longer uses getters, but instead sorts computed properties topologically (like Svelte does at compile time) which a) avoids any unexpected object behaviour, and b) prevents unnecessary recomputation in some situations (because if computed property Also added a // NOTE — Store is no longer a default export
import { Store, combineStores } from 'svelte/store.js';
const a = new Store({
x: 1,
y: 2
});
a.compute('z', ['x', 'y'], (x, y) => x + y);
const b = new Store({
x: 3,
y: 4
});
b.compute('z', ['x', 'y'], (x, y) => x + y);
// optionally, provide an existing store as the second argument
const c = combineStores({ a, b });
c.compute('total', ['a', 'b'], (a, b) => a.z + b.z);
c.observe('total', total => {
console.log(`total is ${total}`);
}); // total is 10
a.set({ x: 2 }); // total is 11
b.set({ x: 5, y: 6 }); // total is 15 |
On reflection, If that proves to be a source of pain, we can solve it once we have a bit more data. |
Was it intended for |
It was, yeah — I can see us adding things like this in future, for example: export function useLocalStorage(store, key) {
const json = localStorage.getItem(key);
if (json) {
store.set(JSON.parse(json));
}
store.onchange(state => {
localStorage.setItem(key, JSON.stringify(state));
});
} If we did a default export, it could make life harder in future (because of interop headaches that arise from modules with both default and named exports). |
store.js
Outdated
|
||
store._dirty[key] = true; | ||
function visit(key) { | ||
if (visited[key]) return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is visited[key] set?
Adjunct to #951. This adds a
compute
method toStore
:Computed properties can depend on other computed properties, and be observed etc. Setting a computed property will cause an error to be thrown.
Might play around with the implementation a bit more but I think this is probably the right API. Thoughts?