-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
feat: $effect.pending(value)
#16849
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
base: main
Are you sure you want to change the base?
feat: $effect.pending(value)
#16849
Conversation
|
|
The problem with only being able to use it in the template is that you need to be vigilant about using it in all places your state shown if you want to avoid tearing of displays of the same state |
yeah it 100% must be usable in the script tag aswell (e.g. inside $derived) |
People have doubts about the name. So do I. I reused So what should we rename it, and should we also rename |
I'm thinking of |
|
I encountered a similar problem. Should this be moved to a separate issue? iShot_2025-09-30_10.14.18.mp4 |
The
$effect.pending
rune tells you how manyawait
expressions are unresolved in the current boundary, which is useful for providing feedback to the user that something is happening.This PR augments it: when you pass an expression to it, it will always return the current value of that expression.
Quick primer: when a piece of state changes, and an
await
expression depends on that state, Svelte doesn't update the UI to reflect the state change until theawait
expression has resolved. Otherwise it's a total free-for-all — if you have one component that doesawait fetchPost(params.slug)
and another one that doesawait fetchComments(params.slug)
, the post and the comments will be rendered at different times. Or, to take a contrived example that makes it clear, in a case like this......if
n
changes from1
to2
the UI will show1 * 2 = 1
. This is a bad default.Other frameworks solve this with things like useTransition. Svelte's view is that you want to use a transition in 99% of cases, and so making it opt-in rather than opt-out is the wrong choice. But right now we don't have an opt-out mechanism.
$effect.pending(...)
is that mechanism. It allows you to always show the latest version in one specific part of your UI — for example, in the button that changes some state.Caveats:
$effect
and$effect.pre
, it can't be inside a function that's called from the effect, it has to be 'visible' to the compiler. (Right now, it only works in the template. And maybe that's fine?)Before submitting the PR, please make sure you do the following
feat:
,fix:
,chore:
, ordocs:
.packages/svelte/src
, add a changeset (npx changeset
).Tests and linting
pnpm test
and lint the project withpnpm lint