computed()
Creates a computed signal.
Example
const name = atom('name', 'John')
const greeting = computed('greeting', () => `Hello ${name.value}!`)
console.log(greeting.value) // 'Hello John!'
computed may also be used as a decorator for creating computed class properties.
Example
class Counter {
max = 100
count = atom<number>(0)
@computed get remaining() {
return this.max - this.count.value
}
}
You may optionally pass in a ComputedOptions when used as a decorator:
Example
class Counter {
max = 100
count = atom<number>(0)
@computed({isEqual: (a, b) => a === b})
get remaining() {
return this.max - this.count.value
}
}
Signature
computed<Value, Diff>(name: string, compute: Function, options?: ComputedOptions<Value, Diff>): Computed<Value, Diff>;
Type parameters
ValueDiff=unknown
Parameters
| Name | Type | Description |
|---|---|---|
name | string | The name of the signal. |
compute | (previousValue: typeof UNINITIALIZED | Value, lastComputedEpoch: number) => Value | WithDiff<Value, Diff> | The function that computes the value of the signal. |
options? | ComputedOptions<Value, Diff> | Options for the signal. |
Returns
Computed<Value, Diff>
Defined in: signia/src/Computed.ts:338
Signature
computed(target: any, key: string, descriptor: PropertyDescriptor): PropertyDescriptor;
Parameters
| Name | Type |
|---|---|
target | any |
key | string |
descriptor | PropertyDescriptor |
Returns
PropertyDescriptor
Defined in: signia/src/Computed.ts:348
Signature
computed<Value, Diff>(options?: ComputedOptions<Value, Diff>): Function;
Type parameters
ValueDiff=unknown
Parameters
| Name | Type |
|---|---|
options? | ComputedOptions<Value, Diff> |
Returns
Function
Signature
(target: any, key: string, descriptor: PropertyDescriptor): PropertyDescriptor;
Parameters
| Name | Type |
|---|---|
target | any |
key | string |
descriptor | PropertyDescriptor |
Returns
PropertyDescriptor
Defined in: signia/src/Computed.ts:354