import type { Agile } from '../agile'; import { State, StateConfigInterface, StateIngestConfigInterface } from '../state'; import type { Observer } from '../runtime'; import type { Collection } from '../collection'; export declare class Computed extends State { config: ComputedConfigInterface; isComputeFunctionAsync: boolean; private _computeFunction; deps: Set; hardCodedDeps: Array; readonly isComputed = true; /** * A Computed is an extension of the State Class * that computes its value based on a specified compute function. * * The computed value will be cached to avoid unnecessary recomputes * and is only recomputed when one of its direct dependencies changes. * * Direct dependencies can be States and Collections. * So when, for example, a dependent State value changes, the computed value is recomputed. * * [Learn more..](https://agile-ts.org/docs/core/computed/) * * @public * @param agileInstance - Instance of Agile the Computed belongs to. * @param computeFunction - Function to compute the computed value. * @param config - Configuration object */ constructor(agileInstance: Agile, computeFunction: ComputeFunctionType, config?: CreateComputedConfigInterface); /** * Returns the compute function of the Computed State * * @public */ get computeFunction(): ComputeFunctionType; /** * Assigns a new compute function to the Computed State * and checks whether it's async. * * To update the compute function properly use 'updateComputeFunction()'! * * @internal * @param value - New compute function. */ set computeFunction(value: ComputeFunctionType); /** * Synchronously computes and returns the new value of the Computed Class * and autodetects used dependencies in the compute function. * * @internal * @param config - Configuration object */ private computeSync; /** * Asynchronously computes and returns the new value of the Computed Class. * !! Since its async it can't autodetect used dependencies in the compute function. * * @internal */ private computeAsync; /** * Computes and returns the new value of the Computed Class * and autodetects used dependencies in a synchronous compute function. * * @internal * @param config - Configuration object */ compute(config?: ComputeConfigInterface): Promise; /** * Recomputes the value and ingests it into the runtime. * * @internal * @param config - Configuration object */ computeAndIngest(config?: StateIngestConfigInterface & ComputeConfigInterface): void; /** * Forces a recomputation of the cached value with the compute function. * * [Learn more..](https://agile-ts.org/docs/core/computed/methods/#recompute) * * @public * @param config - Configuration object */ recompute(config?: RecomputeConfigInterface): this; /** * Assigns a new function to the Computed Class for computing its value. * * The dependencies of the new compute function are automatically detected * and accordingly updated. * * An initial computation is performed with the new function * to change the obsolete cached value. * * [Learn more..](https://agile-ts.org/docs/core/computed/methods/#updatecomputefunction) * * @public * @param computeFunction - New function to compute the value of the Computed Class. * @param deps - Hard coded dependencies on which the Computed Class depends. * @param config - Configuration object */ updateComputeFunction(computeFunction: () => ComputedValueType, deps?: Array, config?: RecomputeConfigInterface): this; } export declare type SyncComputeFunctionType = () => ComputedValueType; export declare type AsyncComputeFunctionType = () => Promise; export declare type ComputeFunctionType = SyncComputeFunctionType | AsyncComputeFunctionType; export interface CreateComputedConfigInterface extends StateConfigInterface { /** * Hard-coded dependencies the Computed Class should depend on. * @default [] */ computedDeps?: Array; /** * Whether the Computed should automatically detect * used dependencies in the specified compute method. * * Note that the automatic dependency detection does not work * in an asynchronous compute method! * * @default true if the compute method isn't asynchronous, otherwise false */ autodetect?: boolean; /** * Initial value of the Computed * which is temporarily set until the first computation has been completed. * * Note: Only really relevant if an async compute method is used. * * @default undefined */ initialValue?: Awaited; } export interface ComputedConfigInterface { /** * Whether the Computed can automatically detect * used dependencies in the compute method. * * Note that the automatic dependency detection does not work * in an asynchronous compute method! * * @default true if the compute method isn't asynchronous, otherwise false */ autodetect: boolean; } export interface ComputeConfigInterface { /** * Whether the Computed can automatically detect * used dependencies in the compute method. * * Note that the automatic dependency detection does not work * in an asynchronous compute method! * * @default true */ autodetect?: boolean; } export interface RecomputeConfigInterface extends StateIngestConfigInterface, ComputeConfigInterface { } export declare type DependableAgileInstancesType = State | Collection | Observer;