/** * create a simple callback bin to collect and later flush callbacks. * * The bin supports collecting callbacks, manually processing them, extracting a * specific callback, and disposing all collected callbacks at once. * * @returns utilities to manage the collected callbacks */ export declare function bin(): { (): void; /** * add a callback to the bin. * * @param callback function to run during `process`/`dispose` * @returns unsubscriber that removes the callback from the bin */ collect: (callback: () => void | Promise) => () => void; /** * remove & return a specific callback from the bin without invoking it. * * @param callback the callback to extract * @returns the same callback passed in */ extract: (callback: () => void | Promise) => () => void | Promise; /** * invoke all currently collected callbacks (without clearing the bin). * * callbacks are invoked in insertion order; async callbacks are started but * not awaited. */ process: () => void; /** invoke all callbacks & clear the bin. */ dispose: () => void; /** * assignment helper that behaves like {@link bin().collect}. * * note: as a setter, this does not return a remover; use * {@link bin().collect} directly if you need to un-collect a callback * later. */ _: (() => void | Promise) | undefined; }; declare type Bondaged = { [K in keyof T]: T[K] extends (...args: infer A) => infer R ? T[K] & ((this: void, ...args: A) => R) : T[K]; }; /** * normalize a maybe-invalidator into a concrete invalidator function if * present. * * accepts a value that can be: * * - `undefined`/`void`: returns `undefined` * - a function: returns the function * - a promise resolving to `void` or a function: returns a function that, when * invoked, awaits & runs the resolved invalidator (fire & forget) * * @param maybeInvalidator - value or promise that may contain an invalidator * @returns an invalidator function, or `undefined` if not present */ export declare function coerceInvalidator(maybeInvalidator: T): Extract | (T extends Promise ? () => void : never) | undefined; /** * create a starter that wires an input readable signal to an output writable * signal via a mapping function. optionally composes a user-provided onStart. * * when the output starts, it subscribes to the input, applying `mapper` to * propagate changes into the output. when the output stops, it unsubscribes & * runs the composed stopper returned by `onStart`, if any. * * @typeParam T - input value type * @typeParam In - input readable signal type * @typeParam R - output value type * @typeParam Out - output writable signal type * @param input - input readable signal * @param mapper - mapping function from `T` to `R` * @param onStart - optional starter to run when output starts; its stopper is * composed * @returns a starter suitable for the derived output signal */ export declare function createDeriveStarter>(input: ReadableSignal, mapper: (v: T) => R, onStart?: Starter): Starter; /** * **record derive**: returns a signal of a record mirroring the input keys with * their latest values; updates preserve object identity while mutating its * properties. * * @template Stores record mapping keys to readable signals * @param stores input readable signals * @returns a readable signal of the current values per key */ export declare function derive>>(stores: Stores): Signal>; /** * **mapper derive**: computes an output value from `$`-prefixed input values * and updates when any input changes. * * @template Stores record mapping keys to readable signals * @template Callback mapping from `$`-values to the derived value * @param stores input readable signals * @param callback mapping function from `$`-values to output * @param onStart optional starter for the derived signal * @returns a readable signal of the computed value */ export declare function derive>, Callback extends (values: DeriveCallbackValues) => any>(stores: Stores, callback: Callback, onStart?: Starter>>): Signal>; /** * values passed to a derive callback: each input key `k` becomes `$k` with its * current value. */ export declare type DeriveCallbackValues>> = { [k in keyof Stores as `$${k extends string | number ? k : never}`]: ReadableValue; }; /** * record shape produced by derive without a callback: same keys as inputs with * current values. */ export declare type DeriveRecordValues>> = { [k in keyof Stores]: ReadableValue; }; export declare type Invalidator = () => void | Promise; export declare type MaybeInvalidator = void | Invalidator | Promise; export declare type MaybeStopper = void | Stopper | Promise; export declare interface ReadableSignal { get(): T; trigger(): void; destroy(): void; subscribe(onValue: Subscriber): Unsubscriber; subscribeSoon(onNextValue: Subscriber): Unsubscriber; subscribeStart(onStart: Starter & this>): Unsubscriber; subscribeStartSoon(onNextStart: Starter & this>): Unsubscriber; subscribeStop(onStop: Stopper): Unsubscriber; subscribeStopSoon(onNextStop: Stopper): Unsubscriber; out>(to: O): O; derive(fn: (v: T) => R): ReadableSignal; } export declare type ReadableValue> = ReturnType; /** * a minimal reactive signal with start/stop lifecycle, subscriptions, & * derivation. * * reads (via `get`) start the signal; when no subscribers remain it stops & * runs any registered stoppers. supports wiring with `in`/`out` & deriving * mapped signals with `derive`. */ export declare class Signal implements WritableSignal { /** * compare two values with special handling for `NaN` where `NaN` equals * `NaN`. * * @param a - left value * @param b - right value * @returns true if equal by identity or both are `NaN` */ static compare(a: unknown, b: unknown): boolean; protected value: T; private readonly invalidators; private readonly subscribers; private readonly starters; private readonly stoppers; private startedValue; get started(): boolean; protected set started(/** @type {boolean} */ it: boolean); get subscribed(): boolean; protected readonly deriveMapperToSignal: WeakMap<(v: any) => any, Signal>; private readonly connectedInStores; private readonly connectedOutStores; /** * create a new signal. * * @param value - initial value * @param onStart - optional starter invoked when the signal starts; its * return value is treated as a stopper */ constructor(value: T, onStart?: Starter>); /** * returns a cast into read-only view of the signal. * * since only the type is changed, the identity of the object remains the * same & you can technically still call {@linkcode set} et al. by ignoring * the typechecker. */ get readonly(): ReadableSignal; /** * set the signal value & notify subscribers if it changed. comparison uses * {@link Signal.compare}. * * @param v - new value */ set(v: T): void; /** * update the current value using a mapping function. * * @param mapper - function receiving the current value & returning the next * value */ update(mapper: (v: T) => T): void; /** * read the current value & start the signal if not already started. * * @returns current value */ get(): T; /** notify all subscribers with the current value & run queued invalidators. */ trigger(): void; /** * subscribe & receive an immediate call with the current value, then on * updates. * * @param onValue - subscriber receiving values; may return an invalidator * @returns unsubscriber */ subscribe(onValue: (v: T) => void): Unsubscriber; /** * subscribe for future updates only (no immediate call). * * @param onNextValue - subscriber for subsequent values; may return an * invalidator * @returns unsubscriber */ subscribeSoon(onNextValue: (v: T) => void): Unsubscriber; /** * subscribe to start events. if already started, invokes immediately. * * @param onStart - starter receiving a bound handle to this signal; may * return a stopper * @returns unsubscriber for the start listener */ subscribeStart(onStart: Starter): () => void; /** * subscribe to future start events (no immediate call). * * @param onNextStart - starter to register * @returns unsubscriber for the start listener */ subscribeStartSoon(onNextStart: Starter): () => void; /** * subscribe to stop events. if not started, invokes immediately. * * @param onStop - stopper to run when the signal stops * @returns unsubscriber for the stop listener */ subscribeStop(onStop: () => void): () => void; /** * subscribe to future stop events (no immediate call). * * @param onNextStop - stopper to register * @returns unsubscriber for the stop listener */ subscribeStopSoon(onNextStop: () => void): () => void; protected flush(): void; /** clear all listeners & release the current value for gc. */ destroy(): void; /** * wire this signal to read from another readable signal. when this signal * starts, it subscribes to `from` & mirrors its values. repeated calls with * the same source are ignored. * * @param from - source readable signal * @returns this signal for chaining */ in(from: ReadableSignal): this; /** * wire this signal to push values into another writable signal. when `to` * starts, it subscribes to this signal & receives updates. repeated calls * with the same target are ignored. * * @typeParam O - target writable signal type * @param to - destination writable signal * @returns the destination signal for chaining */ out>(to: O): O; /** * create a derived signal by mapping this signal's values. * * the returned signal starts when read or subscribed to, wires to this * signal, & updates by applying `mapper`. the same mapper function identity * returns the same derived instance (memoized per-mapper). * * @typeParam R - derived value type * @param mapper - mapping function from current value to derived value * @param onStart - optional starter for the derived signal * @returns derived signal */ derive(mapper: (v: T) => R, onStart?: Starter>): Signal; private invokeSubscriber; private invokeStarter; /** * coerce the contained value to a primitive via `valueOf`. * * @returns primitive representation of the current value, if any */ valueOf(): Object | undefined; } export declare type Starter> = (store: Bondaged) => MaybeStopper; export declare type Stopper = Invalidator; /** * subscribe to multiple readable signals & react to their combined values. * * the callback receives a single object with `$key` properties for each * dependency. it is called immediately with current values, & again whenever * any dependency changes. if the callback returns an invalidator, it runs * before the next callback execution & on unsubscribe. * * @typeParam T - dependency record mapping keys to readable signals * @param dependencies - record of readable signals to observe * @param callback - handler receiving `$`-prefixed current values; may return * invalidator * @returns unsubscriber that removes all internal subscriptions & runs the last * invalidator */ export declare function subscribe>>(dependencies: T, callback: (values: SubscribeCallbackValues) => MaybeInvalidator): () => void; /** * shape of values passed to a subscribe callback: each input key `k` becomes a * `$k` property containing the current value of the corresponding signal. */ export declare type SubscribeCallbackValues>> = { [k in keyof Stores as `$${k extends string | number ? k : never}`]: ReadableValue; }; export declare type Subscriber = (v: T) => MaybeInvalidator; export declare type Unsubscriber = () => void; export declare type Updater = (v: T) => T; export declare interface WritableSignal extends ReadableSignal { set(value: T): void; update(updater: Updater): void; in(from: ReadableSignal): WritableSignal; } export { }