import { type IDisposable } from '../functions/disposer.js'; import type { IResettableModel } from '../models/types.js'; import type { IExpireTracker } from '../structures/expire.js'; import type { IControllableLazyPromise, ILazyPromiseExtension, IResolvedLazyPromise, LazyFactory } from './types.js'; /** * Asynchronous lazy-loading container that initializes via a promise-based factory. * Handles concurrent operations with "latest wins" semantics: multiple refreshes are automatically * coordinated so all awaiting promises receive the final value. Supports extensions for custom behavior. */ export declare class LazyPromise implements IControllableLazyPromise, IDisposable, IResettableModel { private _factory; private readonly _initial; private _instance; /** Current loading state: true = loading, false = loaded, null = not started */ private _state; private _isAsyncStateChange; private _promise; private _expireTracker; private _activeFactoryPromise; private _error; private _ownDisposer?; constructor(factory: LazyFactory, initial?: TInitial); /** Current loading state: true = loading, false = loaded, null = not started */ get isLoading(): boolean | null; /** Returns true if a value of type `T` has been successfully loaded (no error). */ get hasValue(): boolean; get error(): unknown; /** @inheritdoc */ hasResolvedValue(): this is LazyPromise & IResolvedLazyPromise; /** @deprecated Use {@link error} instead. */ get errorMessage(): string | null; get promise(): Promise; get value(): T | TInitial; /** Returns current value without triggering loading. */ get currentValue(): T | TInitial; /** Configures automatic cache expiration using an expire tracker. */ withExpire(tracker: IExpireTracker | undefined): this; withAsyncStateChange(enabled: boolean): this; /** * Extends this instance with additional functionality via in-place mutation. * * **Capabilities:** * - `overrideFactory`: Wrap the factory (logging, retry, caching, etc.) * - `extendShape`: Add custom properties/methods * - `dispose`: Cleanup resources when disposed * * **Type Safety:** * - Use `ILazyPromiseExtension` for universal extensions * - Use `ILazyPromiseExtension` for type-specific extensions * * **Note:** Extensions mutate the instance and can be chained. * * @param extension - Extension configuration * @returns The same instance with applied extensions * * @example * ```typescript * const logged = lazy.extend({ * overrideFactory: (factory) => async (refreshing) => { * console.log('Loading...'); * return await factory(refreshing); * } * }); * ``` */ extend(extension: Partial>): object extends TExtShape ? this : this & TExtShape; /** * Manually sets the value and marks loading as complete. * Clears any errors and restarts the expiration tracker if configured. * * @param res - The value to set * @returns The value that was set */ setInstance(res: T): T; /** * Re-executes the factory to get fresh data. * * **Concurrency handling:** * - Supersedes any in-progress load or refresh * - Multiple concurrent refreshes: latest wins * - All awaiting promises receive the final refreshed value * * @returns Promise resolving to the refreshed value */ refresh(): Promise; reset(): void; dispose(): void; private ensureInstanceLoading; private startLoading; protected onRejected(e: unknown): T | TInitial; protected updateState(isLoading: boolean | null): void; protected setError(err: unknown): void; protected clearError(): void; }