UNPKG

3.56 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright 2020 Palantir Technologies, Inc. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.throttle = exports.throttleReactEventCallback = exports.throttleEvent = exports.elementIsOrContains = void 0;
19var functionUtils_1 = require("./functionUtils");
20function elementIsOrContains(element, testElement) {
21 return element === testElement || element.contains(testElement);
22}
23exports.elementIsOrContains = elementIsOrContains;
24/**
25 * Throttle an event on an EventTarget by wrapping it in a
26 * `requestAnimationFrame` call. Returns the event handler that was bound to
27 * given eventName so you can clean up after yourself.
28 *
29 * @see https://developer.mozilla.org/en-US/docs/Web/Events/scroll
30 */
31function throttleEvent(target, eventName, newEventName) {
32 var throttledFunc = throttleImpl(function (event) {
33 target.dispatchEvent(new CustomEvent(newEventName, event));
34 });
35 target.addEventListener(eventName, throttledFunc);
36 return throttledFunc;
37}
38exports.throttleEvent = throttleEvent;
39/**
40 * Throttle a callback by wrapping it in a `requestAnimationFrame` call. Returns
41 * the throttled function.
42 *
43 * @see https://www.html5rocks.com/en/tutorials/speed/animations/
44 */
45function throttleReactEventCallback(callback, options) {
46 if (options === void 0) { options = {}; }
47 var throttledFunc = throttleImpl(callback, function (event2) {
48 if (options.preventDefault) {
49 event2.preventDefault();
50 }
51 },
52 // prevent React from reclaiming the event object before we reference it
53 function (event2) { return event2.persist(); });
54 return throttledFunc;
55}
56exports.throttleReactEventCallback = throttleReactEventCallback;
57/**
58 * Throttle a method by wrapping it in a `requestAnimationFrame` call. Returns
59 * the throttled function.
60 */
61// eslint-disable-next-line @typescript-eslint/ban-types
62function throttle(method) {
63 return throttleImpl(method);
64}
65exports.throttle = throttle;
66// eslint-disable-next-line @typescript-eslint/ban-types
67function throttleImpl(onAnimationFrameRequested, onBeforeIsRunningCheck, onAfterIsRunningCheck) {
68 var isRunning = false;
69 var func = function () {
70 var args = [];
71 for (var _i = 0; _i < arguments.length; _i++) {
72 args[_i] = arguments[_i];
73 }
74 // don't use safeInvoke, because we might have more than its max number
75 // of typed params
76 if (functionUtils_1.isFunction(onBeforeIsRunningCheck)) {
77 onBeforeIsRunningCheck.apply(void 0, args);
78 }
79 if (isRunning) {
80 return;
81 }
82 isRunning = true;
83 if (functionUtils_1.isFunction(onAfterIsRunningCheck)) {
84 onAfterIsRunningCheck.apply(void 0, args);
85 }
86 requestAnimationFrame(function () {
87 onAnimationFrameRequested.apply(void 0, args);
88 isRunning = false;
89 });
90 };
91 return func;
92}
93//# sourceMappingURL=domUtils.js.map
\No newline at end of file