-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
feat: allow await
in components
#15844
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?
Conversation
This reverts commit 2585516.
Detected reactivity loss | ||
``` | ||
TODO |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO (and below)
Cannot await outside a `<svelte:boundary>` with a `pending` snippet | ||
``` | ||
|
||
TODO |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still working through files (seriously this thing is HUGE) but I figured I should go ahead and submit what comments I do have. So far, really impressed by the general cleanliness.
@@ -406,7 +420,9 @@ export function analyze_component(root, source, options) { | |||
|
|||
const component_name = get_component_name(options.filename); | |||
|
|||
const runes = options.runes ?? Array.from(module.scope.references.keys()).some(is_rune); | |||
const runes = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cannot wait to nuke these heuristics in 6
import { build_attribute_value } from '../shared/element.js'; | ||
import { build_event_handler } from './events.js'; | ||
import { determine_slot } from '../../../../../utils/slot.js'; | ||
import { create_derived } from '../../utils.js'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unused
import { create_derived } from '../../utils.js'; |
@@ -69,7 +69,7 @@ export function createSubscriber(start) { | |||
subscribers += 1; | |||
|
|||
return () => { | |||
tick().then(() => { | |||
queueMicrotask(() => { | |||
// Only count down after timeout, else we would reach 0 before our own render effect reruns, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Only count down after timeout, else we would reach 0 before our own render effect reruns, | |
// Only count down after a microtask, else we would reach 0 before our own render effect reruns, |
import { is_array } from './internal/shared/utils.js'; | ||
import { user_effect } from './internal/client/index.js'; | ||
import * as e from './internal/client/errors.js'; | ||
import { lifecycle_outside_component } from './internal/shared/errors.js'; | ||
import { legacy_mode_flag } from './internal/flags/index.js'; | ||
import { async_mode_flag, legacy_mode_flag } from './internal/flags/index.js'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import { async_mode_flag, legacy_mode_flag } from './internal/flags/index.js'; | |
import { legacy_mode_flag } from './internal/flags/index.js'; |
unused
packages/svelte/src/index-client.js
Outdated
*/ | ||
export function getAbortSignal() { | ||
if (active_reaction === null) { | ||
throw new Error('TODO getAbortSignal can only be called inside a reaction'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO
/** @type {Map<Derived, any> | null} */ | ||
export let batch_deriveds = null; | ||
|
||
/** TODO handy for debugging, but we should probably eventually delete it */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is eventually today or some later time
|
||
for (const [source, value] of this.#current) { | ||
batch.#current.set(source, value); | ||
// TODO what about batch.#previous? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
worth it to wrap up these todos?
|
||
/** | ||
* Forcibly remove all current batches | ||
* TODO investigate why we need this in tests |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO if it matters
/** TODO handy for debugging, but we should probably eventually delete it */ | ||
let uid = 1; | ||
|
||
export class Batch { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class would really benefit from good JSDoc, especially given its reach across the codebase, especially on complex methods like process
. I can see what the code does but trying to intuit why is quite challenging
* @param {Array<() => Promise<any>>} async | ||
* @param {(values: Value[]) => any} fn | ||
*/ | ||
export function flatten(sync, async, fn) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another one that would really benefit from JSDoc
This is the PR to go with #15845. Feedback and questions should happen on the discussion rather than here.
Closes #1857, closes #3203, closes #5501
How this works
I'll likely flesh this out in more detail later, but a few quick bullet points:
Batch
. This was always implied (we would flush effects in a microtask, if aflushSync
didn't get there first, so thata++; b++
would result in one flush rather than two) but now it's explicitBatch
has aprocess
method that is responsible for callingprocess_effects
. Whereas previously this would result in effects running immediately (albeit in two phases, so that e.g.$effect
effects happen after other updates), this now only happens for async effects (so that we can know if a state change resulted in async work) and block effects (so that we can know if a newly-created branch has async work, or if newly-destroyed branches can be skipped). All other effects (i.e. user effects, or those that update the DOM) are added to the batch{#if ...}
etc no longer append or remove branches directly. Instead, they do that in a commit callback belonging to the batch, which runs when everything is settledprocess_effects
run it eagerly) in a trenchcoat. This is because deriveds are lazy and would never update in time$derived
with anawait
expression (that isn't inside a function), or whenever you have anawait
expression in a template effect$.template_effect
now takes a third argument, which is an array of all async deriveds{#if ...}
et al) can also containawait
expressions, in which case they are wrapped in an$.async
block. These simply pass the async derived to the inner logic, so that the blocks themselves don't need to be aware that they're dealing with async valuesI'm sure I'm overlooking a ton of stuff but that's as much braindump as I can give right now. Will add comments to more of the source code as and when I can.
WIP
This is not finished; there are bugs, and parts of the code are a little messy. Some stuff could possibly be extracted into a separate PR so that this one is smaller and more focused (e.g. converting boundaries to the
Boundary
class).preserve_context
more accurategetAbortSignal
into separate PR?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