UNPKG

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