{"version":3,"file":"functionUtils.mjs","names":[],"sources":["../../../src/tools/utils/functionUtils.ts"],"sourcesContent":["import type { TimeoutId } from '../timer'\nimport { setTimeout, clearTimeout } from '../timer'\n\n// use lodash API\nexport function throttle<T extends (...args: any[]) => void>(\n  fn: T,\n  wait: number,\n  options?: { leading?: boolean; trailing?: boolean }\n) {\n  const needLeadingExecution = options?.leading !== undefined ? options.leading : true\n  const needTrailingExecution = options?.trailing !== undefined ? options.trailing : true\n  let inWaitPeriod = false\n  let pendingExecutionWithParameters: Parameters<T> | undefined\n  let pendingTimeoutId: TimeoutId\n\n  return {\n    throttled: (...parameters: Parameters<T>) => {\n      if (inWaitPeriod) {\n        pendingExecutionWithParameters = parameters\n        return\n      }\n      if (needLeadingExecution) {\n        fn(...parameters)\n      } else {\n        pendingExecutionWithParameters = parameters\n      }\n      inWaitPeriod = true\n      pendingTimeoutId = setTimeout(() => {\n        if (needTrailingExecution && pendingExecutionWithParameters) {\n          fn(...pendingExecutionWithParameters)\n        }\n        inWaitPeriod = false\n        pendingExecutionWithParameters = undefined\n      }, wait)\n    },\n    cancel: () => {\n      clearTimeout(pendingTimeoutId)\n      inWaitPeriod = false\n      pendingExecutionWithParameters = undefined\n    },\n  }\n}\n\n// eslint-disable-next-line @typescript-eslint/no-empty-function\nexport function noop() {}\n"],"mappings":";;AAIA,SAAgB,SACd,IACA,MACA,SACA;CACA,MAAM,uBAAuB,SAAS,YAAY,KAAA,IAAY,QAAQ,UAAU;CAChF,MAAM,wBAAwB,SAAS,aAAa,KAAA,IAAY,QAAQ,WAAW;CACnF,IAAI,eAAe;CACnB,IAAI;CACJ,IAAI;CAEJ,OAAO;EACL,YAAY,GAAG,eAA8B;GAC3C,IAAI,cAAc;IAChB,iCAAiC;IACjC;GACF;GACA,IAAI,sBACF,GAAG,GAAG,UAAU;QAEhB,iCAAiC;GAEnC,eAAe;GACf,mBAAmB,iBAAiB;IAClC,IAAI,yBAAyB,gCAC3B,GAAG,GAAG,8BAA8B;IAEtC,eAAe;IACf,iCAAiC,KAAA;GACnC,GAAG,IAAI;EACT;EACA,cAAc;GACZ,aAAa,gBAAgB;GAC7B,eAAe;GACf,iCAAiC,KAAA;EACnC;CACF;AACF;AAGA,SAAgB,OAAO,CAAC"}