-
Notifications
You must be signed in to change notification settings - Fork 0
Computed
Mubarrat edited this page Aug 8, 2025
·
1 revision
Extends the global Function interface by adding a .computed method.
The .computed method transforms a zero-argument function into a reactive computed observable.
- The computed observable automatically recalculates its value whenever any of its dependent
baseObservableinstances emit a"valuechanged"event. - It allows defining derived reactive state easily and declaratively.
| Parameter | Description |
|---|---|
T |
The return type of the computed function |
const obsA = observable(10);
const obsB = observable(20);
// Create a computed observable summing obsA and obsB values
const computedSum = (() => obsA() + obsB()).computed(obsA, obsB);
console.log(computedSum()); // Outputs: 30
obsA(15);
console.log(computedSum()); // Reactively updates and outputs: 35computed: computedConstructor;- Accepts one or more
baseObservabledependencies. - Returns a
computed<T>observable exposing the derived value reactively.
A reactive computed observable derived from a function.
- Implements all features of a
baseObservable<T>(subscriptions, notifications). - Exposes the current computed value via the
.valuereadonly property. - Updates automatically based on dependency changes.
| Parameter | Description |
|---|---|
T |
The computed value type |
- The current computed value.
- Always consistent with the latest dependency states.
Constructor and static API interface for the .computed method.
<T>(this: () => T, ...observables: baseObservable[]): computed<T>;- Converts a zero-argument function into a reactive computed observable.
- Tracks changes in provided observable dependencies.
- Prototype shared by all
computedinstances. - Useful for extending or introspecting computed observables.
A tagged template function for creating reactive computed strings.
- Processes tagged template literals that may embed
baseObservablevalues. - If no observables are present, returns a plain concatenated static string.
- If observables are present, returns a computed function that recomputes the string reactively whenever any embedded observables change.
| Name | Type | Description |
|---|---|---|
strings |
TemplateStringsArray |
Array of literal string segments from the template |
values |
any[] |
Interpolated values inside the template, some may be observables |
-
stringif no observables detected in the interpolated values. -
computed<string>reactive function if observables are present.
const obsName = observable('Alice');
const obsAge = observable(30);
const greeting = cstr`Hello, ${obsName}! You are ${obsAge} years old.`;
console.log(greeting()); // "Hello, Alice! You are 30 years old."
obsName('Bob');
console.log(greeting()); // Reactively updates to "Hello, Bob! You are 30 years old."-
.computedis a powerful extension method to convert pure functions into reactive computed observables, automatically updating on dependencies. -
computed<T>observables expose derived values with all reactive capabilities. -
cstrenables reactive template string interpolation for dynamic, reactive UI-friendly strings.
If you need any assistance or clarification on contributing, feel free to reach out by creating an issue.
Thank you for considering contributing to the Dom-Builder library!