import { type MaybePromise, type Simplify } from '@augment-vir/common'; import { AsyncObservable } from './async-observable.js'; import { type EqualityCheck } from './equality-check.js'; import { type AllowNoUpdate, type ExcludeNoUpdate } from './no-update.js'; /** * Type for update callback provided to {@link CallbackObservable}, used to update its value. * * @category Internal */ export type UpdateCallback = (params: Params, previousResolvedValue: Value | undefined) => AllowNoUpdate>>; /** * Constructor input for {@link CallbackObservable}. * * @category Internal */ export type CallbackObservableInit = Partial<{ /** Starting value */ defaultValue: Promise> | ExcludeNoUpdate; /** * When a trigger changes (according to the `equalityCheck`), this `updateCallback` will be * called and the observable's value will be updated again if it generates a new value. * Otherwise, the `updateCallback` callback will only be called the first time (if there is no * `defaultValue` init). */ updateCallback: UpdateCallback | undefined; /** * Callback to use to check equality between the current value and new values from * `.setValue()`. If the current value and the new value are equal, the new value will not be * set and no listeners will be called. Set this to undefined to disable equality checking, * which will then set values and fire listeners for every call of `.setValue()`. * * @default json equality */ equalityCheck: EqualityCheck> | Params> | undefined; /** * Starting parameters to use for `updateCallback`. Can be omitted entirely and set later with * `updateTrigger` or `forceUpdate`. */ defaultParams: Params; }>; /** * An observable that updates its value by calling a provided callback with the provided parameters. * The callback will only be triggered if the parameters change. * * @category Observable */ export declare class CallbackObservable extends AsyncObservable { protected static readonly NotSet: unique symbol; /** The callback to call for updating `value`. Uses `lastParams` as its inputs. */ updateCallback: CallbackObservableInit['updateCallback']; /** * The function used to check equality between different values for params or `value`. This can * be manually set at any time to change the function used. */ equalityCheck: CallbackObservableInit['equalityCheck']; /** * The last params for `updateCallback`. This can be set by the constructor, `updateTrigger`, * `forceUpdate`, or by `setParams`. * * Do not set this directly. Use `setParams` instead. */ get lastParams(): Params | undefined; protected internalParams: Params | typeof CallbackObservable.NotSet; constructor(init?: Readonly>); /** * Updates the internal value by calling `updateCallback`. * * @throws `Error` if `updateCallback` or params have not been set yet. */ protected updateFromCallback(): boolean; /** @returns `true` if new params were set, otherwise `false`. */ protected updateLastParams(newParams: Params): boolean; /** * Update the params for `updateCallback`. If the params are not equal to the previous params * (according to the provided or default `equalityCheck`), `updateCallback` will be called and * will update `value`. * * @returns `true` if calling this triggered an update, `false` otherwise. * @throws `Error` if `updateCallback` or params have not been set yet. */ update( /** * This complicated params type allows the args to be empty if Params is undefined but * requires arguments otherwise. */ ...[params]: Exclude extends never ? [] : [Params]): boolean; /** * Updates params without triggering updates. * * @returns `true` if the params were updated, `false` otherwise. */ setParams(params: Params): boolean; /** * Force `updateCallback` to be called again regardless of whatever the given params are equal * to the previous params or not. `value` will still only be updated if the output of * `updateCallback` is new. * * New params are optional here. If none are provided, the last set parameters are used. * * @throws `Error` if `updateCallback` or params have not been set yet. */ forceUpdate(...args: [Params?]): boolean; }