{
  "version": 3,
  "sources": ["../../../../src/limiter/internal/LimiterImpl.mjs.ts"],
  "sourcesContent": ["import type { RunQueue, RunQueueEntry } from 'client-run-queue';\nimport type { DebouncedFunc } from 'lodash-es';\nimport { debounce, throttle } from 'lodash-es';\n\nimport type { Limiter } from '../types/Limiter';\nimport type { LimiterOptions } from '../types/LimiterOptions';\nimport type { LimitMode } from '../types/LimitMode';\n\nconst noOp = () => {};\n\nconst leadingEdgeModes = new Set<LimitMode>(['leading', 'leading-and-trailing']);\nconst trailingEdgeModes = new Set<LimitMode>(['trailing', 'leading-and-trailing']);\n\nexport class LimiterImpl implements Limiter {\n  // Limiter Fields\n\n  public readonly queue: RunQueue;\n\n  // Private Fields\n\n  private queueEntry_: RunQueueEntry<void> | undefined;\n  private lodashLimiter_: DebouncedFunc<() => void> | undefined;\n\n  private readonly id_: string;\n  private readonly options_: Required<LimiterOptions>;\n  private runner_ = noOp;\n\n  // Initialization\n\n  constructor(id: string, options: Required<LimiterOptions>) {\n    this.id_ = id;\n    this.queue = options.queue;\n    this.options_ = options;\n  }\n\n  // Limiter Methods\n\n  public cancel() {\n    this.queueEntry_?.cancel();\n    this.queueEntry_ = undefined;\n\n    // Not actually canceling the lodashLimiter because we don't want the limiting state to reset, but changing the runner to a noOp so it\n    // won't do anything\n    this.runner_ = noOp;\n  }\n\n  public limit(run: () => void) {\n    const { limitMode, limitMSec, limitType, priority } = this.options_;\n\n    if (limitType === 'none') {\n      run();\n      return;\n    }\n\n    this.runner_ = run;\n\n    if (this.lodashLimiter_ === undefined) {\n      const func = () => {\n        this.queueEntry_?.cancel();\n        this.queueEntry_ = this.queue!.schedule(priority, this.id_, () => this.runner_());\n      };\n      const options = {\n        leading: leadingEdgeModes.has(limitMode),\n        trailing: trailingEdgeModes.has(limitMode)\n      };\n\n      switch (limitType) {\n        case 'debounce':\n          this.lodashLimiter_ = debounce(func, limitMSec, options);\n          break;\n        case 'throttle':\n          this.lodashLimiter_ = throttle(func, limitMSec, options);\n          break;\n      }\n    }\n\n    this.lodashLimiter_();\n  }\n}\n"],
  "mappings": "AAEA,SAAS,UAAU,gBAAgB;AAMnC,MAAM,OAAO,MAAM;AAAC;AAEpB,MAAM,mBAAmB,oBAAI,IAAe,CAAC,WAAW,sBAAsB,CAAC;AAC/E,MAAM,oBAAoB,oBAAI,IAAe,CAAC,YAAY,sBAAsB,CAAC;AAE1E,MAAM,YAA+B;AAAA;AAAA,EAgB1C,YAAY,IAAY,SAAmC;AAJ3D,SAAQ,UAAU;AAKhB,SAAK,MAAM;AACX,SAAK,QAAQ,QAAQ;AACrB,SAAK,WAAW;AAAA,EAClB;AAAA;AAAA,EAIO,SAAS;AACd,SAAK,aAAa,OAAO;AACzB,SAAK,cAAc;AAInB,SAAK,UAAU;AAAA,EACjB;AAAA,EAEO,MAAM,KAAiB;AAC5B,UAAM,EAAE,WAAW,WAAW,WAAW,SAAS,IAAI,KAAK;AAE3D,QAAI,cAAc,QAAQ;AACxB,UAAI;AACJ;AAAA,IACF;AAEA,SAAK,UAAU;AAEf,QAAI,KAAK,mBAAmB,QAAW;AACrC,YAAM,OAAO,MAAM;AACjB,aAAK,aAAa,OAAO;AACzB,aAAK,cAAc,KAAK,MAAO,SAAS,UAAU,KAAK,KAAK,MAAM,KAAK,QAAQ,CAAC;AAAA,MAClF;AACA,YAAM,UAAU;AAAA,QACd,SAAS,iBAAiB,IAAI,SAAS;AAAA,QACvC,UAAU,kBAAkB,IAAI,SAAS;AAAA,MAC3C;AAEA,cAAQ,WAAW;AAAA,QACjB,KAAK;AACH,eAAK,iBAAiB,SAAS,MAAM,WAAW,OAAO;AACvD;AAAA,QACF,KAAK;AACH,eAAK,iBAAiB,SAAS,MAAM,WAAW,OAAO;AACvD;AAAA,MACJ;AAAA,IACF;AAEA,SAAK,eAAe;AAAA,EACtB;AACF;",
  "names": []
}
