Copybara bot | be50d49 | 2023-11-30 00:16:42 +0100 | [diff] [blame] | 1 | "use strict"; |
| 2 | |
| 3 | Object.defineProperty(exports, "__esModule", { |
| 4 | value: true |
| 5 | }); |
| 6 | |
| 7 | var _apply = require("babel-runtime/core-js/reflect/apply"); |
| 8 | |
| 9 | var _apply2 = _interopRequireDefault(_apply); |
| 10 | |
| 11 | function _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 | */ |
| 28 | var 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 | |
| 51 | exports.default = fullThrottle; |
| 52 | module.exports = exports["default"]; |