Bootstrap

API stores

bindableDerived

function

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

Creates a derived store that binds to multiple stores and triggers a callback when the value changes for any reason.

Type Parameters

T

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

Parameters

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

A readable signal containing a callback function to execute when the value changes.

stores: U

An array of Svelte stores, with the main store at index 0.

adjustValue = ...

A function to adjust the value of the main store. By default, the value of the main store is returned.

equal = ...

A function to determine if two values are equal. Used to compare the ajusted value with the current one.

Returns

WritableSignal<T, T>

The derived store that reflects the combined state of the input stores.


bindableProp

function

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

Creates a computed store that contains the adjusted value of the given store and that triggers a callback when the value changes from the set or update method of the returned writable store.

Type Parameters

T

Parameters

store$: WritableSignal<T, undefined | T>

store to be bound

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

A readable signal containing a callback function to execute when the value changes from the set or update method of the returned writable store.

adjustValue = identity

A function to adjust the value of the store, called in a reactive context each time the value changes or any called dependency changes. By default, the value of store$ is returned.

equal = tansuDefaultEqual

A function to determine if two values are equal.

Returns

WritableSignal<T, T>

A writable store that contains the adjusted value of the given store, with the set or update functions that trigger the onChange$ callback.


createPatch

function

createPatch<T>(stores): <U>(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

Parameters

stores: ToWritableSignal<T>

object of stores

Returns

Function

the patch function

Type Parameters

U extends Partial<T>

Parameters

storesValues?: void | U

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>

Parameters

obj1: Partial<T>

First object

obj2: Partial<T>

Second object

Returns

Partial<T> | null

the object with changed properties


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>

Merge two stores configs into one

Type Parameters

T extends object

Parameters

keys: keyof T[]

the keys of the stores to extract and merge from the two provided configs

config1?: ReadableSignals<T>

the first config

config2?: ReadableSignals<T>

the second config

Returns

ReadableSignals<T>

the merged config


normalizeConfigStores

function

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

Extract and normalize config stores.

Type Parameters

T extends object

Parameters

keys: keyof T[]

the keys of the stores to extract / normalize

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

the config stores

Returns

ReadableSignals<T>

the normalized config stores


ReadableSignals

type alias

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

Type Parameters

T extends object


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

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

Parameters

x: T | ReadableSignal<T>

either a store or a simple value

Returns

ReadableSignal<T>

either x if x is already a store, or readable(x) otherwise


ToWritableSignal

type alias

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

Type Parameters

P


toWritableStore

function

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

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

Type Parameters

T

Parameters

x: T | WritableSignal<T, T>

either a writable store or a simple value

Returns

WritableSignal<T, T>

either x if x is already a store, or writable(x) otherwise


useObservable

function

useObservable<T>(store$): T

Observe a readable store inside of a react component.

Type Parameters

T

Parameters

store$: ReadableSignal<T>

the readable store

Returns

T

the observed value of the store


usePropsAsStore

function

usePropsAsStore<T>(props?): ReadableSignal<Partial<T>>

Turn react props into readable stores.

Type Parameters

T extends object

Parameters

props?: Partial<T>

the props

Returns

ReadableSignal<Partial<T>>

the readable stores


WithoutDollar

type alias

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

Type Parameters

S extends `${string}$`


writablesForProps

function

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

Shortcut for calling both writablesWithDefault and createPatch in one call.

Type Parameters

T extends 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]?: WritableWithDefaultOptions<T[K]> }

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

Returns

[ToWritableSignal<T>, <U>(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

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

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