observeAsync()
Observes one or more targets asynchronously, batching or debouncing/throttling updates.
observeAsync<T extends (string & keyof this) | Binding>(
target: T,
observer: ObservableObject.AsyncObserver<
[ObservableObject.InferObservedValue<this, T>]
>,
): void;
observeAsync<T1 extends (string & keyof this) | Binding>(
targets: [T1],
observer: ObservableObject.AsyncObserver<
[ObservableObject.InferObservedValue<this, T1>]
>,
): void;
observeAsync<
T1 extends (string & keyof this) | Binding,
T2 extends (string & keyof this) | Binding,
>(
targets: [T1, T2],
observer: ObservableObject.AsyncObserver<
[
ObservableObject.InferObservedValue<this, T1>,
ObservableObject.InferObservedValue<this, T2>,
]
>,
): void;
observeAsync<
T1 extends (string & keyof this) | Binding,
T2 extends (string & keyof this) | Binding,
T3 extends (string & keyof this) | Binding,
>(
targets: [T1, T2, T3],
observer: ObservableObject.AsyncObserver<
[
ObservableObject.InferObservedValue<this, T1>,
ObservableObject.InferObservedValue<this, T2>,
ObservableObject.InferObservedValue<this, T3>,
]
>,
): void;
observeAsync<
T1 extends (string & keyof this) | Binding,
T2 extends (string & keyof this) | Binding,
T3 extends (string & keyof this) | Binding,
T4 extends (string & keyof this) | Binding,
>(
targets: [T1, T2, T3, T4],
observer: ObservableObject.AsyncObserver<
[
ObservableObject.InferObservedValue<this, T1>,
ObservableObject.InferObservedValue<this, T2>,
ObservableObject.InferObservedValue<this, T3>,
ObservableObject.InferObservedValue<this, T4>,
]
>,
): void;
observeAsync(
targets: (string | Binding)[],
observer: ObservableObject.AsyncObserver<any[]>,
): void;
Notes
- Use this method to observe multiple targets or to debounce/throttle updates.
- The observer function is called asynchronously after changes are batched.
Parameters
- target — A property name, binding, or array of property names and/or bindings
- observer — A function that will be called with the current values, or an object with update, debounce, and/or throttle options
Examples
// Observe multiple targets, batching updates
this.observeAsync(["foo", "bar"], (foo, bar) => {
// called once per tick when foo and/or bar change
});
// Debounce a single target (e.g., search-as-you-type)
this.observeAsync("searchQuery", {
update: (query) => this.search(query),
debounce: 300,
});
// Throttle updates (e.g., resize handlers)
this.observeAsync(new Binding("viewport.width"), {
update: (width) => this.handleResize(width),
throttle: 100,
});
Related
class ObservableObject
The base class of all observable objects, which can be placed into a tree structure to enable event handling and data binding.
