Skip to content
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

Access to component full state #73

Closed
guzart opened this issue Nov 30, 2016 · 3 comments
Closed

Access to component full state #73

guzart opened this issue Nov 30, 2016 · 3 comments

Comments

@guzart
Copy link
Contributor

guzart commented Nov 30, 2016

While trying to get Hot Module Reloading for webpack going I couldn't find a way to access the full component state.

if (module.hot) {
  module.hot.accept('./components/App.svelte', () => {
    const state = app.get(undefined) // Get the full state
    app.teardown()
    app = new App({ target, data: state })
  })
}

I think this feature could also be useful for storing/restoring session state from top level components. The observe callback suffers from the same problem, but dispatching to state observers would take more bytes to handle.

Not sure if this is a featured you should opt-in to, or if it should be part of the get method.

@Rich-Harris
Copy link
Member

Could be part of the get method – returns key ? state[key] : state. Though that would include computed properties as well as data properties, which should probably be treated as read-only... though I can't think of any specific harm that would come from including them in the returned object.

Very excited to hear you're working on HMR, this is great news!

@guzart
Copy link
Contributor Author

guzart commented Dec 1, 2016

I don't see any harm either. Currently the state already includes the computed values (which makes perfect sense), and there are no mechanisms that prevent your from attempting a set on a computed property, so conceptually a get is already a read-only

I'll work on a PR for this.get, it's a quick win.

Unless there was a way of extending the component skeleton that would let you mess with a nice clean pattern, creating your own "Heavy" base component.

PS: Using this as a learning experience to understand HMR better 😄 , looking forward to integrating it with svelte-loader

guzart added a commit to guzart/svelte that referenced this issue Dec 1, 2016
@guzart
Copy link
Contributor Author

guzart commented Dec 2, 2016

included in v1.0.5

@guzart guzart closed this as completed Dec 2, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants