headless

API stores

bindableDerived

function

bindableDerived<T, U>(onChange$, stores, adjustValue, equal): WritableSignal<T, T>

Creates a writable signal that derives its value from multiple stores and allows for custom adjustment and equality checks.

Type Parameters

T

The type of the derived value.

U extends [WritableSignal<T, T>, ...StoreInput<any>[]]

A tuple type where the first element is a writable signal of type T and the rest are store inputs.

Parameters

onChange$: ReadableSignal<(value) => void>

A readable signal that emits a function to be called when the derived value changes.

stores: U

A tuple of stores where the first element is a writable signal of type T and the rest are store inputs.

adjustValue = ...

A function to adjust the derived value based on the input values from the stores.

equal = ...

A function to compare the current and new values for equality.

Returns

WritableSignal<T, T>

A writable signal that derives its value from the provided stores and allows for custom adjustment and equality checks.


bindableProp

function

bindableProp<T>(store$, onChange$, adjustValue?, equal?): WritableSignal<T, T>

Creates a bindable property that synchronizes a writable signal with an optional adjustment function and equality check.

Type Parameters

T

The type of the value being stored.

Parameters

store$: WritableSignal<T, undefined | T>

The writable signal that holds the current value.

onChange$: ReadableSignal<(newValue) => void>

A readable signal that triggers a callback when the value changes.

adjustValue? = identity

An optional function to adjust the value before storing it. Defaults to the identity function.

equal? = tansuDefaultEqual

An optional function to compare values for equality. Defaults to tansuDefaultEqual.

Returns

WritableSignal<T, T>

A writable signal that synchronizes with the provided store and triggers the onChange callback when updated.


createPatch

function

createPatch<T>(stores): (storesValues) => void

Utility function designed to create a patch function related to the provided stores. Any key given to the patch function which is not in the original object will be ignored.

Type Parameters

T extends object

The type of the object that the stores represent.

Parameters

stores: ToWritableSignal<T>

The stores to be updated.

Returns

Function

  • A function that takes partial values of the stores and updates them.
Parameters

storesValues: Partial<T>

Returns

void

Example
const storeA$ = writable(1);
const storeB$ = writable(1);
const patch = createPatch({a: storeA$, b: storeB$});

patch({a: 2}) // will perform storeA$.set(2)
patch({a: 2, b: 2}) // will perform storeA$.set(2) and storeB$.set(2) in the same batch.
patch({a: 2, c: 2}) // will perform storeA$.set(2), c is ignored.

findChangedProperties

function

findChangedProperties<T>(obj1, obj2): Partial<T> | null

This utility function is designed to compare the first level of two objects.

It returns a new object which has all the keys for which the values in obj1 and obj2 are different, with the values from obj2, or null if objects are identical.

Type Parameters

T extends Record<string, any>

The type of the objects being compared.

Parameters

obj1: Partial<T>

The first partial object to compare.

obj2: Partial<T>

The second partial object to compare.

Returns

Partial<T> | null

A partial object containing the properties that have different values, or null if the objects are identical.


isStore

function

isStore(x): x is ReadableSignal<any>

Returns true if the provided argument is a store (ReadableSignal).

Parameters

x: any

argument that is tested

Returns

x is ReadableSignal<any>

true if the argument is a store (ReadableSignal)


mergeConfigStores

function

mergeConfigStores<T>(keys, config1?, config2?): ReadableSignals<T>

Merges two configuration stores into one, prioritizing the first store's values when both stores have a value for the same key.

Type Parameters

T extends object

The type of the configuration object.

Parameters

keys: keyof T[]

The keys to merge from the configuration stores.

config1?: ReadableSignals<T>

The first configuration store.

config2?: ReadableSignals<T>

The second configuration store.

Returns

ReadableSignals<T>

  • The merged configuration store.

normalizeConfigStores

function

normalizeConfigStores<T>(keys, config?): ReadableSignals<T>

Normalizes configuration stores by converting them into readable signals.

Type Parameters

T extends object

The type of the configuration object.

Parameters

keys: keyof T[]

An array of keys to normalize from the configuration object.

config?: ReadableSignal<Partial<T>> | ValuesOrReadableSignals<T>

The configuration object or readable signals to normalize.

Returns

ReadableSignals<T>

An object containing readable signals for each key in the configuration.


ReadableSignals

type alias

ReadableSignals<T>: { [K in keyof T]?: ReadableSignal<T[K] | undefined> }

Represents a collection of readable signals for an object type T. Each key in the object corresponds to a key in T, and the value is an optional ReadableSignal that can hold the value of the corresponding property in T or undefined.

Type Parameters

T extends object

The object type for which the readable signals are defined.


stateStores

function

stateStores<A>(inputStores): object

Using input stores, this function builds an object containing the stores as readable and a global state.

Type Parameters

A extends object

The type of the state object.

Parameters

inputStores: { [K in string | number | symbol as `${K & string}$`]: ReadableSignal<any> }

the input stores

Returns

object

the object containing the stores as readable and the global state

state$

state$: ReadableSignal<A>

stores

stores: { [K in string | number | symbol as `${K & string}$`]: ReadableSignal<A[K]> }


