Skip to content
awekrx edited this page May 29, 2026 · 1 revision

dedupe

Import

import { dedupe } from '@dev-suite/decorators/dedupe'

Category

  • method

Use Case

Coalesce concurrent identical requests into one in-flight call.

Replaces

  • Manual in-flight promise maps
  • Duplicate request suppression utilities

Example 1

Without decorator

class AccountService {
  private inFlight = new Map<string, Promise<Account>>();

  async getAccount(id: string) {
    if (!this.inFlight.has(id)) {
      this.inFlight.set(id, this.repo.fetch(id).finally(() => this.inFlight.delete(id)));
    }
    return this.inFlight.get(id)!;
  }
}

With decorator

import { dedupe } from '@dev-suite/decorators/dedupe';

class AccountService {
  @dedupe()
  async getAccount(id: string) {
    return this.repo.fetch(id);
  }
}

Why better

  • Eliminates map/finally bookkeeping from method body.
  • Concurrent duplicates resolve from same promise.

Example 2

Without decorator

class FeatureService {
  private inFlight = new Map<string, Promise<FeatureFlags>>();

  async loadFlags(tenantId: string) {
    if (!this.inFlight.has(tenantId)) {
      this.inFlight.set(tenantId, this.remote.fetchFlags(tenantId).finally(() => this.inFlight.delete(tenantId)));
    }
    return this.inFlight.get(tenantId)!;
  }
}

With decorator

import { dedupe } from '@dev-suite/decorators/dedupe';

class FeatureService {
  @dedupe({ keyResolver: ([tenantId]) => String(tenantId) })
  async loadFlags(tenantId: string) {
    return this.remote.fetchFlags(tenantId);
  }
}

Why better

  • Supports scoped dedupe keys (tenant, user, etc.).
  • Behavior is reusable across services.

Clone this wiki locally