index.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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)
  9. * are most useful.
  10. * @param {Function} callback - A function to be executed after delay milliseconds. The `this` context and all arguments are passed through,
  11. * as-is, to `callback` when the throttled-function is executed.
  12. * @param {object} [options] - An object to configure options.
  13. * @param {boolean} [options.noTrailing] - Optional, defaults to false. If noTrailing is true, callback will only execute every `delay` milliseconds
  14. * while the throttled-function is being called. If noTrailing is false or unspecified, callback will be executed
  15. * one final time after the last throttled-function call. (After the throttled-function has not been called for
  16. * `delay` milliseconds, the internal counter is reset).
  17. * @param {boolean} [options.noLeading] - Optional, defaults to false. If noLeading is false, the first throttled-function call will execute callback
  18. * immediately. If noLeading is true, the first the callback execution will be skipped. It should be noted that
  19. * callback will never executed if both noLeading = true and noTrailing = true.
  20. * @param {boolean} [options.debounceMode] - If `debounceMode` is true (at begin), schedule `clear` to execute after `delay` ms. If `debounceMode` is
  21. * false (at end), schedule `callback` to execute after `delay` ms.
  22. *
  23. * @returns {Function} A new, throttled, function.
  24. */
  25. function throttle (delay, callback, options) {
  26. var _ref = options || {},
  27. _ref$noTrailing = _ref.noTrailing,
  28. noTrailing = _ref$noTrailing === void 0 ? false : _ref$noTrailing,
  29. _ref$noLeading = _ref.noLeading,
  30. noLeading = _ref$noLeading === void 0 ? false : _ref$noLeading,
  31. _ref$debounceMode = _ref.debounceMode,
  32. debounceMode = _ref$debounceMode === void 0 ? undefined : _ref$debounceMode;
  33. /*
  34. * After wrapper has stopped being called, this timeout ensures that
  35. * `callback` is executed at the proper times in `throttle` and `end`
  36. * debounce modes.
  37. */
  38. var timeoutID;
  39. var cancelled = false;
  40. // Keep track of the last time `callback` was executed.
  41. var lastExec = 0;
  42. // Function to clear existing timeout
  43. function clearExistingTimeout() {
  44. if (timeoutID) {
  45. clearTimeout(timeoutID);
  46. }
  47. }
  48. // Function to cancel next exec
  49. function cancel(options) {
  50. var _ref2 = options || {},
  51. _ref2$upcomingOnly = _ref2.upcomingOnly,
  52. upcomingOnly = _ref2$upcomingOnly === void 0 ? false : _ref2$upcomingOnly;
  53. clearExistingTimeout();
  54. cancelled = !upcomingOnly;
  55. }
  56. /*
  57. * The `wrapper` function encapsulates all of the throttling / debouncing
  58. * functionality and when executed will limit the rate at which `callback`
  59. * is executed.
  60. */
  61. function wrapper() {
  62. for (var _len = arguments.length, arguments_ = new Array(_len), _key = 0; _key < _len; _key++) {
  63. arguments_[_key] = arguments[_key];
  64. }
  65. var self = this;
  66. var elapsed = Date.now() - lastExec;
  67. if (cancelled) {
  68. return;
  69. }
  70. // Execute `callback` and update the `lastExec` timestamp.
  71. function exec() {
  72. lastExec = Date.now();
  73. callback.apply(self, arguments_);
  74. }
  75. /*
  76. * If `debounceMode` is true (at begin) this is used to clear the flag
  77. * to allow future `callback` executions.
  78. */
  79. function clear() {
  80. timeoutID = undefined;
  81. }
  82. if (!noLeading && debounceMode && !timeoutID) {
  83. /*
  84. * Since `wrapper` is being called for the first time and
  85. * `debounceMode` is true (at begin), execute `callback`
  86. * and noLeading != true.
  87. */
  88. exec();
  89. }
  90. clearExistingTimeout();
  91. if (debounceMode === undefined && elapsed > delay) {
  92. if (noLeading) {
  93. /*
  94. * In throttle mode with noLeading, if `delay` time has
  95. * been exceeded, update `lastExec` and schedule `callback`
  96. * to execute after `delay` ms.
  97. */
  98. lastExec = Date.now();
  99. if (!noTrailing) {
  100. timeoutID = setTimeout(debounceMode ? clear : exec, delay);
  101. }
  102. } else {
  103. /*
  104. * In throttle mode without noLeading, if `delay` time has been exceeded, execute
  105. * `callback`.
  106. */
  107. exec();
  108. }
  109. } else if (noTrailing !== true) {
  110. /*
  111. * In trailing throttle mode, since `delay` time has not been
  112. * exceeded, schedule `callback` to execute `delay` ms after most
  113. * recent execution.
  114. *
  115. * If `debounceMode` is true (at begin), schedule `clear` to execute
  116. * after `delay` ms.
  117. *
  118. * If `debounceMode` is false (at end), schedule `callback` to
  119. * execute after `delay` ms.
  120. */
  121. timeoutID = setTimeout(debounceMode ? clear : exec, debounceMode === undefined ? delay - elapsed : delay);
  122. }
  123. }
  124. wrapper.cancel = cancel;
  125. // Return the wrapper function.
  126. return wrapper;
  127. }
  128. /* eslint-disable no-undefined */
  129. /**
  130. * Debounce execution of a function. Debouncing, unlike throttling,
  131. * guarantees that a function is only executed a single time, either at the
  132. * very beginning of a series of calls, or at the very end.
  133. *
  134. * @param {number} delay - A zero-or-greater delay in milliseconds. For event callbacks, values around 100 or 250 (or even higher) are most useful.
  135. * @param {Function} callback - A function to be executed after delay milliseconds. The `this` context and all arguments are passed through, as-is,
  136. * to `callback` when the debounced-function is executed.
  137. * @param {object} [options] - An object to configure options.
  138. * @param {boolean} [options.atBegin] - Optional, defaults to false. If atBegin is false or unspecified, callback will only be executed `delay` milliseconds
  139. * after the last debounced-function call. If atBegin is true, callback will be executed only at the first debounced-function call.
  140. * (After the throttled-function has not been called for `delay` milliseconds, the internal counter is reset).
  141. *
  142. * @returns {Function} A new, debounced function.
  143. */
  144. function debounce (delay, callback, options) {
  145. var _ref = options || {},
  146. _ref$atBegin = _ref.atBegin,
  147. atBegin = _ref$atBegin === void 0 ? false : _ref$atBegin;
  148. return throttle(delay, callback, {
  149. debounceMode: atBegin !== false
  150. });
  151. }
  152. exports.debounce = debounce;
  153. exports.throttle = throttle;
  154. //# sourceMappingURL=index.js.map