From 05c55dd2d7e6e821cbd818b3a0705e4282e2f65b Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Fri, 23 Feb 2024 21:44:14 -0500 Subject: [PATCH] tidy up --- .../src/internal/client/dom/blocks/each.js | 6 +- .../src/internal/client/reactivity/sources.js | 61 +++++++------------ .../src/internal/client/reactivity/types.d.ts | 23 ++++--- packages/svelte/src/internal/client/render.js | 2 +- .../svelte/src/internal/client/runtime.js | 16 ++--- .../svelte/src/internal/client/types.d.ts | 6 +- 6 files changed, 49 insertions(+), 65 deletions(-) diff --git a/packages/svelte/src/internal/client/dom/blocks/each.js b/packages/svelte/src/internal/client/dom/blocks/each.js index 6fc5d1f925..744cc546ac 100644 --- a/packages/svelte/src/internal/client/dom/blocks/each.js +++ b/packages/svelte/src/internal/client/dom/blocks/each.js @@ -32,7 +32,7 @@ const LIS_BLOCK = -2; /** * @template T - * @typedef {T | import('#client').Signal} MaybeSignal + * @typedef {T | import('#client').ValueSignal} MaybeSignal */ /** @@ -563,7 +563,7 @@ function update_each_item_block(block, item, index, type) { // each_animation(block, transitions); } if (index_is_reactive) { - set_signal_value(/** @type {import('#client').Signal} */ (block.i), index); + set_signal_value(/** @type {import('#client').Source} */ (block.i), index); } else { block.i = index; } @@ -574,7 +574,7 @@ function update_each_item_block(block, item, index, type) { * @param {V} item * @param {unknown} key * @param {number} index - * @param {(anchor: null, item: V, index: number | import('#client').Signal) => void} render_fn + * @param {(anchor: null, item: V, index: number | import('#client').Source) => void} render_fn * @param {number} flags * @returns {import('#client').EachItemBlock} */ diff --git a/packages/svelte/src/internal/client/reactivity/sources.js b/packages/svelte/src/internal/client/reactivity/sources.js index f4f739a558..c6eb3d31e0 100644 --- a/packages/svelte/src/internal/client/reactivity/sources.js +++ b/packages/svelte/src/internal/client/reactivity/sources.js @@ -5,12 +5,30 @@ import { CLEAN, SOURCE } from '../constants.js'; /** * @template V - * @param {V} initial_value + * @param {V} value * @returns {import('./types.js').Source} */ /*#__NO_SIDE_EFFECTS__*/ -export function source(initial_value) { - return create_source_signal(SOURCE | CLEAN, initial_value); +export function source(value) { + /** @type {import('#client').Source} */ + const source = { + // consumers + c: null, + // equals + e: default_equals, + // flags + f: SOURCE | CLEAN, + // value + v: value, + // write version + w: 0 + }; + + if (DEV) { + /** @type {import('#client').SourceDebug} */ (source).inspect = new Set(); + } + + return source; } /** @@ -31,40 +49,3 @@ export function mutable_source(initial_value) { return s; } - -/** - * @template V - * @param {import('./types.js').SignalFlags} flags - * @param {V} value - * @returns {import('./types.js').Source | import('./types.js').Source & import('./types.js').SourceDebug} - */ -function create_source_signal(flags, value) { - if (DEV) { - return { - // consumers - c: null, - // equals - e: default_equals, - // flags - f: flags, - // value - v: value, - // write version - w: 0, - // this is for DEV only - inspect: new Set() - }; - } - return { - // consumers - c: null, - // equals - e: default_equals, - // flags - f: flags, - // value - v: value, - // write version - w: 0 - }; -} diff --git a/packages/svelte/src/internal/client/reactivity/types.d.ts b/packages/svelte/src/internal/client/reactivity/types.d.ts index cb6a6c996b..5a7cff630f 100644 --- a/packages/svelte/src/internal/client/reactivity/types.d.ts +++ b/packages/svelte/src/internal/client/reactivity/types.d.ts @@ -10,7 +10,7 @@ export type SignalFlags = export type EffectType = typeof EFFECT | typeof PRE_EFFECT | typeof RENDER_EFFECT; -export type Source = { +export interface Source { /** consumers: Signals that read from the current signal */ c: null | Reaction[]; /** equals: For value equality */ @@ -21,12 +21,11 @@ export type Source = { v: V; // write version w: number; -}; +} -export type SourceDebug = { - /** This is DEV only */ +export interface SourceDebug extends Source { inspect: Set; -}; +} export interface Derived { /** consumers: Signals that read from the current signal */ @@ -34,7 +33,7 @@ export interface Derived { /** context: The associated component if this signal is an effect/computed */ x: null | ComponentContext; /** dependencies: Signals that this signal reads from */ - d: null | Signal[]; + d: null | ValueSignal[]; /** equals: For value equality */ e: null | EqualsFunctions; /** The types that the signal represent, as a bitwise value */ @@ -49,13 +48,17 @@ export interface Derived { w: number; } +export interface DerivedDebug extends Derived { + inspect: Set; +} + export interface Effect { /** consumers: Signals that read from the current signal */ c: null | Reaction[]; /** context: The associated component if this signal is an effect/computed */ x: null | ComponentContext; /** dependencies: Signals that this signal reads from */ - d: null | Signal[]; + d: null | ValueSignal[]; /** destroy: Thing(s) that need destroying */ y: null | (() => void) | Array<() => void>; /** equals: For value equality */ @@ -86,8 +89,8 @@ export interface Effect { export type Reaction = Derived | Effect; -export type ValueSignal = Source | Derived; +export type ValueSignal = Source | Derived; -export type Signal = Source | Reaction; +export type ValueSignalDebug = SourceDebug | DerivedDebug; -export type SignalDebug = SourceDebug & Signal; +export type Signal = Source | Reaction; diff --git a/packages/svelte/src/internal/client/render.js b/packages/svelte/src/internal/client/render.js index 89d85e54f4..0a550cc522 100644 --- a/packages/svelte/src/internal/client/render.js +++ b/packages/svelte/src/internal/client/render.js @@ -2053,7 +2053,7 @@ const rest_props_handler = { }; /** - * @param {import('#client').Source> | Record} props + * @param {Record} props * @param {string[]} rest * @returns {Record} */ diff --git a/packages/svelte/src/internal/client/runtime.js b/packages/svelte/src/internal/client/runtime.js index 22e9037506..af04882b2d 100644 --- a/packages/svelte/src/internal/client/runtime.js +++ b/packages/svelte/src/internal/client/runtime.js @@ -65,16 +65,16 @@ export function set_current_effect(effect) { current_effect = effect; } -/** @type {null | import('#client').Signal[]} */ +/** @type {null | import('#client').ValueSignal[]} */ let current_dependencies = null; let current_dependencies_index = 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 | import('#client').Signal[]} + * @type {null | import('#client').Source[]} */ let current_untracked_writes = null; -/** @type {null | import('#client').SignalDebug} */ +/** @type {null | import('#client').SourceDebug} */ let last_inspected_signal = null; /** If `true`, `get`ting the signal should not register it as a dependency */ export let current_untracking = false; @@ -95,7 +95,7 @@ let captured_signals = new Set(); /** @type {Function | null} */ export let inspect_fn = null; -/** @type {Array} */ +/** @type {Array} */ let inspect_captured_signals = []; // Handling runtime component context @@ -673,7 +673,7 @@ function update_derived(signal, force_schedule) { // @ts-expect-error if (DEV && signal.inspect && force_schedule) { - for (const fn of /** @type {import('#client').SignalDebug} */ (signal).inspect) fn(); + for (const fn of /** @type {import('#client').ValueSignalDebug} */ (signal).inspect) fn(); } } } @@ -686,7 +686,7 @@ function update_derived(signal, force_schedule) { export function get(signal) { // @ts-expect-error if (DEV && signal.inspect && inspect_fn) { - /** @type {import('#client').SignalDebug} */ (signal).inspect.add(inspect_fn); + /** @type {import('#client').SourceDebug} */ (signal).inspect.add(inspect_fn); // @ts-expect-error inspect_captured_signals.push(signal); } @@ -913,9 +913,9 @@ export function set_signal_value(signal, value) { // @ts-expect-error if (DEV && signal.inspect) { if (is_batching_effect) { - last_inspected_signal = /** @type {import('#client').SignalDebug} */ (signal); + last_inspected_signal = /** @type {import('#client').SourceDebug} */ (signal); } else { - for (const fn of /** @type {import('#client').SignalDebug} */ (signal).inspect) fn(); + for (const fn of /** @type {import('#client').SourceDebug} */ (signal).inspect) fn(); } } } diff --git a/packages/svelte/src/internal/client/types.d.ts b/packages/svelte/src/internal/client/types.d.ts index a1c484a833..e01cd85750 100644 --- a/packages/svelte/src/internal/client/types.d.ts +++ b/packages/svelte/src/internal/client/types.d.ts @@ -17,7 +17,7 @@ export type Store = { export type ComponentContext = { /** local signals (needed for beforeUpdate/afterUpdate) */ - d: null | Signal[]; + d: null | Source[]; /** props */ s: Record; /** exports (and props, if `accessors: true`) */ @@ -51,9 +51,9 @@ export type EachItemBlock = { /** effect */ e: Effect; /** item */ - v: any | Signal; + v: any | Source; /** index */ - i: number | Signal; + i: number | Source; /** key */ k: unknown; };