/** * Cancellation utilities for async operations. * * Provides AbortController-based cancellation for async workflows, * ensuring no mutations occur after timeout and resources are cleaned up. * * These utilities are in the domain layer as they represent domain-agnostic * patterns that can be used by both application and infrastructure layers. */ /** * Error thrown when an operation is cancelled. */ export declare class CancellationError extends Error { constructor(message?: string); } /** * Check if an error is a CancellationError. */ export declare function isCancellationError(error: unknown): error is CancellationError; /** * Options for cancellable operations. */ export interface CancellableOptions { /** Timeout in milliseconds. If not provided, no timeout is set. */ timeoutMs?: number; /** External abort signal to combine with internal timeout. */ signal?: AbortSignal; /** Callback when cancellation occurs (for cleanup). */ onCancel?: () => void; } /** * Result of a cancellable operation. */ export interface CancellableResult { /** The result value, if operation completed successfully. */ value?: T; /** Whether the operation was cancelled (timeout or external signal). */ cancelled: boolean; /** Whether the operation timed out specifically. */ timedOut: boolean; /** Error if operation failed (not due to cancellation). */ error?: Error; } /** * Run an async operation with cancellation support. * * The operation receives an AbortSignal that it should check periodically. * If the operation doesn't respect the signal, it will still be "cancelled" * from the caller's perspective, but the underlying work may continue. * * @param operation - Async function that receives an AbortSignal * @param options - Cancellation options (timeout, external signal, cleanup callback) * @returns Result with value, cancelled status, and any error * * @example * ```typescript * const result = await withCancellation( * async (signal) => { * // Check signal before expensive operations * if (signal.aborted) throw new CancellationError(); * * const data = await fetchData(); * * // Check again before mutations * if (signal.aborted) throw new CancellationError(); * * return processData(data); * }, * { timeoutMs: 5000 } * ); * * if (result.cancelled) { * console.log('Operation was cancelled'); * } else if (result.error) { * console.error('Operation failed:', result.error); * } else { * console.log('Result:', result.value); * } * ``` */ export declare function withCancellation(operation: (signal: AbortSignal) => Promise, options?: CancellableOptions): Promise>; /** * Check if an AbortSignal is aborted and throw CancellationError if so. * Use this at checkpoints within long-running operations. * * @param signal - The abort signal to check * @param message - Optional message for the CancellationError * @throws CancellationError if the signal is aborted * * @example * ```typescript * async function processItems(items: Item[], signal: AbortSignal) { * for (const item of items) { * checkCancellation(signal); * await processItem(item); * } * } * ``` */ export declare function checkCancellation(signal: AbortSignal, message?: string): void; /** * Create a deferred promise that can be resolved/rejected externally. * Useful for creating cancellable promise wrappers. */ export interface Deferred { promise: Promise; resolve: (value: T) => void; reject: (reason?: unknown) => void; } export declare function createDeferred(): Deferred; //# sourceMappingURL=cancellation.d.ts.map