mirror of
https://github.com/zoriya/v10.git
synced 2026-08-16 02:45:09 +00:00
46 lines
2.1 KiB
TypeScript
46 lines
2.1 KiB
TypeScript
import { Signal } from "signal-polyfill";
|
|
//#region src/core/signals/primitives.d.ts
|
|
/** Read a signal value without tracking it as a dependency. */
|
|
declare const untrack: <T>(fn: () => T) => T;
|
|
/** A writable reactive value (read + write). */
|
|
type Signal$1<T> = Signal.State<T>;
|
|
/** A derived reactive value that re-evaluates when its dependencies change (read-only). */
|
|
type Computed<T> = Signal.Computed<T>;
|
|
/** A read-only view of a reactive value. */
|
|
type ReadonlySignal<T> = Omit<Signal.State<T>, 'set'>;
|
|
interface SignalOptions<T> {
|
|
equals?: (t: T, t2: T) => boolean;
|
|
}
|
|
/** Create a writable reactive value. */
|
|
declare function signal<T>(initialValue: T, options?: SignalOptions<T>): Signal$1<T>;
|
|
/** Create a computed reactive value. */
|
|
declare function computed<T>(fn: () => T, options?: SignalOptions<T>): Computed<T>;
|
|
/**
|
|
* Update a writable signal. Two forms:
|
|
*
|
|
* - **Updater function** `(current) => next`. Works for any signal type,
|
|
* including `Signal<T | undefined>` — handle undefined in the updater.
|
|
* - **Partial object** to merge into the current state. Requires
|
|
* `T extends object`.
|
|
*
|
|
* @example
|
|
* update(state, { playbackRate: 2 });
|
|
* update(state, (s) => ({ ...s, count: s.count + 1 }));
|
|
* update(maybeUndefinedSignal, (current) => current ?? defaultValue);
|
|
*/
|
|
declare function update<T>(signal: Signal$1<T>, updater: (current: T) => T): void;
|
|
declare function update<T extends object>(signal: Signal$1<T>, updater: Partial<T>): void;
|
|
/**
|
|
* Read every signal in a map and return a plain object snapshot. Each read
|
|
* tracks in the surrounding Computed/Effect — equivalent to calling `.get()`
|
|
* on a single `Signal<S>` over the merged shape.
|
|
*
|
|
* Convenience for behaviors that pass whole state/context snapshots to pure
|
|
* helpers; prefer per-field reads when only a few fields are needed.
|
|
*/
|
|
declare function snapshot<M extends Record<string, ReadonlySignal<unknown>>>(map: M): { [K in keyof M]: M[K] extends {
|
|
get(): infer V;
|
|
} ? V : never; };
|
|
//#endregion
|
|
export { Computed, ReadonlySignal, Signal$1 as Signal, SignalOptions, computed, signal, snapshot, untrack, update };
|
|
//# sourceMappingURL=primitives.d.ts.map
|