@lesjoursfr/edith 2.1.3 → 2.2.0

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.
@@ -1,172 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-explicit-any */
2
- /**
3
- * Based on lodash version of throttle : https://github.com/lodash/lodash/blob/master/throttle.js
4
- */
5
- /**
6
- * Creates a debounced function that delays invoking `func` until after `wait`
7
- * milliseconds have elapsed since the last time the debounced function was
8
- * invoked, or until the next browser frame is drawn. Provide `options` to indicate
9
- * whether `func` should be invoked on the leading and/or trailing edge of the
10
- * `wait` timeout. The `func` is invoked with the last arguments provided to the
11
- * debounced function. Subsequent calls to the debounced function return the
12
- * result of the last `func` invocation.
13
- * **Note:** If `leading` and `trailing` options are `true`, `func` is
14
- * invoked on the trailing edge of the timeout only if the debounced function
15
- * is invoked more than once during the `wait` timeout.
16
- * If `wait` is `0` and `leading` is `false`, `func` invocation is deferred
17
- * until the next tick, similar to `setTimeout` with a timeout of `0`.
18
- * @param {Function} func The function to debounce
19
- * @param {number} wait The number of milliseconds to delay
20
- * @param {Object} [options={}] The options object
21
- * @param {boolean} [options.leading=false] Specify invoking on the leading edge of the timeout
22
- * @param {boolean} [options.trailing=true] Specify invoking on the trailing edge of the timeout
23
- * @param {number} [options.maxWait] The maximum time `func` is allowed to be delayed before it's invoked
24
- * @returns {Function} Returns the new debounced function
25
- */
26
- function debounce<F extends (...args: any) => any>(
27
- func: F,
28
- wait: number,
29
- options: { leading?: boolean; trailing?: boolean; maxWait?: number } = {}
30
- ): (...args: Parameters<F>) => ReturnType<F> {
31
- let lastArgs: Parameters<F> | undefined,
32
- lastThis: any | undefined,
33
- result: ReturnType<F> | undefined,
34
- timerId: ReturnType<typeof setTimeout> | undefined,
35
- lastCallTime: number | undefined;
36
-
37
- let lastInvokeTime: number = 0;
38
- const leading = !!options.leading;
39
- const maxing = "maxWait" in options;
40
- const maxWait = maxing ? Math.max(options.maxWait || 0, wait) : undefined;
41
- const trailing = "trailing" in options ? !!options.trailing : true;
42
-
43
- function invokeFunc(time: number): ReturnType<F> {
44
- const args = lastArgs;
45
- const thisArg = lastThis;
46
-
47
- lastArgs = lastThis = undefined;
48
- lastInvokeTime = time;
49
- result = func.apply(thisArg!, args!);
50
- return result!;
51
- }
52
-
53
- function startTimer(pendingFunc: () => void, wait: number): ReturnType<typeof setTimeout> {
54
- return setTimeout(pendingFunc, wait);
55
- }
56
-
57
- function leadingEdge(time: number): ReturnType<F> {
58
- // Reset any `maxWait` timer.
59
- lastInvokeTime = time;
60
- // Start the timer for the trailing edge.
61
- timerId = startTimer(timerExpired, wait);
62
- // Invoke the leading edge.
63
- return leading ? invokeFunc(time) : result!;
64
- }
65
-
66
- function remainingWait(time: number): number {
67
- const timeSinceLastCall = time - lastCallTime!;
68
- const timeSinceLastInvoke = time - lastInvokeTime;
69
- const timeWaiting = wait - timeSinceLastCall;
70
-
71
- return maxing ? Math.min(timeWaiting, maxWait! - timeSinceLastInvoke) : timeWaiting;
72
- }
73
-
74
- function shouldInvoke(time: number): boolean {
75
- const timeSinceLastCall = time - lastCallTime!;
76
- const timeSinceLastInvoke = time - lastInvokeTime;
77
-
78
- // Either this is the first call, activity has stopped and we're at the
79
- // trailing edge, the system time has gone backwards and we're treating
80
- // it as the trailing edge, or we've hit the `maxWait` limit.
81
- return (
82
- lastCallTime === undefined ||
83
- timeSinceLastCall >= wait ||
84
- timeSinceLastCall < 0 ||
85
- (maxing && timeSinceLastInvoke >= maxWait!)
86
- );
87
- }
88
-
89
- function timerExpired(): ReturnType<F> | undefined {
90
- const time = Date.now();
91
- if (shouldInvoke(time)) {
92
- return trailingEdge(time);
93
- }
94
- // Restart the timer.
95
- timerId = startTimer(timerExpired, remainingWait(time));
96
- }
97
-
98
- function trailingEdge(time: number): ReturnType<F> {
99
- timerId = undefined;
100
-
101
- // Only invoke if we have `lastArgs` which means `func` has been
102
- // debounced at least once.
103
- if (trailing && lastArgs) {
104
- return invokeFunc(time);
105
- }
106
- lastArgs = lastThis = undefined;
107
- return result!;
108
- }
109
-
110
- function debounced(this: any, ...args: Parameters<F>): ReturnType<F> {
111
- const time = Date.now();
112
- const isInvoking = shouldInvoke(time);
113
-
114
- lastArgs = args;
115
- // eslint-disable-next-line @typescript-eslint/no-this-alias
116
- lastThis = this;
117
- lastCallTime = time;
118
-
119
- if (isInvoking) {
120
- if (timerId === undefined) {
121
- return leadingEdge(lastCallTime);
122
- }
123
- if (maxing) {
124
- // Handle invocations in a tight loop.
125
- timerId = startTimer(timerExpired, wait);
126
- return invokeFunc(lastCallTime);
127
- }
128
- }
129
- if (timerId === undefined) {
130
- timerId = startTimer(timerExpired, wait);
131
- }
132
- return result!;
133
- }
134
-
135
- return debounced;
136
- }
137
-
138
- /**
139
- * Creates a throttled function that only invokes `func` at most once per
140
- * every `wait` milliseconds (or once per browser frame). Provide `options` to indicate
141
- * whether `func` should be invoked on the leading and/or trailing edge of the
142
- * `wait` timeout. The `func` is invoked with the last arguments provided to the
143
- * throttled function. Subsequent calls to the throttled function return the
144
- * result of the last `func` invocation.
145
- * **Note:** If `leading` and `trailing` options are `true`, `func` is
146
- * invoked on the trailing edge of the timeout only if the throttled function
147
- * is invoked more than once during the `wait` timeout.
148
- * If `wait` is `0` and `leading` is `false`, `func` invocation is deferred
149
- * until the next tick, similar to `setTimeout` with a timeout of `0`.
150
- * @param {Function} func The function to throttle
151
- * @param {number} wait The number of milliseconds to throttle invocations to
152
- * @param {Object} [options={}] The options object
153
- * @param {boolean} [options.leading=true] Specify invoking on the leading edge of the timeout
154
- * @param {boolean} [options.trailing=true] Specify invoking on the trailing edge of the timeout
155
- * @returns {Function} Returns the new throttled function
156
- */
157
- function throttle<F extends (...args: any) => any>(
158
- func: F,
159
- wait: number,
160
- options: { leading?: boolean; trailing?: boolean } = {}
161
- ): (...args: Parameters<F>) => ReturnType<F> {
162
- const leading = "leading" in options ? !!options.leading : true;
163
- const trailing = "trailing" in options ? !!options.trailing : true;
164
-
165
- return debounce(func, wait, {
166
- leading,
167
- trailing,
168
- maxWait: wait,
169
- });
170
- }
171
-
172
- export { debounce, throttle };