blob: da321237816970f152dfa3a2014b7256e3d59b09 [file] [log] [blame]
Copybara botbe50d492023-11-30 00:16:42 +01001"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _apply = require("babel-runtime/core-js/reflect/apply");
8
9var _apply2 = _interopRequireDefault(_apply);
10
11function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
13/**
14 * Since some events can fire at a high rate, the event handler should be limited to execute computationally
15 * expensive operations, such as DOM modifications, inside a single rendered frame.
16 * When listening to e.g. scroll and resize events, the browser tends to fire off more events per
17 * second than are actually useful. For instance, if your event listener sets some element positions, then it
18 * is possible for those positions to be updated multiple times in a single rendered frame. In this case, all of
19 * the layout calculations triggered by setting the elements' positions will be wasted except for the one time that
20 * it runs immediately prior to the browser rendering the updated layout to the screen.
21 * To avoid wasting cycles, we can use requestAnimationFrame to only run the event listener once just before the page
22 * is rendered to the screen.
23 * *
24 * @param callback the function to throttle
25 * @param context optional context of this, default to global
26 * @return {function(...[*])}
27 */
28var fullThrottle = function fullThrottle(callback, context) {
29
30 if (!context) {
31 context = undefined || window;
32 }
33
34 var throttling = false;
35
36 return function () {
37 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
38 args[_key] = arguments[_key];
39 }
40
41 if (!throttling) {
42 throttling = true;
43 window.requestAnimationFrame(function () {
44 throttling = false;
45 return (0, _apply2.default)(callback, context, args);
46 });
47 }
48 };
49};
50
51exports.default = fullThrottle;
52module.exports = exports["default"];