index.esm.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* eslint-disable no-undefined,no-param-reassign,no-shadow */
  2. /**
  3. * Throttle execution of a function. Especially useful for rate limiting
  4. * execution of handlers on events like resize and scroll.
  5. *
  6. * @param {number} delay - A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.
  7. * @param {boolean} [noTrailing] - Optional, defaults to false. If noTrailing is true, callback will only execute every `delay` milliseconds while the
  8. * throttled-function is being called. If noTrailing is false or unspecified, callback will be executed one final time
  9. * after the last throttled-function call. (After the throttled-function has not been called for `delay` milliseconds,
  10. * the internal counter is reset).
  11. * @param {Function} callback - A function to be executed after delay milliseconds. The `this` context and all arguments are passed through, as-is,
  12. * to `callback` when the throttled-function is executed.
  13. * @param {boolean} [debounceMode] - If `debounceMode` is true (at begin), schedule `clear` to execute after `delay` ms. If `debounceMode` is false (at end),
  14. * schedule `callback` to execute after `delay` ms.
  15. *
  16. * @returns {Function} A new, throttled, function.
  17. */
  18. function throttle (delay, noTrailing, callback, debounceMode) {
  19. /*
  20. * After wrapper has stopped being called, this timeout ensures that
  21. * `callback` is executed at the proper times in `throttle` and `end`
  22. * debounce modes.
  23. */
  24. var timeoutID;
  25. var cancelled = false; // Keep track of the last time `callback` was executed.
  26. var lastExec = 0; // Function to clear existing timeout
  27. function clearExistingTimeout() {
  28. if (timeoutID) {
  29. clearTimeout(timeoutID);
  30. }
  31. } // Function to cancel next exec
  32. function cancel() {
  33. clearExistingTimeout();
  34. cancelled = true;
  35. } // `noTrailing` defaults to falsy.
  36. if (typeof noTrailing !== 'boolean') {
  37. debounceMode = callback;
  38. callback = noTrailing;
  39. noTrailing = undefined;
  40. }
  41. /*
  42. * The `wrapper` function encapsulates all of the throttling / debouncing
  43. * functionality and when executed will limit the rate at which `callback`
  44. * is executed.
  45. */
  46. function wrapper() {
  47. for (var _len = arguments.length, arguments_ = new Array(_len), _key = 0; _key < _len; _key++) {
  48. arguments_[_key] = arguments[_key];
  49. }
  50. var self = this;
  51. var elapsed = Date.now() - lastExec;
  52. if (cancelled) {
  53. return;
  54. } // Execute `callback` and update the `lastExec` timestamp.
  55. function exec() {
  56. lastExec = Date.now();
  57. callback.apply(self, arguments_);
  58. }
  59. /*
  60. * If `debounceMode` is true (at begin) this is used to clear the flag
  61. * to allow future `callback` executions.
  62. */
  63. function clear() {
  64. timeoutID = undefined;
  65. }
  66. if (debounceMode && !timeoutID) {
  67. /*
  68. * Since `wrapper` is being called for the first time and
  69. * `debounceMode` is true (at begin), execute `callback`.
  70. */
  71. exec();
  72. }
  73. clearExistingTimeout();
  74. if (debounceMode === undefined && elapsed > delay) {
  75. /*
  76. * In throttle mode, if `delay` time has been exceeded, execute
  77. * `callback`.
  78. */
  79. exec();
  80. } else if (noTrailing !== true) {
  81. /*
  82. * In trailing throttle mode, since `delay` time has not been
  83. * exceeded, schedule `callback` to execute `delay` ms after most
  84. * recent execution.
  85. *
  86. * If `debounceMode` is true (at begin), schedule `clear` to execute
  87. * after `delay` ms.
  88. *
  89. * If `debounceMode` is false (at end), schedule `callback` to
  90. * execute after `delay` ms.
  91. */
  92. timeoutID = setTimeout(debounceMode ? clear : exec, debounceMode === undefined ? delay - elapsed : delay);
  93. }
  94. }
  95. wrapper.cancel = cancel; // Return the wrapper function.
  96. return wrapper;
  97. }
  98. /* eslint-disable no-undefined */
  99. /**
  100. * Debounce execution of a function. Debouncing, unlike throttling,
  101. * guarantees that a function is only executed a single time, either at the
  102. * very beginning of a series of calls, or at the very end.
  103. *
  104. * @param {number} delay - A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.
  105. * @param {boolean} [atBegin] - Optional, defaults to false. If atBegin is false or unspecified, callback will only be executed `delay` milliseconds
  106. * after the last debounced-function call. If atBegin is true, callback will be executed only at the first debounced-function call.
  107. * (After the throttled-function has not been called for `delay` milliseconds, the internal counter is reset).
  108. * @param {Function} callback - A function to be executed after delay milliseconds. The `this` context and all arguments are passed through, as-is,
  109. * to `callback` when the debounced-function is executed.
  110. *
  111. * @returns {Function} A new, debounced function.
  112. */
  113. function debounce (delay, atBegin, callback) {
  114. return callback === undefined ? throttle(delay, atBegin, false) : throttle(delay, callback, atBegin !== false);
  115. }
  116. export { debounce, throttle };
  117. //# sourceMappingURL=index.esm.js.map