-
Notifications
You must be signed in to change notification settings - Fork 0
Observable
An extension of the core reactive primitive baseObservable<T>, observable<T> maintains a stateful value alongside reactive subscriptions.
observable<T> supports:
- A callable getter/setter interface: you can read the value by calling with no arguments, or update it by calling with a new value.
- Two-way binding modes (
bind.toandbind.from) enabling flexible directional data flows. - Async update patterns, including an optimistic update strategy allowing immediate UI updates with rollback on failure.
Note: For array-like data requiring fine-grained mutation events, prefer arrayObservable.
| Parameter | Description |
|---|---|
T |
The type of the internal stored value |
(newValue?: T): T-
Acts as both getter and setter:
- If called without arguments, returns the current value.
- If called with a new value, updates the stored value and returns the updated value.
- The current stored value.
- Can be accessed or assigned directly via getter/setter.
Applies an optimistic update strategy:
- Calls the synchronous
updaterfunction to compute a new immutable value based on the current state. - Immediately applies this new value.
- Returns a promise that, if rejected, rolls back the value to the previous state.
This pattern is ideal for UI updates requiring instant feedback that can revert on async failure (e.g., server update errors).
| Name | Type | Description |
|---|---|---|
updater |
(current: T) => T |
Pure synchronous function producing new value |
promise |
Promise<R> |
Async operation representing eventual result |
- The same
promise, augmented with rollback behavior on rejection.
Binding object extending baseObservable<T>.bind with additional properties for two-way data binding:
-
__observable__: Internal reference to this observable instance. -
to: Observable that reflects one-way binding from ViewModel → UI (UI updates don’t propagate back). -
from: Observable that reflects one-way binding from UI → ViewModel (ViewModel updates don’t propagate to UI).
// Create observable with initial value
const count = observable(0);
// Getter
console.log(count()); // 0
console.log(count.value); // 0
// Setter
count(10);
console.log(count()); // 10
// Optimistic update
const updatePromise = fetch('/api/update', { method: 'POST', body: JSON.stringify({ count: 20 }) });
count.optimistic(
current => current + 10,
updatePromise
).catch(() => {
console.log('Update failed, reverted to previous count:', count());
});Constructor interface defining the factory signature and prototype.
<T>(initialValue?: T): observable<T>Creates a new observable instance optionally initialized with initialValue.
| Property | Type | Description |
|---|---|---|
prototype |
observable |
Shared prototype for all instances |
A factory function creating a new observable instance.
- Encapsulates an internal stateful value.
- Provides a callable getter/setter wrapper.
- Delegates core reactive logic to
baseObservable.
observable<T> is a stateful reactive primitive enabling simple value tracking, two-way bindings, and optimistic async updates with rollback support. It simplifies reactive programming by combining value storage with observable notifications and flexible binding strategies.
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!