Skip to content

Expose intrinsic to invalidate variable #6278

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

Closed
NyxCode opened this issue May 1, 2021 · 1 comment
Closed

Expose intrinsic to invalidate variable #6278

NyxCode opened this issue May 1, 2021 · 1 comment

Comments

@NyxCode
Copy link

NyxCode commented May 1, 2021

It's common to assign a variable to itself to trigger an update:

users.push(user);
users = user;

IDEs love to complain about this, and, arguably, it's a bit strange.

Since these calls are transpiled to $$invalidate, I propose an intrinsic function which explicitly exposes this.
This would

  1. make IDEs happy
  2. result in easier to understand code

The snippet below could look like this:

users.push(user);
invalidate(users);

Additional context
Maybe this could be usefull in regards to #4535 by offering an alternative to <svelte:options immutable/> for selectively updating a single property. This example could be rewritten like this:

<script>
	import Todo from './Todo.svelte';

	let todos = [
		{ id: 1, done: true, text: 'wash the car' },
		{ id: 2, done: false, text: 'take the dog for a walk' },
		{ id: 3, done: false, text: 'mow the lawn' }
	];

	function toggle(idx) {
		todos[idx].done ^= true;
                // invalidate the updated property
		invalidate(todos[idx].done);
	}
</script>

<h2>Todos</h2>
{#each todos as todo, i}
	<Todo {todo} on:click={() => toggle(i)}/>
{/each}

I'm not sure if it would be possible to implement invalidate like this.
However, even the "simple" usecase, being just an alternative to variable = variable, seems worth it to me.

@Conduitry
Copy link
Member

The $$invalidate that that compiles to is specific to the given component - it's not just the same function all the time that you can call for any component. We don't want another way to invalidate variables that looks like a function call but really compiles to something else. There should be one way to invalidate variables in Svelte, and if your editor is complaining about it, you should look into further configuring your tooling.

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