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.
• 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.
• 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.
WritableSignal
<T
, T
>
A writable signal that derives its value from the provided stores and allows for custom adjustment and equality checks.
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.
• T
The type of the value being stored.
• 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
.
WritableSignal
<T
, T
>
A writable signal that synchronizes with the provided store and triggers the onChange callback when updated.
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.
• T extends object
The type of the object that the stores represent.
• stores: ToWritableSignal
<T
>
The stores to be updated.
Function
• storesValues: Partial
<T
>
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
>
The type of the objects being compared.
• obj1: Partial
<T
>
The first partial object to compare.
• obj2: Partial
<T
>
The second partial object to compare.
Partial
<T
> | null
A partial object containing the properties that have different values, or null
if the objects are identical.
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
>
Merges two configuration stores into one, prioritizing the first store's values when both stores have a value for the same key.
• T extends object
The type of the configuration object.
• 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.
normalizeConfigStores<
T
>(keys
,config
?):ReadableSignals
<T
>
Normalizes configuration stores by converting them into readable signals.
• T extends object
The type of the configuration object.
• 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.
An object containing readable signals for each key in the configuration.
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
.
• T extends object
The object type for which the readable signals are defined.
stateStores<
A
>(inputStores
):object
Using input stores, this function builds an object containing the stores as readable and a global state.
• A extends object
The type of the state 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
The type of the value.
• x: T
| ReadableSignal
<T
>
The value to be converted to a readable store.
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
.
• P
The original type whose properties are to be transformed.
toWritableStore<
T
>(x
):WritableSignal
<T
,T
>
Converts a value or a writable signal into a writable signal.
• T
The type of the value or signal.
• x: T
| WritableSignal
<T
, T
>
The value or writable signal to convert.
WritableSignal
<T
, T
>
useObservable<
T
>(store$
):T
Observe a readable store inside of a react component.
• T
The type of the value emitted by the ReadableSignal.
• store$: ReadableSignal
<T
>
the readable store
T
the observed value of the store
useObservablesProxy<
State
>(stores
):State
Hook to create a proxy object that subscribes to observable stores and triggers re-renders on updates.
• State
The shape of the state object.
• stores: { [K in string as `${K}$`]: ReadableSignal<State[K]> }
An object where keys are suffixed with $
and values are readable signals.
State
usePropsAsStore<
T
>(props
?):ReadableSignal
<Partial
<T
>>
Turn react props into readable stores.
• T extends object
The type of the props object.
• props?: Partial
<T
>
the props
the readable stores
WithoutDollar<
S
>:S
extends `${infer U}$` ?U
:never
A utility type that removes the trailing dollar sign ($
) from a string type.
• S extends `${string}$`
A string type that ends with a dollar sign ($
).
The string type without the trailing dollar sign ($
), or never
if the input type does not end with a dollar sign.
writablesForProps<
T
>(defConfig
,propsConfig
?,options
?): [ToWritableSignal
<T
>, (storesValues
) =>void
]
Shortcut for calling both writablesWithDefault and createPatch in one call.
• T extends object
The type of the properties configuration 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]: undefined | WritableWithDefaultOptions<T[K]> }
object containing, for each property of defConfig
, an optional object with the following optional functions: normalizeValue and equal
[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)
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
The type of the default configuration 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
The type of the value.
• 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