diff --git a/packages/svelte/src/internal/client/dev/tracing.js b/packages/svelte/src/internal/client/dev/tracing.js index c6edfde933..7ad4ca40af 100644 --- a/packages/svelte/src/internal/client/dev/tracing.js +++ b/packages/svelte/src/internal/client/dev/tracing.js @@ -1,4 +1,4 @@ -/** @import { Derived, Reaction, Value } from '#client' */ +/** @import { Derived, Reaction, Source, Value } from '#client' */ import { UNINITIALIZED } from '../../../constants.js'; import { snapshot } from '../../shared/clone.js'; import { DERIVED, ASYNC, PROXY_PATH_SYMBOL, STATE_SYMBOL } from '#client/constants'; @@ -131,8 +131,10 @@ export function trace(label, fn) { } /** - * @param {Value} source + * @template {Value} T + * @param {T} source * @param {string} label + * @returns {T} */ export function tag(source, label) { source.label = label; diff --git a/packages/svelte/src/internal/client/dom/blocks/boundary.js b/packages/svelte/src/internal/client/dom/blocks/boundary.js index f5b9ce981d..5afe09adb4 100644 --- a/packages/svelte/src/internal/client/dom/blocks/boundary.js +++ b/packages/svelte/src/internal/client/dom/blocks/boundary.js @@ -110,9 +110,6 @@ export class Boundary { /** @type {Set} */ #dirty_deriveds = new Set(); - /** @type {Set} */ - #maybe_dirty_deriveds = new Set(); - /** * A source containing the number of pending async deriveds/expressions. * Only created if `$effect.pending()` is used inside the boundary, @@ -341,12 +338,7 @@ export class Boundary { // any effects that were previously deferred should be transferred // to the batch, which will flush in the next microtask - batch.transfer_effects( - this.#dirty_effects, - this.#maybe_dirty_effects, - this.#dirty_deriveds, - this.#maybe_dirty_deriveds - ); + batch.transfer_effects(this.#dirty_effects, this.#maybe_dirty_effects, this.#dirty_deriveds); } /** @@ -354,13 +346,7 @@ export class Boundary { * @param {Effect} effect */ defer_effect(effect) { - defer_effect( - effect, - this.#dirty_effects, - this.#maybe_dirty_effects, - this.#dirty_deriveds, - this.#maybe_dirty_deriveds - ); + defer_effect(effect, this.#dirty_effects, this.#maybe_dirty_effects, this.#dirty_deriveds); } /** diff --git a/packages/svelte/src/internal/client/dom/blocks/each.js b/packages/svelte/src/internal/client/dom/blocks/each.js index 65d3c9fe24..da2f1adebf 100644 --- a/packages/svelte/src/internal/client/dom/blocks/each.js +++ b/packages/svelte/src/internal/client/dom/blocks/each.js @@ -666,7 +666,7 @@ function reconcile(state, array, anchor, flags, get_key) { * @param {V} value * @param {unknown} key * @param {number} index - * @param {(anchor: Node, item: V | Source, index: number | Value, collection: () => V[]) => void} render_fn + * @param {(anchor: Node, item: MaybeSource, index: MaybeSource, collection: () => V[]) => void} render_fn * @param {number} flags * @param {() => V[]} get_collection * @returns {EachItem} diff --git a/packages/svelte/src/internal/client/reactivity/batch.js b/packages/svelte/src/internal/client/reactivity/batch.js index cae469ce22..cc1545696e 100644 --- a/packages/svelte/src/internal/client/reactivity/batch.js +++ b/packages/svelte/src/internal/client/reactivity/batch.js @@ -827,7 +827,7 @@ export class Batch { * Tell a fork batch that a source has been updated. Will delete that source from the fork, * discarding it if it has no other sources left, and rerunning it else with the new value. * @param {Batch} batch A fork - * @param {Source} source + * @param {Value} source * @param {boolean} is_derived * @param {any} value */ @@ -844,7 +844,7 @@ export class Batch { (!current || current[0] !== value) && ((source.f & ASYNC) === 0 || !depends_on( - source.e, + /** @type {Effect} */ (/** @type {Source} */ (source).e), [...batch.current.keys()].filter((s) => !this.current.has(s)), new Map() )) @@ -1283,7 +1283,7 @@ function mark_eager_effects(value, effects) { /** * @param {Reaction} reaction - * @param {Source[]} sources + * @param {Value[]} sources * @param {Map} checked */ function depends_on(reaction, sources, checked) { diff --git a/packages/svelte/src/internal/client/reactivity/deriveds.js b/packages/svelte/src/internal/client/reactivity/deriveds.js index 585151e02e..01bec8518c 100644 --- a/packages/svelte/src/internal/client/reactivity/deriveds.js +++ b/packages/svelte/src/internal/client/reactivity/deriveds.js @@ -139,7 +139,7 @@ export function async_derived(fn, label, location) { if (DEV) { reactivity_loss_tracker = { effect, effect_deps: new Set(), warned: false }; - effect.label ??= label ?? fn.toString(); + // effect.label ??= label ?? fn.toString(); // TODO add dev time labeling for effects in follow-up PR } /** @type {ReturnType>} */ diff --git a/packages/svelte/src/internal/client/reactivity/sources.js b/packages/svelte/src/internal/client/reactivity/sources.js index 8230edc114..746aed25ae 100644 --- a/packages/svelte/src/internal/client/reactivity/sources.js +++ b/packages/svelte/src/internal/client/reactivity/sources.js @@ -47,7 +47,7 @@ import { set_signal_status, update_derived_status } from './status.js'; /** @type {Set} */ export let eager_effects = new Set(); -/** @type {Map} */ +/** @type {Map} */ export const old_values = new Map(); /** @@ -71,14 +71,15 @@ export function set_eager_effects_deferred() { */ // TODO rename this to `state` throughout the codebase export function source(v, stack) { - /** @type {Value} */ + /** @type {Source} */ var signal = { - f: 0, // TODO ideally we could skip this altogether, but it causes type errors + f: 0, v, reactions: null, equals, rv: 0, - wv: 0 + wv: 0, + e: null }; if (DEV && tracing_mode_flag) { @@ -142,7 +143,7 @@ export function mutate(source, value) { /** * @template V - * @param {Source} source + * @param {Value} source * @param {V} value * @param {boolean} [should_proxy] * @returns {V} @@ -182,7 +183,7 @@ var count_deps = 0; /** * @template V - * @param {Source} source + * @param {Value} source * @param {V} value * @param {Effect[] | null} [updated_during_traversal] * @returns {V} diff --git a/packages/svelte/src/internal/client/reactivity/types.d.ts b/packages/svelte/src/internal/client/reactivity/types.d.ts index 0ee8570c3d..8e9b4e7624 100644 --- a/packages/svelte/src/internal/client/reactivity/types.d.ts +++ b/packages/svelte/src/internal/client/reactivity/types.d.ts @@ -100,7 +100,10 @@ export interface Effect extends Reaction { dev_stack?: DevStackEntry | null; } -export type Source = Value; +export interface Source extends Value { + /** Only set for ASYNC signals - the corresponding effect that writes to this source */ + e: Effect | null; +} export type MaybeSource = T | Source; diff --git a/packages/svelte/src/internal/client/runtime.js b/packages/svelte/src/internal/client/runtime.js index 563b0e692b..72a2621b05 100644 --- a/packages/svelte/src/internal/client/runtime.js +++ b/packages/svelte/src/internal/client/runtime.js @@ -96,9 +96,9 @@ export function set_active_effect(effect) { } /** - * When sources are created within a reaction, reading and writing + * When sources/deriveds are created within a reaction, reading and writing * them within that reaction should not cause a re-run - * @type {null | Set} + * @type {null | Set} */ export let current_sources = null; @@ -126,11 +126,11 @@ export let skipped_deps = 0; /** * Tracks writes that the effect it's executed in doesn't listen to yet, * so that the dependency can be added to the effect later on if it then reads it - * @type {null | Source[]} + * @type {null | Value[]} */ export let untracked_writes = null; -/** @param {null | Source[]} value */ +/** @param {null | Value[]} value */ export function set_untracked_writes(value) { untracked_writes = value; } diff --git a/packages/svelte/src/reactivity/date.js b/packages/svelte/src/reactivity/date.js index f882c05d76..b70ba7969a 100644 --- a/packages/svelte/src/reactivity/date.js +++ b/packages/svelte/src/reactivity/date.js @@ -1,4 +1,4 @@ -/** @import { Source } from '#client' */ +/** @import { Derived } from '#client' */ import { derived } from '../internal/client/index.js'; import { set, state } from '../internal/client/reactivity/sources.js'; import { tag } from '../internal/client/dev/tracing.js'; @@ -42,7 +42,7 @@ var inited = false; export class SvelteDate extends Date { #time = state(super.getTime()); - /** @type {Map>} */ + /** @type {Map>} */ #deriveds = new Map(); #reaction = active_reaction;