index.cjs.js 5.5 KB

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