UNPKG

i18n-ai-translate

Version:

Use LLMs to translate your i18n JSON to any language.

36 lines (28 loc) 934 B
import { delay } from "./utils"; export default class RateLimiter { lastAPICall: number | null; delayBetweenCallsMs: number; verboseLogging: boolean; constructor(delayBetweenCallsMs: number, verboseLogging: boolean) { this.lastAPICall = null; this.delayBetweenCallsMs = delayBetweenCallsMs; this.verboseLogging = verboseLogging; } apiCalled(): void { this.lastAPICall = Date.now(); } async wait(): Promise<void> { if (this.lastAPICall) { const timeToWait = this.delayBetweenCallsMs - (Date.now() - this.lastAPICall); if (timeToWait > 0) { await delay(timeToWait); if (this.verboseLogging) { console.log( `RateLimiter | Waiting ${timeToWait}ms before next API call`, ); } } } } }