@himanshu-sorathiya/react-kit 1.0.19 → 1.0.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +58 -4
- package/dist/index.js +4 -4
- package/dist/performance.d.ts +58 -4
- package/dist/performance.js +2 -2
- package/dist/performance2.js +171 -50
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -212,26 +212,80 @@ export interface DebounceOptions {
|
|
|
212
212
|
leading?: boolean;
|
|
213
213
|
trailing?: boolean;
|
|
214
214
|
}
|
|
215
|
-
export interface UseDebounceReturn
|
|
215
|
+
export interface UseDebounceReturn {
|
|
216
|
+
run: <Args extends unknown[]>(func: (...args: Args) => void, ...args: Args) => void;
|
|
217
|
+
cancel: () => void;
|
|
218
|
+
flush: () => void;
|
|
219
|
+
isPending: boolean;
|
|
220
|
+
}
|
|
221
|
+
export declare function useDebounce(delay: number, options?: DebounceOptions): UseDebounceReturn;
|
|
222
|
+
export interface UseDebouncedCallbackReturn<Args extends unknown[]> {
|
|
216
223
|
debouncedFunc: (...args: Args) => void;
|
|
217
224
|
cancel: () => void;
|
|
218
225
|
flush: () => void;
|
|
219
226
|
isPending: boolean;
|
|
220
227
|
}
|
|
221
|
-
export declare function
|
|
228
|
+
export declare function useDebouncedCallback<Args extends unknown[]>(func: (...args: Args) => void, delay: number, options?: DebounceOptions): UseDebouncedCallbackReturn<Args>;
|
|
229
|
+
export type UseDebouncedStateReturn<T> = [
|
|
230
|
+
T,
|
|
231
|
+
(value: T) => void,
|
|
232
|
+
{
|
|
233
|
+
isPending: boolean;
|
|
234
|
+
cancel: () => void;
|
|
235
|
+
flush: () => void;
|
|
236
|
+
forceSetValue: (value: T) => void;
|
|
237
|
+
}
|
|
238
|
+
];
|
|
239
|
+
export declare function useDebouncedState<T>(initialValue: T | (() => T), delay: number, options?: DebounceOptions): UseDebouncedStateReturn<T>;
|
|
222
240
|
export type UseDebouncedValueReturn<T> = T;
|
|
223
241
|
export declare function useDebouncedValue<T>(value: T, delay: number, options?: DebounceOptions): UseDebouncedValueReturn<T>;
|
|
242
|
+
export interface RateLimitOptions {
|
|
243
|
+
onRateLimitReached?: () => void;
|
|
244
|
+
refillStrategy?: "burst" | "gradual";
|
|
245
|
+
}
|
|
246
|
+
export interface UseRateLimitReturn {
|
|
247
|
+
run: <Args extends unknown[]>(func: (...args: Args) => void, ...args: Args) => void;
|
|
248
|
+
remaining: number;
|
|
249
|
+
isRateLimited: boolean;
|
|
250
|
+
}
|
|
251
|
+
export declare function useRateLimit(limit: number, windowMs: number, options?: RateLimitOptions): UseRateLimitReturn;
|
|
252
|
+
export interface UseRateLimitedCallbackReturn<Args extends unknown[]> {
|
|
253
|
+
rateLimitedFunc: (...args: Args) => void;
|
|
254
|
+
remaining: number;
|
|
255
|
+
isRateLimited: boolean;
|
|
256
|
+
}
|
|
257
|
+
export declare function useRateLimitedCallback<Args extends unknown[]>(func: (...args: Args) => void, limit: number, windowMs: number, options?: RateLimitOptions): UseRateLimitedCallbackReturn<Args>;
|
|
224
258
|
export interface ThrottleOptions {
|
|
225
259
|
leading?: boolean;
|
|
226
260
|
trailing?: boolean;
|
|
227
261
|
}
|
|
228
|
-
export interface UseThrottleReturn
|
|
262
|
+
export interface UseThrottleReturn {
|
|
263
|
+
run: <Args extends unknown[]>(func: (...args: Args) => void, ...args: Args) => void;
|
|
264
|
+
cancel: () => void;
|
|
265
|
+
flush: () => void;
|
|
266
|
+
isPending: boolean;
|
|
267
|
+
}
|
|
268
|
+
export declare function useThrottle(delay: number, options?: ThrottleOptions): UseThrottleReturn;
|
|
269
|
+
export interface UseThrottledCallbackReturn<Args extends unknown[]> {
|
|
229
270
|
throttledFunc: (...args: Args) => void;
|
|
230
271
|
cancel: () => void;
|
|
231
272
|
flush: () => void;
|
|
232
273
|
isPending: boolean;
|
|
233
274
|
}
|
|
234
|
-
export declare function
|
|
275
|
+
export declare function useThrottledCallback<Args extends unknown[]>(func: (...args: Args) => void, delay: number, options?: ThrottleOptions): UseThrottledCallbackReturn<Args>;
|
|
276
|
+
export type UseThrottledStateReturn<T> = [
|
|
277
|
+
T,
|
|
278
|
+
(value: T) => void,
|
|
279
|
+
{
|
|
280
|
+
isPending: boolean;
|
|
281
|
+
cancel: () => void;
|
|
282
|
+
flush: () => void;
|
|
283
|
+
forceSetValue: (value: T) => void;
|
|
284
|
+
}
|
|
285
|
+
];
|
|
286
|
+
export declare function useThrottledState<T>(initialValue: T | (() => T), delay: number, options?: ThrottleOptions): UseThrottledStateReturn<T>;
|
|
287
|
+
export type UseThrottledValueReturn<T> = T;
|
|
288
|
+
export declare function useThrottledValue<T>(value: T, delay: number, options?: ThrottleOptions): UseThrottledValueReturn<T>;
|
|
235
289
|
export type FilterType = "text" | "number" | "boolean" | "date" | "select" | "multiselect" | "custom";
|
|
236
290
|
export type TextOperator = "contains" | "equals" | "startsWith" | "endsWith" | "notContains";
|
|
237
291
|
export type NumberOperator = "equals" | "greaterThan" | "lessThan" | "greaterThanOrEqual" | "lessThanOrEqual" | "between";
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { addBusinessDays as e, addDays as t, addHours as n, addMilliseconds as r, addMinutes as i, addMonths as a, addSeconds as o, addWeeks as s, addYears as c, clampDate as l, closestTo as u, createDate as d, differenceInBusinessDays as f, differenceInDays as p, differenceInHours as m, differenceInMilliseconds as h, differenceInMinutes as g, differenceInMonths as _, differenceInSeconds as v, differenceInWeeks as y, differenceInYears as b, durationToMilliseconds as x, eachDayOfInterval as S, eachMonthOfInterval as C, eachYearOfInterval as w, endOfDay as T, endOfHour as E, endOfMinute as D, endOfMonth as O, endOfQuarter as k, endOfWeek as A, endOfYear as j, format as M, formatDate as N, formatDuration as P, formatISO as F, formatISO9075 as I, formatRFC2822 as L, formatRFC3339 as R, formatRelativeTime as z, fromUnixTime as B, getDate as V, getDayOfWeek as H, getDayOfYear as U, getDaysInMonth as W, getHours as G, getISOWeek as K, getMilliseconds as q, getMinutes as J, getMonth as Y, getQuarter as X, getSeconds as Z, getTimestamp as Q, getYear as $, intervalToDuration as ee, isAM as te, isAfter as ne, isBefore as re, isDate as ie, isEqual as ae, isFirstDayOfMonth as oe, isFuture as se, isInLeapYear as ce, isLastDayOfMonth as le, isLeapYear as ue, isOverlapping as de, isPM as fe, isPast as pe, isSameDay as me, isSameHour as he, isSameMinute as ge, isSameMonth as _e, isSameQuarter as ve, isSameSecond as ye, isSameTime as be, isSameWeek as xe, isSameYear as Se, isThisMonth as Ce, isThisWeek as we, isThisYear as Te, isToday as Ee, isTomorrow as De, isValid as Oe, isWeekday as ke, isWeekend as Ae, isWithinRange as je, isYesterday as Me, max as Ne, min as Pe, parseISO as Fe, roundToNearestMinutes as Ie, setDate as Le, setHours as Re, setISOWeek as ze, setMilliseconds as Be, setMinutes as Ve, setMonth as He, setSeconds as Ue, setYear as We, startOfDay as Ge, startOfHour as Ke, startOfMinute as qe, startOfMonth as Je, startOfQuarter as Ye, startOfWeek as Xe, startOfYear as Ze, subDays as Qe, subHours as $e, subMilliseconds as et, subMinutes as tt, subMonths as nt, subSeconds as rt, subWeeks as it, subYears as at, toDate as ot, toISOString as st, toUnixTime as ct } from "./datetime.js";
|
|
2
2
|
import { t as lt } from "./events2.js";
|
|
3
|
-
import {
|
|
4
|
-
import { a as
|
|
5
|
-
import { a as
|
|
6
|
-
export {
|
|
3
|
+
import { a as ut, c as dt, i as ft, l as pt, n as mt, o as ht, r as gt, s as _t, t as vt, u as yt } from "./performance2.js";
|
|
4
|
+
import { a as bt, c as xt, i as St, n as Ct, o as wt, r as Tt, s as Et, t as Dt } from "./state2.js";
|
|
5
|
+
import { a as Ot, c as kt, i as At, n as jt, o as Mt, r as Nt, s as Pt, t as Ft } from "./ui2.js";
|
|
6
|
+
export { kt as FuzzyHighlighter, At as ModalLayout, e as addBusinessDays, t as addDays, n as addHours, r as addMilliseconds, i as addMinutes, a as addMonths, o as addSeconds, s as addWeeks, c as addYears, l as clampDate, u as closestTo, d as createDate, f as differenceInBusinessDays, p as differenceInDays, m as differenceInHours, h as differenceInMilliseconds, g as differenceInMinutes, _ as differenceInMonths, v as differenceInSeconds, y as differenceInWeeks, b as differenceInYears, x as durationToMilliseconds, S as eachDayOfInterval, C as eachMonthOfInterval, w as eachYearOfInterval, T as endOfDay, E as endOfHour, D as endOfMinute, O as endOfMonth, k as endOfQuarter, A as endOfWeek, j as endOfYear, M as format, N as formatDate, P as formatDuration, F as formatISO, I as formatISO9075, L as formatRFC2822, R as formatRFC3339, z as formatRelativeTime, B as fromUnixTime, V as getDate, H as getDayOfWeek, U as getDayOfYear, W as getDaysInMonth, G as getHours, K as getISOWeek, q as getMilliseconds, J as getMinutes, Y as getMonth, X as getQuarter, Z as getSeconds, Q as getTimestamp, $ as getYear, ee as intervalToDuration, te as isAM, ne as isAfter, re as isBefore, ie as isDate, ae as isEqual, oe as isFirstDayOfMonth, se as isFuture, ce as isInLeapYear, le as isLastDayOfMonth, ue as isLeapYear, de as isOverlapping, fe as isPM, pe as isPast, me as isSameDay, he as isSameHour, ge as isSameMinute, _e as isSameMonth, ve as isSameQuarter, ye as isSameSecond, be as isSameTime, xe as isSameWeek, Se as isSameYear, Ce as isThisMonth, we as isThisWeek, Te as isThisYear, Ee as isToday, De as isTomorrow, Oe as isValid, ke as isWeekday, Ae as isWeekend, je as isWithinRange, Me as isYesterday, Ne as max, Pe as min, Fe as parseISO, Ie as roundToNearestMinutes, Le as setDate, Re as setHours, ze as setISOWeek, Be as setMilliseconds, Ve as setMinutes, He as setMonth, Ue as setSeconds, We as setYear, Ge as startOfDay, Ke as startOfHour, qe as startOfMinute, Je as startOfMonth, Ye as startOfQuarter, Xe as startOfWeek, Ze as startOfYear, Qe as subDays, $e as subHours, et as subMilliseconds, tt as subMinutes, nt as subMonths, rt as subSeconds, it as subWeeks, at as subYears, ot as toDate, st as toISOString, ct as toUnixTime, yt as useDebounce, pt as useDebouncedCallback, dt as useDebouncedState, _t as useDebouncedValue, Nt as useExpansion, xt as useFilter, Et as useFuzzySearch, wt as useGrouping, lt as useKey, Ot as useModal, Mt as useModalActions, Pt as useModalState, bt as useMultipleSelection, St as useOrder, Tt as usePagination, jt as usePin, ht as useRateLimit, ut as useRateLimitedCallback, Ct as useSingleSelection, Dt as useSort, ft as useThrottle, gt as useThrottledCallback, mt as useThrottledState, vt as useThrottledValue, Ft as useVisibility };
|
package/dist/performance.d.ts
CHANGED
|
@@ -5,25 +5,79 @@ export interface DebounceOptions {
|
|
|
5
5
|
leading?: boolean;
|
|
6
6
|
trailing?: boolean;
|
|
7
7
|
}
|
|
8
|
-
export interface UseDebounceReturn
|
|
8
|
+
export interface UseDebounceReturn {
|
|
9
|
+
run: <Args extends unknown[]>(func: (...args: Args) => void, ...args: Args) => void;
|
|
10
|
+
cancel: () => void;
|
|
11
|
+
flush: () => void;
|
|
12
|
+
isPending: boolean;
|
|
13
|
+
}
|
|
14
|
+
export declare function useDebounce(delay: number, options?: DebounceOptions): UseDebounceReturn;
|
|
15
|
+
export interface UseDebouncedCallbackReturn<Args extends unknown[]> {
|
|
9
16
|
debouncedFunc: (...args: Args) => void;
|
|
10
17
|
cancel: () => void;
|
|
11
18
|
flush: () => void;
|
|
12
19
|
isPending: boolean;
|
|
13
20
|
}
|
|
14
|
-
export declare function
|
|
21
|
+
export declare function useDebouncedCallback<Args extends unknown[]>(func: (...args: Args) => void, delay: number, options?: DebounceOptions): UseDebouncedCallbackReturn<Args>;
|
|
22
|
+
export type UseDebouncedStateReturn<T> = [
|
|
23
|
+
T,
|
|
24
|
+
(value: T) => void,
|
|
25
|
+
{
|
|
26
|
+
isPending: boolean;
|
|
27
|
+
cancel: () => void;
|
|
28
|
+
flush: () => void;
|
|
29
|
+
forceSetValue: (value: T) => void;
|
|
30
|
+
}
|
|
31
|
+
];
|
|
32
|
+
export declare function useDebouncedState<T>(initialValue: T | (() => T), delay: number, options?: DebounceOptions): UseDebouncedStateReturn<T>;
|
|
15
33
|
export type UseDebouncedValueReturn<T> = T;
|
|
16
34
|
export declare function useDebouncedValue<T>(value: T, delay: number, options?: DebounceOptions): UseDebouncedValueReturn<T>;
|
|
35
|
+
export interface RateLimitOptions {
|
|
36
|
+
onRateLimitReached?: () => void;
|
|
37
|
+
refillStrategy?: "burst" | "gradual";
|
|
38
|
+
}
|
|
39
|
+
export interface UseRateLimitReturn {
|
|
40
|
+
run: <Args extends unknown[]>(func: (...args: Args) => void, ...args: Args) => void;
|
|
41
|
+
remaining: number;
|
|
42
|
+
isRateLimited: boolean;
|
|
43
|
+
}
|
|
44
|
+
export declare function useRateLimit(limit: number, windowMs: number, options?: RateLimitOptions): UseRateLimitReturn;
|
|
45
|
+
export interface UseRateLimitedCallbackReturn<Args extends unknown[]> {
|
|
46
|
+
rateLimitedFunc: (...args: Args) => void;
|
|
47
|
+
remaining: number;
|
|
48
|
+
isRateLimited: boolean;
|
|
49
|
+
}
|
|
50
|
+
export declare function useRateLimitedCallback<Args extends unknown[]>(func: (...args: Args) => void, limit: number, windowMs: number, options?: RateLimitOptions): UseRateLimitedCallbackReturn<Args>;
|
|
17
51
|
export interface ThrottleOptions {
|
|
18
52
|
leading?: boolean;
|
|
19
53
|
trailing?: boolean;
|
|
20
54
|
}
|
|
21
|
-
export interface UseThrottleReturn
|
|
55
|
+
export interface UseThrottleReturn {
|
|
56
|
+
run: <Args extends unknown[]>(func: (...args: Args) => void, ...args: Args) => void;
|
|
57
|
+
cancel: () => void;
|
|
58
|
+
flush: () => void;
|
|
59
|
+
isPending: boolean;
|
|
60
|
+
}
|
|
61
|
+
export declare function useThrottle(delay: number, options?: ThrottleOptions): UseThrottleReturn;
|
|
62
|
+
export interface UseThrottledCallbackReturn<Args extends unknown[]> {
|
|
22
63
|
throttledFunc: (...args: Args) => void;
|
|
23
64
|
cancel: () => void;
|
|
24
65
|
flush: () => void;
|
|
25
66
|
isPending: boolean;
|
|
26
67
|
}
|
|
27
|
-
export declare function
|
|
68
|
+
export declare function useThrottledCallback<Args extends unknown[]>(func: (...args: Args) => void, delay: number, options?: ThrottleOptions): UseThrottledCallbackReturn<Args>;
|
|
69
|
+
export type UseThrottledStateReturn<T> = [
|
|
70
|
+
T,
|
|
71
|
+
(value: T) => void,
|
|
72
|
+
{
|
|
73
|
+
isPending: boolean;
|
|
74
|
+
cancel: () => void;
|
|
75
|
+
flush: () => void;
|
|
76
|
+
forceSetValue: (value: T) => void;
|
|
77
|
+
}
|
|
78
|
+
];
|
|
79
|
+
export declare function useThrottledState<T>(initialValue: T | (() => T), delay: number, options?: ThrottleOptions): UseThrottledStateReturn<T>;
|
|
80
|
+
export type UseThrottledValueReturn<T> = T;
|
|
81
|
+
export declare function useThrottledValue<T>(value: T, delay: number, options?: ThrottleOptions): UseThrottledValueReturn<T>;
|
|
28
82
|
|
|
29
83
|
export {};
|
package/dist/performance.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export {
|
|
1
|
+
import { a as e, c as t, i as n, l as r, n as i, o as a, r as o, s, t as c, u as l } from "./performance2.js";
|
|
2
|
+
export { l as useDebounce, r as useDebouncedCallback, t as useDebouncedState, s as useDebouncedValue, a as useRateLimit, e as useRateLimitedCallback, n as useThrottle, o as useThrottledCallback, i as useThrottledState, c as useThrottledValue };
|
package/dist/performance2.js
CHANGED
|
@@ -1,77 +1,198 @@
|
|
|
1
1
|
import { useCallback as e, useEffect as t, useRef as n, useState as r } from "react";
|
|
2
2
|
//#region src/performance/useDebounce/useDebounce.ts
|
|
3
|
-
function i(i, a
|
|
4
|
-
let [
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
let _ = e(() => {
|
|
9
|
-
p.current &&= (clearTimeout(p.current), null), m.current = -1, h.current = -1, g.current = null, c(!1);
|
|
10
|
-
}, []), v = e(() => {
|
|
11
|
-
p.current && g.current && (clearTimeout(p.current), p.current = null, f.current(...g.current), m.current = Date.now(), h.current = -1, g.current = null, c(!1));
|
|
3
|
+
function i(i, a = {}) {
|
|
4
|
+
let [o, s] = r(!1), c = a.maxWait, l = !!a.leading, u = !l && a.trailing === !1 ? !0 : a.trailing ?? !l, d = n(null), f = n(null), p = n(null), m = n(-1), h = n(-1), g = e(() => {
|
|
5
|
+
p.current &&= (clearTimeout(p.current), null), m.current = -1, h.current = -1, f.current = null, d.current = null, s(!1);
|
|
6
|
+
}, []), _ = e(() => {
|
|
7
|
+
p.current && d.current && f.current && (clearTimeout(p.current), p.current = null, d.current(...f.current), m.current = Date.now(), h.current = -1, f.current = null, d.current = null, s(!1));
|
|
12
8
|
}, []);
|
|
13
9
|
return t(() => () => {
|
|
14
|
-
|
|
15
|
-
}, [
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
h.current !== m.current &&
|
|
19
|
-
},
|
|
10
|
+
g();
|
|
11
|
+
}, [g]), {
|
|
12
|
+
run: e((e, ...t) => {
|
|
13
|
+
s(!0), h.current = Date.now(), f.current = t, d.current = e, m.current === -1 && (m.current = Date.now(), l === !0 && d.current(...t)), c !== void 0 && Date.now() - m.current >= c ? (p.current && clearTimeout(p.current), d.current && d.current(...t), m.current = -1, s(!1)) : (p.current && clearTimeout(p.current), p.current = setTimeout(() => {
|
|
14
|
+
h.current !== m.current && u !== !1 && d.current && f.current && d.current(...f.current), m.current = -1, s(!1);
|
|
15
|
+
}, i));
|
|
20
16
|
}, [
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
17
|
+
i,
|
|
18
|
+
c,
|
|
19
|
+
u,
|
|
20
|
+
l
|
|
25
21
|
]),
|
|
26
|
-
cancel:
|
|
27
|
-
flush:
|
|
28
|
-
isPending:
|
|
22
|
+
cancel: g,
|
|
23
|
+
flush: _,
|
|
24
|
+
isPending: o
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
//#endregion
|
|
28
|
+
//#region src/performance/useDebounce/useDebouncedCallback.ts
|
|
29
|
+
function a(r, a, o = {}) {
|
|
30
|
+
let s = n(r);
|
|
31
|
+
t(() => {
|
|
32
|
+
s.current = r;
|
|
33
|
+
}, [r]);
|
|
34
|
+
let { run: c, cancel: l, flush: u, isPending: d } = i(a, o);
|
|
35
|
+
return {
|
|
36
|
+
debouncedFunc: e((...e) => {
|
|
37
|
+
c(s.current, ...e);
|
|
38
|
+
}, [c]),
|
|
39
|
+
cancel: l,
|
|
40
|
+
flush: u,
|
|
41
|
+
isPending: d
|
|
29
42
|
};
|
|
30
43
|
}
|
|
31
44
|
//#endregion
|
|
45
|
+
//#region src/performance/useDebounce/useDebouncedState.ts
|
|
46
|
+
function o(e, t, n = {}) {
|
|
47
|
+
let [i, o] = r(e), { debouncedFunc: s, cancel: c, flush: l, isPending: u } = a((e) => {
|
|
48
|
+
o(e);
|
|
49
|
+
}, t, n);
|
|
50
|
+
return [
|
|
51
|
+
i,
|
|
52
|
+
s,
|
|
53
|
+
{
|
|
54
|
+
isPending: u,
|
|
55
|
+
cancel: c,
|
|
56
|
+
flush: l,
|
|
57
|
+
forceSetValue: o
|
|
58
|
+
}
|
|
59
|
+
];
|
|
60
|
+
}
|
|
61
|
+
//#endregion
|
|
32
62
|
//#region src/performance/useDebounce/useDebouncedValue.ts
|
|
33
|
-
function
|
|
34
|
-
let [o, s] = r(e), { debouncedFunc: c } =
|
|
63
|
+
function s(e, n, i = {}) {
|
|
64
|
+
let [o, s] = r(e), { debouncedFunc: c } = a((e) => {
|
|
35
65
|
s(e);
|
|
36
|
-
}, n,
|
|
66
|
+
}, n, i);
|
|
37
67
|
return t(() => {
|
|
38
68
|
c(e);
|
|
39
69
|
}, [e, c]), o;
|
|
40
70
|
}
|
|
41
71
|
//#endregion
|
|
42
|
-
//#region src/performance/
|
|
43
|
-
function
|
|
44
|
-
let [s, c] = r(
|
|
72
|
+
//#region src/performance/useRateLimit/useRateLimit.ts
|
|
73
|
+
function c(i, a, o) {
|
|
74
|
+
let [s, c] = r(i), l = o?.refillStrategy ?? "burst", u = n(o?.onRateLimitReached);
|
|
45
75
|
t(() => {
|
|
46
|
-
|
|
47
|
-
}, [
|
|
48
|
-
let
|
|
49
|
-
f.current &&= (clearTimeout(f.current), null), p.current = -1, m.current = null, c(!1);
|
|
50
|
-
}, []), g = e(() => {
|
|
51
|
-
m.current && (f.current &&= (clearTimeout(f.current), null), d.current(...m.current), p.current = l ? Date.now() : -1, m.current = null, c(!1));
|
|
52
|
-
}, [l]);
|
|
76
|
+
u.current = o?.onRateLimitReached;
|
|
77
|
+
}, [o?.onRateLimitReached]);
|
|
78
|
+
let d = n(i), [f] = r(() => Date.now()), p = n(f), m = n(null);
|
|
53
79
|
return t(() => () => {
|
|
54
|
-
|
|
55
|
-
}, [
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
80
|
+
m.current && clearTimeout(m.current);
|
|
81
|
+
}, []), {
|
|
82
|
+
run: e((e, ...t) => {
|
|
83
|
+
let n = Date.now(), r = n - p.current;
|
|
84
|
+
if (l === "burst") r >= a && (d.current = i, p.current = n);
|
|
85
|
+
else {
|
|
86
|
+
let e = a / i, t = Math.floor(r / e);
|
|
87
|
+
t > 0 && (d.current = Math.min(i, d.current + t), p.current += t * e);
|
|
88
|
+
}
|
|
89
|
+
if (d.current > 0 ? (--d.current, c(d.current), e(...t)) : u.current && u.current(), m.current && clearTimeout(m.current), d.current < i) {
|
|
90
|
+
let e = a / i, t = l === "burst" ? a - (Date.now() - p.current) : e - (Date.now() - p.current);
|
|
91
|
+
m.current = setTimeout(() => {
|
|
92
|
+
c((e) => Math.min(i, e + 1)), p.current = Date.now(), d.current = Math.min(i, d.current + 1);
|
|
93
|
+
}, Math.max(0, t));
|
|
94
|
+
}
|
|
95
|
+
}, [
|
|
96
|
+
i,
|
|
97
|
+
a,
|
|
98
|
+
l
|
|
99
|
+
]),
|
|
100
|
+
remaining: s,
|
|
101
|
+
isRateLimited: s === 0
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
//#endregion
|
|
105
|
+
//#region src/performance/useRateLimit/useRateLimitedCallback.ts
|
|
106
|
+
function l(r, i, a, o) {
|
|
107
|
+
let s = n(r);
|
|
108
|
+
t(() => {
|
|
109
|
+
s.current = r;
|
|
110
|
+
}, [r]);
|
|
111
|
+
let { run: l, remaining: u, isRateLimited: d } = c(i, a, o);
|
|
112
|
+
return {
|
|
113
|
+
rateLimitedFunc: e((...e) => {
|
|
114
|
+
l(s.current, ...e);
|
|
115
|
+
}, [l]),
|
|
116
|
+
remaining: u,
|
|
117
|
+
isRateLimited: d
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
//#endregion
|
|
121
|
+
//#region src/performance/useThrottle/useThrottle.ts
|
|
122
|
+
function u(i, a = {}) {
|
|
123
|
+
let [o, s] = r(!1), c = a.leading ?? !0, l = a.trailing ?? !0;
|
|
124
|
+
c === !1 && l === !1 && (c = !0, l = !0);
|
|
125
|
+
let u = n(null), d = n(null), f = n(null), p = n(-1), m = e(() => {
|
|
126
|
+
f.current &&= (clearTimeout(f.current), null), p.current = -1, d.current = null, u.current = null, s(!1);
|
|
127
|
+
}, []), h = e(() => {
|
|
128
|
+
d.current && u.current && (f.current &&= (clearTimeout(f.current), null), u.current(...d.current), p.current = c ? Date.now() : -1, d.current = null, u.current = null, s(!1));
|
|
129
|
+
}, [c]);
|
|
130
|
+
return t(() => () => {
|
|
131
|
+
m();
|
|
132
|
+
}, [m]), {
|
|
133
|
+
run: e((e, ...t) => {
|
|
134
|
+
s(!0);
|
|
135
|
+
let n = Date.now();
|
|
136
|
+
if (d.current = t, u.current = e, p.current === -1 ? c : n - p.current >= i) f.current &&= (clearTimeout(f.current), null), u.current(...t), p.current = n, d.current = null, s(!1);
|
|
60
137
|
else if (!f.current) {
|
|
61
|
-
let e =
|
|
138
|
+
let e = i - (p.current === -1 ? 0 : n - p.current);
|
|
62
139
|
f.current = setTimeout(() => {
|
|
63
|
-
|
|
140
|
+
l && d.current && u.current ? (u.current(...d.current), p.current = c ? Date.now() : -1) : p.current = -1, d.current = null, u.current = null, f.current = null, s(!1);
|
|
64
141
|
}, e);
|
|
65
142
|
}
|
|
66
143
|
}, [
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
144
|
+
i,
|
|
145
|
+
c,
|
|
146
|
+
l
|
|
70
147
|
]),
|
|
71
|
-
cancel:
|
|
72
|
-
flush:
|
|
73
|
-
isPending:
|
|
148
|
+
cancel: m,
|
|
149
|
+
flush: h,
|
|
150
|
+
isPending: o
|
|
74
151
|
};
|
|
75
152
|
}
|
|
76
153
|
//#endregion
|
|
77
|
-
|
|
154
|
+
//#region src/performance/useThrottle/useThrottledCallback.ts
|
|
155
|
+
function d(r, i, a = {}) {
|
|
156
|
+
let o = n(r);
|
|
157
|
+
t(() => {
|
|
158
|
+
o.current = r;
|
|
159
|
+
}, [r]);
|
|
160
|
+
let { run: s, cancel: c, flush: l, isPending: d } = u(i, a);
|
|
161
|
+
return {
|
|
162
|
+
throttledFunc: e((...e) => {
|
|
163
|
+
s(o.current, ...e);
|
|
164
|
+
}, [s]),
|
|
165
|
+
cancel: c,
|
|
166
|
+
flush: l,
|
|
167
|
+
isPending: d
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
//#endregion
|
|
171
|
+
//#region src/performance/useThrottle/useThrottledState.ts
|
|
172
|
+
function f(e, t, n = {}) {
|
|
173
|
+
let [i, a] = r(e), { throttledFunc: o, isPending: s, cancel: c, flush: l } = d((e) => {
|
|
174
|
+
a(e);
|
|
175
|
+
}, t, n);
|
|
176
|
+
return [
|
|
177
|
+
i,
|
|
178
|
+
o,
|
|
179
|
+
{
|
|
180
|
+
isPending: s,
|
|
181
|
+
cancel: c,
|
|
182
|
+
flush: l,
|
|
183
|
+
forceSetValue: a
|
|
184
|
+
}
|
|
185
|
+
];
|
|
186
|
+
}
|
|
187
|
+
//#endregion
|
|
188
|
+
//#region src/performance/useThrottle/useThrottledValue.ts
|
|
189
|
+
function p(e, n, i = {}) {
|
|
190
|
+
let [a, o] = r(e), { throttledFunc: s } = d((e) => {
|
|
191
|
+
o(e);
|
|
192
|
+
}, n, i);
|
|
193
|
+
return t(() => {
|
|
194
|
+
s(e);
|
|
195
|
+
}, [e, s]), a;
|
|
196
|
+
}
|
|
197
|
+
//#endregion
|
|
198
|
+
export { l as a, o as c, u as i, a as l, f as n, c as o, d as r, s, p as t, i as u };
|