throttleByAnimationFrame.js 849 B

123456789101112131415161718192021222324252627
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = void 0;
  7. var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
  8. var _raf = _interopRequireDefault(require("rc-util/lib/raf"));
  9. function throttleByAnimationFrame(fn) {
  10. let requestId = null;
  11. const later = args => () => {
  12. requestId = null;
  13. fn.apply(void 0, (0, _toConsumableArray2.default)(args));
  14. };
  15. const throttled = (...args) => {
  16. if (requestId === null) {
  17. requestId = (0, _raf.default)(later(args));
  18. }
  19. };
  20. throttled.cancel = () => {
  21. _raf.default.cancel(requestId);
  22. requestId = null;
  23. };
  24. return throttled;
  25. }
  26. var _default = exports.default = throttleByAnimationFrame;