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.
• T
• U extends [WritableSignal
<T
, T
>, ...StoreInput<any>[]
]
• 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.
WritableSignal
<T
, T
>
The derived store that reflects the combined state of the input stores.
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.
• T
• 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.
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<
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.
• T extends object
• stores: ToWritableSignal
<T
>
object of stores
Function
the patch function
• U extends Partial
<T
>
• storesValues?: void
| U
void
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<
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.
• T extends Record
<string
, any
>
• obj1: Partial
<T
>
First object
• obj2: Partial
<T
>
Second object
Partial
<T
> | null
the object with changed properties
isStore(
x
):x is ReadableSignal<any>
Returns true if the provided argument is a store (ReadableSignal).
• x: any
argument that is tested
x is ReadableSignal<any>
true if the argument is a store (ReadableSignal)
mergeConfigStores<
T
>(keys
,config1
?,config2
?):ReadableSignals
<T
>
Merge two stores configs into one
• T extends object
• 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
the merged config
normalizeConfigStores<
T
>(keys
,config
?):ReadableSignals
<T
>
Extract and normalize config stores.
• T extends object
• keys: keyof T
[]
the keys of the stores to extract / normalize
• config?: ReadableSignal
<Partial
<T
>> | ValuesOrReadableSignals
<T
>
the config stores
the normalized config stores
ReadableSignals<
T
>: { [K in keyof T]?: ReadableSignal<T[K] | undefined> }
• T extends object
stateStores<
A
>(inputStores
):object
Using input stores, this function builds an object containing the stores as readable and a global state.
• A extends object
• inputStores: { [K in string | number | symbol as `${K & string}$`]: ReadableSignal<any> }
the input stores
object
the object containing the stores as readable and the global state
state$:
ReadableSignal
<A
>
stores: { [K in string | number | symbol as `${K & string}$`]: ReadableSignal<A[K]> }
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.
• T
• x: T
| ReadableSignal
<T
>
either a store or a simple value
either x if x is already a store, or readable(x) otherwise
ToWritableSignal<
P
>: { [K in keyof P as `${K & string}$`]-?: WritableSignal<P[K], P[K] | undefined> }
• P
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.
• T
• x: T
| WritableSignal
<T
, T
>
either a writable store or a simple value
WritableSignal
<T
, T
>
either x if x is already a store, or writable(x) otherwise
useObservable<
T
>(store$
):T
Observe a readable store inside of a react component.
• T
• store$: ReadableSignal
<T
>
the readable store
T
the observed value of the store
usePropsAsStore<
T
>(props
?):ReadableSignal
<Partial
<T
>>
Turn react props into readable stores.
• T extends object
• props?: Partial
<T
>
the props
the readable stores
WithoutDollar<
S
>:S
extends `${infer U}$` ?U
:never
• S extends `${string}$`
writablesForProps<
T
>(defConfig
,propsConfig
?,options
?): [ToWritableSignal
<T
>, <U
>(storesValues
?) =>void
]
Shortcut for calling both writablesWithDefault and createPatch in one call.
• T extends object
• 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
[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)
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<
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).
• T extends object
• 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
an object containing writables
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<
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).
• T
• 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
WritableSignal
<T
, T
| undefined
>
a writable store with the extra default value and normalization logic described above