toReadableStore

function

toReadableStore<T>(x): ReadableSignal<T>

If the provided argument is already a store, it is returned as is, otherwise, a readable store is created with the provided argument as its initial value.

Type Parameters

T

The type of the value.

Parameters

x: T | ReadableSignal<T>

The value to be converted to a readable store.

Returns

ReadableSignal<T>

  • The readable store containing the value.

ToWritableSignal

type alias

ToWritableSignal<P>: { [K in keyof P as `${K & string}$`]-?: WritableSignal<P[K], P[K] | undefined> }

Transforms the properties of a given type P into writable signals. Each property key in P is suffixed with a $ and its type is converted to a WritableSignal.

Type Parameters

P

The original type whose properties are to be transformed.


toWritableStore

function

toWritableStore<T>(x): WritableSignal<T, T>

Converts a value or a writable signal into a writable signal.

Type Parameters

T

The type of the value or signal.

Parameters

x: T | WritableSignal<T, T>

The value or writable signal to convert.

Returns

WritableSignal<T, T>

  • The resulting writable signal.

WithoutDollar

type alias

WithoutDollar<S>: S extends `${infer U}$` ? U : never

A utility type that removes the trailing dollar sign ($) from a string type.

Type Parameters

S extends `${string}$`

A string type that ends with a dollar sign ($).

Returns

The string type without the trailing dollar sign ($), or never if the input type does not end with a dollar sign.


writablesForProps

function

writablesForProps<T>(defConfig, propsConfig?, options?): [ToWritableSignal<T>, (storesValues) => void]

Shortcut for calling both writablesWithDefault and createPatch in one call.

Type Parameters

T extends object

The type of the properties configuration object.

Parameters

defConfig: T

object containing, for each property, a default value to use in case config does not provide the suitable default value for that property

propsConfig?: PropsConfig<T>

either a store of objects containing, for each property of defConfig, the default value or an object containing for each property of defConfig either a store containing the default value or the default value itself

options?: { [K in string | number | symbol]: undefined | WritableWithDefaultOptions<T[K]> }

object containing, for each property of defConfig, an optional object with the following optional functions: normalizeValue and equal

Returns

[ToWritableSignal<T>, (storesValues) => void]

an array with two items: the first one containing the writables (returned by writablesWithDefault), and the second one containing the patch function (returned by createPatch)

Examples
const defConfig = {propA: 1};
const validation = {propA: {normalizeValue: value => +value}};
const config$ = writable({propA: 5});
const [{propA$}, patch] = writablesForProps(defConfig, config$, validation);
const defConfig = {propA: 1, propB: 2};
const validation = {propA: {normalizeValue: value => +value}};
const config = {propA: 5, propB: writable(3)};
const [{propA$, propB$}, patch] = writablesForProps(defConfig, config, validation);

writablesWithDefault

function

writablesWithDefault<T>(defConfig, propsConfig?, options?): ToWritableSignal<T>

Returns an object containing, for each property of defConfig, a corresponding writable with the normalization and default value logic described in writableWithDefault. Keys in the returned object are the same as the ones present in defConfig, with the exta $ suffix (showing that they are stores).

Type Parameters

T extends object

The type of the default configuration object.

Parameters

defConfig: T

object containing, for each property, a default value to use in case config$ does not provide the suitable default value for that property

propsConfig?: PropsConfig<T>

object defining the config and props

options?: ConfigValidator<T>

object containing, for each property of defConfig, an optional object with the following optional functions: normalizeValue and equal

Returns

ToWritableSignal<T>

an object containing writables

Examples
const defConfig = {propA: 1};
const validation = {propA: {normalizeValue: value => +value}};
const config = writable({propA: 5});
const {propA$} = writablesWithDefault(defConfig, {config}, validation);
const defConfig = {propA: 1, propB: 2};
const validation = {propA: {normalizeValue: value => +value}};
const config = {propA: 5, propB: writable(3)};
const {propA$, propB$} = writablesWithDefault(defConfig, {config}, validation);

writableWithDefault

function

writableWithDefault<T>(defValue, config$, options, own$): WritableSignal<T, T | undefined>

Returns a writable store whose value is either its own value (when it is not undefined) or a default value that comes either from the config$ store (when it is not undefined) or from defValue. If a normalizeValue function is passed in the options, it is called to normalize non-undefined values coming either from the config$ store or from the set or update functions. If a value is invalid (i.e. normalizeValue returns the invalidValue symbol), an error is logged on the console and it is either not set (if it comes from the set or update functions), or the defValue is used instead (if the invalid value comes from the config$ store).

Type Parameters

T

The type of the value.

Parameters

defValue: T

Default value used when both the own value and the config$ value are undefined.

config$: ReadableSignal<undefined | T> = ...

Store containing the default value used when the own value is undefined

options: WritableWithDefaultOptions<T> = {}

Object which can contain the following optional functions: normalizeValue and equal

own$: WritableSignal<undefined | T, undefined | T> = ...

Store containing the own value

Returns

WritableSignal<T, T | undefined>

a writable store with the extra default value and normalization logic described above