{"version":3,"file":"throttle.mjs","sources":["../../src/utils/throttle.ts"],"sourcesContent":["import { performance } from 'node:perf_hooks';\n\nexport interface ThrottleOptions {\n  /** Minimum time between two emissions. */\n  intervalMs: number;\n  /**\n   * Clock in milliseconds, injectable for tests. Defaults to performance.now(), which is monotonic:\n   * a wall clock stepping backwards (NTP, VM resume) would hold the throttle shut for the size of\n   * the step plus the interval.\n   */\n  now?: () => number;\n}\n\nexport interface ThrottleDecision {\n  /** True when the caller should log now. */\n  emit: boolean;\n  /** Calls not emitted since the previous emission (reported with the next one). */\n  suppressed: number;\n}\n\nexport interface Throttle {\n  take(): ThrottleDecision;\n}\n\n/**\n * Lets the first call through, then at most one per interval, and counts what it held back.\n * Sibling of createHeartbeatLogger (migrations/heartbeat.ts), which only emits once a full interval\n * has passed and so would drop the first warning of an incident.\n */\nexport const createThrottle = ({\n  intervalMs,\n  now = () => performance.now(),\n}: ThrottleOptions): Throttle => {\n  let lastEmittedAt: number | undefined;\n  let suppressed = 0;\n\n  return {\n    take() {\n      const current = now();\n\n      if (lastEmittedAt !== undefined && current - lastEmittedAt < intervalMs) {\n        suppressed += 1;\n        return { emit: false, suppressed };\n      }\n\n      const held = suppressed;\n      suppressed = 0;\n      lastEmittedAt = current;\n      return { emit: true, suppressed: held };\n    },\n  };\n};\n"],"names":["createThrottle","intervalMs","now","performance","lastEmittedAt","suppressed","take","current","undefined","emit","held"],"mappings":";;AAwBA;;;;AAIC,IACM,MAAMA,cAAAA,GAAiB,CAAC,EAC7BC,UAAU,EACVC,GAAAA,GAAM,IAAMC,WAAAA,CAAYD,GAAG,EAAE,EACb,GAAA;IAChB,IAAIE,aAAAA;AACJ,IAAA,IAAIC,UAAAA,GAAa,CAAA;IAEjB,OAAO;AACLC,QAAAA,IAAAA,CAAAA,GAAAA;AACE,YAAA,MAAMC,OAAAA,GAAUL,GAAAA,EAAAA;AAEhB,YAAA,IAAIE,aAAAA,KAAkBI,SAAAA,IAAaD,OAAAA,GAAUH,aAAAA,GAAgBH,UAAAA,EAAY;gBACvEI,UAAAA,IAAc,CAAA;gBACd,OAAO;oBAAEI,IAAAA,EAAM,KAAA;AAAOJ,oBAAAA;AAAW,iBAAA;AACnC,YAAA;AAEA,YAAA,MAAMK,IAAAA,GAAOL,UAAAA;YACbA,UAAAA,GAAa,CAAA;YACbD,aAAAA,GAAgBG,OAAAA;YAChB,OAAO;gBAAEE,IAAAA,EAAM,IAAA;gBAAMJ,UAAAA,EAAYK;AAAK,aAAA;AACxC,QAAA;AACF,KAAA;AACF;;;;"}