/**
 * Timeout wrapper for async operations.
 *
 * Prevents stuck states when API calls hang due to network issues.
 * Every API call in screens should be wrapped in this.
 */
export class TimeoutError extends Error {
    constructor(label, ms) {
        super(`${label} timed out after ${ms}ms`);
        this.name = 'TimeoutError';
    }
}
export async function withTimeout(promise, ms, label) {
    let timer;
    const timeout = new Promise((_, reject) => {
        timer = setTimeout(() => reject(new TimeoutError(label, ms)), ms);
    });
    try {
        return await Promise.race([promise, timeout]);
    }
    finally {
        clearTimeout(timer);
    }
}
