openoracle-sdk-js
Version:
OpenOracle Node.js SDK - Intelligent Oracle Routing with Multiple LLM Providers
114 lines (113 loc) • 3.64 kB
TypeScript
/**
* Async utility functions for OpenOracle SDK
*/
/**
* Execute multiple async operations with timeout
*/
export declare function withTimeout<T>(promise: Promise<T>, timeoutMs: number, errorMessage?: string): Promise<T>;
/**
* Execute async operations with retry logic and exponential backoff
*/
export declare function withRetry<T>(operation: () => Promise<T>, options?: {
maxAttempts?: number;
baseDelay?: number;
maxDelay?: number;
backoffFactor?: number;
shouldRetry?: (error: Error, attempt: number) => boolean;
}): Promise<T>;
/**
* Execute multiple promises concurrently with timeout
*/
export declare function gatherWithTimeout<T>(promises: Promise<T>[], timeoutMs: number): Promise<T[]>;
/**
* Execute promises with limited concurrency
*/
export declare function mapConcurrent<T, U>(items: T[], mapper: (item: T, index: number) => Promise<U>, concurrency?: number): Promise<U[]>;
/**
* Execute promises in batches with delay between batches
*/
export declare function batchExecute<T, U>(items: T[], batchSize: number, processor: (batch: T[]) => Promise<U[]>, delayBetweenBatches?: number): Promise<U[]>;
/**
* Promise-based sleep function
*/
export declare function sleep(ms: number): Promise<void>;
/**
* Debounce async function calls
*/
export declare function debounce<T extends (...args: any[]) => Promise<any>>(func: T, waitMs: number): T;
/**
* Throttle async function calls
*/
export declare function throttle<T extends (...args: any[]) => Promise<any>>(func: T, limitMs: number): T;
/**
* Circuit breaker pattern for async operations
*/
export declare class CircuitBreaker<T extends (...args: any[]) => Promise<any>> {
private func;
private options;
private failures;
private lastFailureTime;
private state;
constructor(func: T, options?: {
failureThreshold?: number;
recoveryTimeout?: number;
monitorTimeout?: number;
});
execute(...args: Parameters<T>): Promise<ReturnType<T>>;
private recordFailure;
private reset;
getState(): {
state: string;
failures: number;
lastFailureTime: number;
};
}
/**
* Rate limiter for async operations
*/
export declare class RateLimiter {
private maxTokens;
private refillRate;
private tokens;
private lastRefill;
constructor(maxTokens: number, refillRate: number);
acquire(tokensNeeded?: number): Promise<void>;
private refillTokens;
getTokens(): number;
}
/**
* Promise pool for managing concurrent operations
*/
export declare class PromisePool {
private concurrency;
private running;
private queue;
constructor(concurrency: number);
add<T>(task: () => Promise<T>): Promise<T>;
private process;
getStats(): {
running: number;
queued: number;
};
}
/**
* Async event emitter
*/
export declare class AsyncEventEmitter {
private listeners;
on(event: string, listener: (...args: any[]) => Promise<any>): void;
off(event: string, listener: (...args: any[]) => Promise<any>): void;
emit(event: string, ...args: any[]): Promise<void>;
once(event: string, listener: (...args: any[]) => Promise<any>): void;
}
/**
* Utility to convert callback-based functions to promises
*/
export declare function promisify<T>(func: (callback: (error: Error | null, result?: T) => void) => void): () => Promise<T>;
/**
* Create a cancelable promise
*/
export declare function createCancelablePromise<T>(executor: (resolve: (value: T) => void, reject: (error: Error) => void, isCanceled: () => boolean) => void): {
promise: Promise<T>;
cancel: () => void;
};