-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
svelte/store #951
svelte/store #951
Conversation
Codecov Report
@@ Coverage Diff @@
## master #951 +/- ##
==========================================
- 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.
|
Just to clarify — the |
See #954 for another PR based on this one that adds computed properties. |
This addresses #930 (see also this gist).
It introduces a new
Store
object...The
store
object hasget
,set
andobserve
methods that work identically to those on a Svelte component, plus anonchange
function that accepts a(state, changed) => void
callback for handling state changes of any kind.Store
can be subclassed for adding whatever methods you need:Fairly straightforward stuff. But here comes the fun part — all children of
<App>
getthis.store
, and if you opt in with thestore: true
compiler option then they become bound to it.Once bound, components can reference store properties by prefixing them with
$
:Store props and regular props coexist peacefully. For example a component's computed property can happily depend on both store and regular props.
Because this is Svelte, components only watch properties that they depend on, so there's no penalty for storing and updating everything centrally. The footprint is tiny (we can reuse component prototype methods, so the whole thing is less than 100 lines), and if you know how to use Svelte you already know how to use
Store
. And of course it's completely opt-in (we have to addcomponent.store = component._root.options.store
to theinit
function shared between components, but that's the only penalty for people not usingStore
).I'm excited about this addition. Let me know what you all think.
TODO:
bind:foo=$bar
on:click='store.doSomething()'
(i.e. whiteliststore.*
)