svelte icon indicating copy to clipboard operation
svelte copied to clipboard

[feat] no invalidate reactive vars if dependencies are all static

Open tanhauhau opened this issue 3 years ago • 2 comments

Reopen of https://github.com/sveltejs/svelte/pull/4066

this does output optimisation for a simple edge case of:

<script>
  let name = 'world';
  $: two_name = name + name;
</script>
{two_name} {name}

Here even though two_name is declared using reactive statements, but since name are static, it should not need $$invalidate to wrap around it

Before submitting the PR, please make sure you do the following

  • [x] It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • [x] Prefix your PR title with [feat], [fix], [chore], or [docs].
  • [x] This message body should clearly illustrate what problems it solves.
  • [x] Ideally, include a test that fails without this PR but passes with it.

Tests

  • [x] Run the tests with npm test and lint the project with npm run lint

tanhauhau avatar Oct 14 '22 04:10 tanhauhau

I tried to run the compiler and I have 1 question.

two_name is static, so can we remove update lifecycle process also?

p(ctx, [dirty]) {
  if (dirty & /*two_name*/ 1) set_data(t0, /*two_name*/ ctx[0]);
},

baseballyama avatar Oct 14 '22 14:10 baseballyama

I just copied my comment from https://github.com/sveltejs/svelte/pull/7938#issuecomment-1279114630


We discussed with maintainers and concution is that we should not implement warnings because compiler can not decide that reactive statement is essentially unnecessary or not.

e.g.

<script>
  export const foo = 1;
  export const bar = 1;
  export const baz = 1;

  // For now, all dependencies are static, but maybe in the future, one or more variable will be dynamic.
  // So Intentionally, I used `$:` for here for preventing to make a bug.
  $: qux = foo + bar + baz;
</script>

baseballyama avatar Dec 04 '22 04:12 baseballyama

@dummdidumm is attempting to deploy a commit to the Svelte Team on Vercel.

A member of the Team first needs to authorize it.

vercel[bot] avatar Feb 22 '23 15:02 vercel[bot]

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated
svelte-dev-2 ❌ Failed (Inspect) Feb 22, 2023 at 5:01PM (UTC)

vercel[bot] avatar Feb 22 '23 17:02 vercel[bot]

@baseballyama that seems like an edge case, i think if you want to do that you should just svelte-ignore the warning

DetachHead avatar Feb 22 '23 23:02 DetachHead

From my perspective, it sounds like semantics matter. (e.g. For now, this is static, but according to domain knowledge, this is not truly static.) So Svelte compiler doesn't know such semantics, so IMO it should be completely configurable. Already you created an issue on eslint-plugin-svelte, and I agree to add this rule on ESLint, so I or someone will implement this.👍

baseballyama avatar Feb 23 '23 02:02 baseballyama

This has been released in 3.56.0.

Conduitry avatar Mar 10 '23 00:03 Conduitry

It seems that this causes the value to be undefined on first render. https://github.com/TanStack/query/issues/5120

AgarwalPragy avatar Mar 14 '23 11:03 AgarwalPragy