blockless
Rich Harris 10 months ago
parent e064363529
commit 05c55dd2d7

@ -32,7 +32,7 @@ const LIS_BLOCK = -2;
/**
* @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);
}
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 {
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<number>) => void} render_fn
* @param {(anchor: null, item: V, index: number | import('#client').Source<number>) => void} render_fn
* @param {number} flags
* @returns {import('#client').EachItemBlock}
*/

@ -5,12 +5,30 @@ import { CLEAN, SOURCE } from '../constants.js';
/**
* @template V
* @param {V} initial_value
* @param {V} value
* @returns {import('./types.js').Source<V>}
*/
/*#__NO_SIDE_EFFECTS__*/
export function source(initial_value) {
return create_source_signal(SOURCE | CLEAN, initial_value);
export function source(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;
}
/**
* @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 Source<V = unknown> = {
export interface Source<V = unknown> {
/** consumers: Signals that read from the current signal */
c: null | Reaction[];
/** equals: For value equality */
@ -21,12 +21,11 @@ export type Source<V = unknown> = {
v: V;
// write version
w: number;
};
}
export type SourceDebug = {
/** This is DEV only */
export interface SourceDebug<V = unknown> extends Source<V> {
inspect: Set<Function>;
};
}
export interface Derived<V = unknown> {
/** 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 */
x: null | ComponentContext;
/** dependencies: Signals that this signal reads from */
d: null | Signal<V>[];
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<V = unknown> {
w: number;
}
export interface DerivedDebug<V = unknown> extends Derived<V> {
inspect: Set<Function>;
}
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<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
* @returns {Record<string, unknown>}
*/

@ -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<import('#client').SignalDebug>} */
/** @type {Array<import('#client').SourceDebug>} */
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();
}
}
}

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

Loading…
Cancel
Save