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

batch function to update multiple sources at once #12238

Closed
ottomated opened this issue Jun 30, 2024 · 4 comments
Closed

batch function to update multiple sources at once #12238

ottomated opened this issue Jun 30, 2024 · 4 comments

Comments

@ottomated
Copy link

Describe the problem

If I have multiple $states that are all dependencies of a $derived rune, there is no way to update them all at once. For example: REPL

let pending = $state(false);
let data = $state(undefined);
	
const status = $derived(pending ? 'pending' : data ? 'success' : 'idle');
	
function load(d) {
  pending = true;
  setTimeout(() => {
    // I need to update these at the same time
    data = d;
    pending = false;
  }, 400);
}

$inspect({ status, data });

Logs:

"init"  { status: "idle", data: undefined } 
"update" { status: "pending", data: undefined } 
"update" { status: "pending", data: "data" } // Bad state!
"update" { status: "success", data: "data" }

Describe the proposed solution

import { batch } from 'svelte';

function load(d) {
  pending = true;
  setTimeout(() => {
    // Does not mark any of the sources as dirty until the function exits
    batch(() => {
      data = d;
      pending = false;
    });
  }, 400);
}

Importance

would make my life easier

@trueadm
Copy link
Contributor

trueadm commented Jul 1, 2024

We already batch updates on a microtask. The reason you're seeing all the intermediate updates is because you're using $inspect – if you instead switch to using an effect that logs the changes then you'll see it works as you expect already. Inspect works this way to help you debug each individual change.

@trueadm trueadm closed this as completed Jul 1, 2024
@PatrickG
Copy link

PatrickG commented Jul 1, 2024

We already batch updates on a microtask. The reason you're seeing all the intermediate updates is because you're using $inspect – if you instead switch to using an effect that logs the changes then you'll see it works as you expect already. Inspect works this way to help you debug each individual change.

Maybe the type for these logs should be 'intermediate-update' instead of 'update', to make that visible.

@trueadm
Copy link
Contributor

trueadm commented Jul 1, 2024

@PatrickG It's not logging a UI update, but rather, it's logging the update to the bit of state itself.

@PatrickG
Copy link

PatrickG commented Jul 1, 2024

@trueadm Yea, I understood that. But it seems like it might confuse some people.

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

3 participants