@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 CHANGED
@@ -212,26 +212,80 @@ export interface DebounceOptions {
212
212
  leading?: boolean;
213
213
  trailing?: boolean;
214
214
  }
215
- export interface UseDebounceReturn<Args extends any[]> {
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 useDebounce<Args extends any[]>(func: (...args: Args) => void, delay: number, options?: DebounceOptions): UseDebounceReturn<Args>;
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<Args extends any[]> {
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 useThrottle<Args extends any[]>(func: (...args: Args) => void, delay: number, options?: ThrottleOptions): UseThrottleReturn<Args>;
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 { n as ut, r as dt, t as ft } from "./performance2.js";
4
- import { a as pt, c as mt, i as ht, n as gt, o as _t, r as vt, s as yt, t as bt } from "./state2.js";
5
- import { a as xt, c as St, i as Ct, n as wt, o as Tt, r as Et, s as Dt, t as Ot } from "./ui2.js";
6
- export { St as FuzzyHighlighter, Ct 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, dt as useDebounce, ut as useDebouncedValue, Et as useExpansion, mt as useFilter, yt as useFuzzySearch, _t as useGrouping, lt as useKey, xt as useModal, Tt as useModalActions, Dt as useModalState, pt as useMultipleSelection, ht as useOrder, vt as usePagination, wt as usePin, gt as useSingleSelection, bt as useSort, ft as useThrottle, Ot as useVisibility };
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 };
@@ -5,25 +5,79 @@ export interface DebounceOptions {
5
5
  leading?: boolean;
6
6
  trailing?: boolean;
7
7
  }
8
- export interface UseDebounceReturn<Args extends any[]> {
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 useDebounce<Args extends any[]>(func: (...args: Args) => void, delay: number, options?: DebounceOptions): UseDebounceReturn<Args>;
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<Args extends any[]> {
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 useThrottle<Args extends any[]>(func: (...args: Args) => void, delay: number, options?: ThrottleOptions): UseThrottleReturn<Args>;
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 {};
@@ -1,2 +1,2 @@
1
- import { n as e, r as t, t as n } from "./performance2.js";
2
- export { t as useDebounce, e as useDebouncedValue, n as useThrottle };
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 };
@@ -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, o = {}) {
4
- let [s, c] = r(!1), l = o.maxWait, u = !!o.leading, d = !u && o.trailing === !1 ? !0 : o.trailing ?? !u, f = n(i), p = n(null), m = n(-1), h = n(-1), g = n(null);
5
- t(() => {
6
- f.current = i;
7
- }, [i]);
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
- debouncedFunc: e((...e) => {
17
- c(!0), h.current = Date.now(), g.current = e, m.current === -1 && (m.current = Date.now(), u === !0 && f.current(...e)), l !== void 0 && Date.now() - m.current >= l ? (p.current && clearTimeout(p.current), f.current(...e), m.current = -1, c(!1)) : (p.current && clearTimeout(p.current), p.current = setTimeout(() => {
18
- h.current !== m.current && d !== !1 && f.current(...e), m.current = -1, c(!1);
19
- }, a));
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
- a,
22
- l,
23
- d,
24
- u
17
+ i,
18
+ c,
19
+ u,
20
+ l
25
21
  ]),
26
- cancel: _,
27
- flush: v,
28
- isPending: s
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 a(e, n, a = {}) {
34
- let [o, s] = r(e), { debouncedFunc: c } = i((e) => {
63
+ function s(e, n, i = {}) {
64
+ let [o, s] = r(e), { debouncedFunc: c } = a((e) => {
35
65
  s(e);
36
- }, n, a);
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/useThrottle/useThrottle.ts
43
- function o(i, a, o = {}) {
44
- let [s, c] = r(!1), l = o.leading ?? !0, u = o.trailing ?? !0, d = n(i), f = n(null), p = n(-1), m = n(null);
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
- d.current = i;
47
- }, [i]);
48
- let h = e(() => {
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
- h();
55
- }, [h]), {
56
- throttledFunc: e((...e) => {
57
- c(!0);
58
- let t = Date.now();
59
- if (m.current = e, p.current === -1 ? l : t - p.current >= a) f.current &&= (clearTimeout(f.current), null), d.current(...e), p.current = t, m.current = null, c(!1);
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 = a - (p.current === -1 ? 0 : t - p.current);
138
+ let e = i - (p.current === -1 ? 0 : n - p.current);
62
139
  f.current = setTimeout(() => {
63
- u && m.current ? (d.current(...m.current), p.current = l ? Date.now() : -1) : p.current = -1, m.current = null, f.current = null, c(!1);
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
- a,
68
- l,
69
- u
144
+ i,
145
+ c,
146
+ l
70
147
  ]),
71
- cancel: h,
72
- flush: g,
73
- isPending: s
148
+ cancel: m,
149
+ flush: h,
150
+ isPending: o
74
151
  };
75
152
  }
76
153
  //#endregion
77
- export { a as n, i as r, o as t };
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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@himanshu-sorathiya/react-kit",
3
- "version": "1.0.19",
3
+ "version": "1.0.20",
4
4
  "description": "An opinionated collection of react hooks, and reusable UI components.",
5
5
  "type": "module",
6
6
  "module": "./dist/index.js",