/** * Asynchronous Condition Waiting Utility * * This module provides a robust utility for waiting for asynchronous conditions * to become true with configurable timeouts and intervals. It's designed to * prevent infinite loops and provide clear error messages for debugging. * * Key features: * - Configurable timeout and interval parameters * - Protection against infinite loops and hanging * - Comprehensive error handling and logging * - Graceful handling of predicate failures * - Efficient timing mechanism with cleanup * * Use cases: * - Waiting for DOM elements to appear * - Polling for async operations to complete * - Testing asynchronous state changes * - Waiting for network conditions * * Safety mechanisms: * - Maximum iteration limits to prevent infinite loops * - Timeout enforcement to prevent hanging * - Error handling for predicate failures * - Minimum delay enforcement to prevent CPU spinning */ /** * Configuration options for waitForCondition function */ export interface WaitForConditionOptions { /** Maximum time to wait in milliseconds (default: 2000) */ timeoutMs?: number; /** Interval between predicate checks in milliseconds (default: 25) */ intervalMs?: number; } /** * Wait for a condition to become true with timeout protection * * This function repeatedly executes a predicate function until it returns true * or the timeout is reached. It includes comprehensive safety measures * to prevent infinite loops and hanging behavior. * * Algorithm: * 1. Validate and normalize input parameters * 2. Calculate safety margins and iteration limits * 3. Loop: check timeout, execute predicate, conditionally wait * 4. Handle errors and edge cases gracefully * * Safety features: * - Minimum timeout enforcement (100ms) * - Maximum iteration calculation with safety margin * - Predicate error handling with continued execution * - Timer cleanup mechanism to prevent hanging * - Fallback delays when timer operations fail * * @param predicate - Function that returns boolean or Promise to test condition * @param opts - Configuration options for timeout and interval * @returns Promise that resolves when condition becomes true * @throws Error when timeout is exceeded or max iterations reached * * @example * // Wait for element to appear in DOM * await waitForCondition(() => document.getElementById('my-element') !== null); * * @example * // Wait with custom timeout * await waitForCondition( * () => window.myData !== undefined, * { timeoutMs: 5000, intervalMs: 100 } * ); */ export declare function waitForCondition(predicate: () => boolean | Promise, opts?: WaitForConditionOptions): Promise; export default waitForCondition; //# sourceMappingURL=waitForCondition.d.ts.map