import { type MaybePromise } from '@augment-vir/common'; import { type ExtractEventByType, type ExtractEventTypes, type ListenOptions, ListenTarget, type RemoveListenerCallback, type TypedEventListenerWithRemoval } from 'typed-event-target'; import { type EqualityCheck } from './equality-check.js'; import { type ObservableBase } from './observable-base.js'; import { type ObservableEvents, ObservableValueUpdateEvent } from './observable-events.js'; /** * A callback for listening to observable value changes. * * @category Internal */ export type ObservableListener = (newValue: Value, /** * `oldValue` will be undefined the firs time the listener is called, because in that case there * is only the present value, no past value. */ oldValue: Value | undefined) => MaybePromise; /** * A non-type-safe observable implementation meant as a base for more advanced, type safe * observables like `Observable` or `AsyncObservable`, etc. * * @category Internal */ export declare abstract class AnyObservable implements ObservableBase { protected listenTarget: ListenTarget; /** * The value currently contained with the observable. * * Do not set this directly: use `setValue` instead. (If you try to set this value directly, it * won't fire listeners which defeats the entire purpose of using an observable. */ value: any; /** * The function used to check equality between different values. This can be manually set at any * time to change the function used. */ equalityCheck: EqualityCheck | undefined; /** * This is necessary so we can fire listeners that listen directly to the value, not the emitted * event. */ protected readonly listenerMap: WeakMap, TypedEventListenerWithRemoval>; /** * Dispatch a typed event. Causes all attached listeners listening to this event to be fired. * * @returns The number of listeners that were fired. */ protected dispatch(...args: Parameters): number; /** * Remove all currently attached event listeners. * * @returns The number of listeners that were removed. */ removeAllListeners(): number; /** * Get a count of all currently attached listeners. If a listener is removed, it will no longer * be counted. */ getListenerCount(): number; /** * Set a new value to the observable. The new value will only be set and listeners will only be * fired if the new value is not equal to the current value ("equal" determined by the * `equalityCheck` constructor parameter) or if equality checking is disabled. * * @returns `true` if the new value was set, `false` otherwise. */ setValue(...args: [ newValue: any, /** * Omit to use internal equality check. Set `undefined` to bypass equality check. Set to * an equality check function to use it in place of the internal equality check. */ equalityCheck?: EqualityCheck | undefined ]): boolean; /** * Listen to changes in the observable's value. * * @returns A callback to remove the listener. */ listen( /** If true, the callback will immediately be fired with whatever the current value is. */ fireImmediately: boolean, /** The callback to fire when a new value is set on the observable. */ callback: ObservableListener): RemoveListenerCallback; /** * Removes a listener from the observable. * * @returns `true` if the callback was removed. `false` if the callback was not removed (meaning * it was never added in the first place). */ removeListener(callback: ObservableListener): boolean; /** Clean up all listeners and any other internal state. */ destroy(): void; /** * Listen to any event omitted by the observable rather than just the value changing. * * @returns A callback to remove the listener. */ listenToEvent; }>>(eventDefinition: EventDefinition, listenerCallback: TypedEventListenerWithRemoval>, options?: ListenOptions | undefined): RemoveListenerCallback; }