blockless
Rich Harris 2 years ago
parent e064363529
commit 05c55dd2d7

@ -32,7 +32,7 @@ const LIS_BLOCK = -2;
/** /**
* @template T * @template T
* @typedef {T | import('#client').Signal<T>} MaybeSignal * @typedef {T | import('#client').ValueSignal<T>} MaybeSignal
*/ */
/** /**
@ -563,7 +563,7 @@ function update_each_item_block(block, item, index, type) {
// each_animation(block, transitions); // each_animation(block, transitions);
} }
if (index_is_reactive) { if (index_is_reactive) {
set_signal_value(/** @type {import('#client').Signal<number>} */ (block.i), index); set_signal_value(/** @type {import('#client').Source<number>} */ (block.i), index);
} else { } else {
block.i = index; block.i = index;
} }
@ -574,7 +574,7 @@ function update_each_item_block(block, item, index, type) {
* @param {V} item * @param {V} item
* @param {unknown} key * @param {unknown} key
* @param {number} index * @param {number} index
* @param {(anchor: null, item: V, index: number | import('#client').Signal<number>) => void} render_fn * @param {(anchor: null, item: V, index: number | import('#client').Source<number>) => void} render_fn
* @param {number} flags * @param {number} flags
* @returns {import('#client').EachItemBlock} * @returns {import('#client').EachItemBlock}
*/ */

@ -5,12 +5,30 @@ import { CLEAN, SOURCE } from '../constants.js';
/** /**
* @template V * @template V
* @param {V} initial_value * @param {V} value
* @returns {import('./types.js').Source<V>} * @returns {import('./types.js').Source<V>}
*/ */
/*#__NO_SIDE_EFFECTS__*/ /*#__NO_SIDE_EFFECTS__*/
export function source(initial_value) { export function source(value) {
return create_source_signal(SOURCE | CLEAN, initial_value); /** @type {import('#client').Source<V>} */
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<V>} */ (source).inspect = new Set();
}
return source;
} }
/** /**
@ -31,40 +49,3 @@ export function mutable_source(initial_value) {
return s; return s;
} }
/**
* @template V
* @param {import('./types.js').SignalFlags} flags
* @param {V} value
* @returns {import('./types.js').Source<V> | import('./types.js').Source<V> & 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
};
}

@ -10,7 +10,7 @@ export type SignalFlags =
export type EffectType = typeof EFFECT | typeof PRE_EFFECT | typeof RENDER_EFFECT; export type EffectType = typeof EFFECT | typeof PRE_EFFECT | typeof RENDER_EFFECT;
export type Source<V = unknown> = { export interface Source<V = unknown> {
/** consumers: Signals that read from the current signal */ /** consumers: Signals that read from the current signal */
c: null | Reaction[]; c: null | Reaction[];
/** equals: For value equality */ /** equals: For value equality */
@ -21,12 +21,11 @@ export type Source<V = unknown> = {
v: V; v: V;
// write version // write version
w: number; w: number;
}; }
export type SourceDebug = { export interface SourceDebug<V = unknown> extends Source<V> {
/** This is DEV only */
inspect: Set<Function>; inspect: Set<Function>;
}; }
export interface Derived<V = unknown> { export interface Derived<V = unknown> {
/** consumers: Signals that read from the current signal */ /** consumers: Signals that read from the current signal */
@ -34,7 +33,7 @@ export interface Derived<V = unknown> {
/** context: The associated component if this signal is an effect/computed */ /** context: The associated component if this signal is an effect/computed */
x: null | ComponentContext; x: null | ComponentContext;
/** dependencies: Signals that this signal reads from */ /** dependencies: Signals that this signal reads from */
d: null | Signal<V>[]; d: null | ValueSignal[];
/** equals: For value equality */ /** equals: For value equality */
e: null | EqualsFunctions; e: null | EqualsFunctions;
/** The types that the signal represent, as a bitwise value */ /** The types that the signal represent, as a bitwise value */
@ -49,13 +48,17 @@ export interface Derived<V = unknown> {
w: number; w: number;
} }
export interface DerivedDebug<V = unknown> extends Derived<V> {
inspect: Set<Function>;
}
export interface Effect { export interface Effect {
/** consumers: Signals that read from the current signal */ /** consumers: Signals that read from the current signal */
c: null | Reaction[]; c: null | Reaction[];
/** context: The associated component if this signal is an effect/computed */ /** context: The associated component if this signal is an effect/computed */
x: null | ComponentContext; x: null | ComponentContext;
/** dependencies: Signals that this signal reads from */ /** dependencies: Signals that this signal reads from */
d: null | Signal[]; d: null | ValueSignal[];
/** destroy: Thing(s) that need destroying */ /** destroy: Thing(s) that need destroying */
y: null | (() => void) | Array<() => void>; y: null | (() => void) | Array<() => void>;
/** equals: For value equality */ /** equals: For value equality */
@ -86,8 +89,8 @@ export interface Effect {
export type Reaction = Derived | Effect; export type Reaction = Derived | Effect;
export type ValueSignal<V> = Source<V> | Derived<V>; export type ValueSignal<V = unknown> = Source<V> | Derived<V>;
export type Signal<V = unknown> = Source<V> | Reaction; export type ValueSignalDebug<V = unknown> = SourceDebug<V> | DerivedDebug<V>;
export type SignalDebug<V = unknown> = SourceDebug & Signal<V>; export type Signal<V = unknown> = Source<V> | Reaction;

@ -2053,7 +2053,7 @@ const rest_props_handler = {
}; };
/** /**
* @param {import('#client').Source<Record<string, unknown>> | Record<string, unknown>} props * @param {Record<string, unknown>} props
* @param {string[]} rest * @param {string[]} rest
* @returns {Record<string, unknown>} * @returns {Record<string, unknown>}
*/ */

@ -65,16 +65,16 @@ export function set_current_effect(effect) {
current_effect = effect; current_effect = effect;
} }
/** @type {null | import('#client').Signal[]} */ /** @type {null | import('#client').ValueSignal[]} */
let current_dependencies = null; let current_dependencies = null;
let current_dependencies_index = 0; let current_dependencies_index = 0;
/** /**
* Tracks writes that the effect it's executed in doesn't listen to yet, * 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 * 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; let current_untracked_writes = null;
/** @type {null | import('#client').SignalDebug} */ /** @type {null | import('#client').SourceDebug} */
let last_inspected_signal = null; let last_inspected_signal = null;
/** If `true`, `get`ting the signal should not register it as a dependency */ /** If `true`, `get`ting the signal should not register it as a dependency */
export let current_untracking = false; export let current_untracking = false;
@ -95,7 +95,7 @@ let captured_signals = new Set();
/** @type {Function | null} */ /** @type {Function | null} */
export let inspect_fn = null; export let inspect_fn = null;
/** @type {Array<import('#client').SignalDebug>} */ /** @type {Array<import('#client').SourceDebug>} */
let inspect_captured_signals = []; let inspect_captured_signals = [];
// Handling runtime component context // Handling runtime component context
@ -673,7 +673,7 @@ function update_derived(signal, force_schedule) {
// @ts-expect-error // @ts-expect-error
if (DEV && signal.inspect && force_schedule) { 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) { export function get(signal) {
// @ts-expect-error // @ts-expect-error
if (DEV && signal.inspect && inspect_fn) { 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 // @ts-expect-error
inspect_captured_signals.push(signal); inspect_captured_signals.push(signal);
} }
@ -913,9 +913,9 @@ export function set_signal_value(signal, value) {
// @ts-expect-error // @ts-expect-error
if (DEV && signal.inspect) { if (DEV && signal.inspect) {
if (is_batching_effect) { if (is_batching_effect) {
last_inspected_signal = /** @type {import('#client').SignalDebug} */ (signal); last_inspected_signal = /** @type {import('#client').SourceDebug} */ (signal);
} else { } else {
for (const fn of /** @type {import('#client').SignalDebug} */ (signal).inspect) fn(); for (const fn of /** @type {import('#client').SourceDebug} */ (signal).inspect) fn();
} }
} }
} }

@ -17,7 +17,7 @@ export type Store<V> = {
export type ComponentContext = { export type ComponentContext = {
/** local signals (needed for beforeUpdate/afterUpdate) */ /** local signals (needed for beforeUpdate/afterUpdate) */
d: null | Signal<any>[]; d: null | Source[];
/** props */ /** props */
s: Record<string, unknown>; s: Record<string, unknown>;
/** exports (and props, if `accessors: true`) */ /** exports (and props, if `accessors: true`) */
@ -51,9 +51,9 @@ export type EachItemBlock = {
/** effect */ /** effect */
e: Effect; e: Effect;
/** item */ /** item */
v: any | Signal<any>; v: any | Source<any>;
/** index */ /** index */
i: number | Signal<number>; i: number | Source<number>;
/** key */ /** key */
k: unknown; k: unknown;
}; };

Loading…
Cancel
Save