@tempots/dom

Signal class

Represents a signal that holds a value and notifies its listeners when the value changes.

Signature:

declare class Signal 

Constructors

Constructor

Modifiers

Description

(constructor)(value, equals)

Represents a signal with a value of type T.

Properties

Property

Modifiers

Type

Description

$

readonly

AtGetter

Represents a collection of signals mapping to each key/field in the wrapped value.

at

readonly

(key: K) => Signal

Returns a new Signal that emits the value at the specified key of the current value.

count

readonly

() => Computed

Returns a signal that emits the count of values received so far.

deriveProp

readonly

(autoDisposeProp?: boolean) => Prop

Derives a new property from the current signal.

dispose

readonly

() => void

Disposes the signal, releasing any resources associated with it.

equals

readonly

(a: T, b: T) => boolean

feedProp

readonly

(prop: Prop, autoDisposeProp?: boolean) => Prop

Feeds a property into the signal and sets up disposal behavior.

filter

readonly

(fn: (value: T) => boolean, startValue?: T) => Computed

filterMap

readonly

(fn: (value: T) => O | undefined | null, startValue: O, equals?: (a: O, b: O) => boolean) => Computed

Returns a new Computed object that applies the provided mapping function to the value of this Signal, and filters out values that are undefined or null.

flatMap

readonly

(fn: (value: T) => Signal, equals?: (a: O, b: O) => boolean) => Computed

Returns a new Signal that applies the given function to the value of the current Signal, and then flattens the resulting Signal.

get

readonly

() => T

Gets the current value of the signal.

hasListeners

readonly

() => boolean

Checks if the signal has any registered listeners.

is

static

readonly

(value: O | Signal) => value is Signal

Checks if a value is a Signal.

isDisposed

readonly

() => boolean

Checks whether the signal is disposed.

map

readonly

(fn: (value: T) => O, equals?: (a: O, b: O) => boolean) => Computed

Returns a new Computed instance that applies the given mapping function to the value of this Signal. The mapping function is called whenever the value of this Signal changes.

mapAsync

readonly

(fn: (value: T, options: { abortSignal: AbortSignal; }) => Promise, alt: O, recover?: (error: unknown) => O, equals?: (a: O, b: O) => boolean) => Prop

Maps the values emitted by the signal to a new value asynchronously using the provided function. If the function throws an error, it will be caught and logged. If a recovery function is provided, it will be called with the error and its return value will be used as the mapped value. If no recovery function is provided, the error will be logged as an unhandled promise rejection.

mapMaybe

readonly

(fn: (value: T) => O | undefined | null, alt: O) => Computed

Maps the values of the signal using the provided function fn, and returns a new signal containing the mapped values. If the mapped value is undefined or null, it is replaced with the provided alt value.

ofPromise

static

readonly

(promise: Promise, init: O, recover?: (error: unknown) => O, equals?: (a: O, b: O) => boolean) => Signal

Creates a Signal that holds the result of a Promise.

on

readonly

(listener: (value: T) => void) => () => void

Registers a listener function to be called whenever the value of the signal changes. The listener function will be immediately called with the current value of the signal. Returns a function that can be called to unregister the listener.

onDispose

readonly

(listener: () => void) => void

Adds a listener function to be called when the object is disposed.

setDerivative

readonly

(computed: Computed) => void

Adds a computed value as a derivative of the signal. When the computed value is disposed, it is automatically removed from the derivatives list. Additionally, when the computed value is disposed, it sets the signal as dirty.

tap

readonly

(fn: (value: T) => void) => Computed

Invokes a callback function with the current value of the signal, without modifying the signal.

value

readonly

T

Gets the value of the signal.