UNPKG

2.55 kBJavaScriptView Raw
1function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
2
3function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
4
5function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
6
7function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
8
9function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
10
11function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
12
13export default function throttleByAnimationFrame(fn) {
14 var requestId;
15
16 var later = function later(args) {
17 return function () {
18 requestId = null;
19 fn.apply(void 0, _toConsumableArray(args));
20 };
21 };
22
23 var throttled = function throttled() {
24 if (requestId == null) {
25 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
26 args[_key] = arguments[_key];
27 }
28
29 requestId = requestAnimationFrame(later(args));
30 }
31 };
32
33 throttled.cancel = function () {
34 return cancelAnimationFrame(requestId);
35 };
36
37 return throttled;
38}
39export function throttleByAnimationFrameDecorator() {
40 // eslint-disable-next-line func-names
41 return function (target, key, descriptor) {
42 var fn = descriptor.value;
43 var definingProperty = false;
44 return {
45 configurable: true,
46 get: function get() {
47 // eslint-disable-next-line no-prototype-builtins
48 if (definingProperty || this === target.prototype || this.hasOwnProperty(key)) {
49 return fn;
50 }
51
52 var boundFn = throttleByAnimationFrame(fn.bind(this));
53 definingProperty = true;
54 Object.defineProperty(this, key, {
55 value: boundFn,
56 configurable: true,
57 writable: true
58 });
59 definingProperty = false;
60 return boundFn;
61 }
62 };
63 };
64}
\No newline at end of file