/**
 * Request deduplication with promise sharing
 */

export class RequestDeduplicator {
  private inflight = new Map<string, Promise<any>>()
  
  dedupe<T>(key: string, fn: () => Promise<T>): Promise<T> {
    if (this.inflight.has(key)) {
      return this.inflight.get(key)!
    }
    
    const promise = fn().finally(() => {
      this.inflight.delete(key)
    })
    
    this.inflight.set(key, promise)
    return promise
  }
  
  clear(): void {
    this.inflight.clear()
  }
}