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

draft: Smart preserve local state #41

Open
wants to merge 5 commits into
base: monorepo-and-tests
Choose a base branch
from

Conversation

rixo
Copy link
Collaborator

@rixo rixo commented Nov 17, 2021

Adds preserveLocalState:'smart' option.

When enabled, components local state will be preserved, except if the initialisation code of their declaration (appears to) have changed, based on static code analysis. AFAICT this is similar to what React Fast Refresh does with useState.

This option decides whether to preserve state or not at the individual variable level. This is different from React, that resets the whole state if any of them changes.

To try the option

[email protected]:rixo/svelte-hmr.git
cd svelte-hmr
git checkout preserve-local-state-smart
pnpm i
cd playground/kit-app
pnpm run dev

Open the browser. Change the src/routes/index.svelte file (or anything you want, really) and note effect on HMR.

Discussion

Reset whole state instead of individual variables?

Should we reset the state of the whole component (all variables) as soon as any one of them is reset? (Side note: if we ever get to preserve child components state, this means they would get reset too when a parent's state is reset.)

More aggressive intention detection?

Should we reset a variable for any assignment to it that happens at the root of the init function (but not in reactive expressions)?

For example, this would reset foo (even though the declaration has not changed):

<script>
  let foo = 0
</script>
...

=>

<script>
  let foo = 0
  foo++
</script>
...

@rixo rixo changed the title Smart preserve local state draft: Smart preserve local state Apr 11, 2022
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

Successfully merging this pull request may close these issues.

1 participant