es-toolkit 1.39.6 → 1.39.7-dev.1427

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,46 +1,93 @@
1
- import { debounce as debounce$1 } from '../../function/debounce.mjs';
2
-
3
- function debounce(func, debounceMs = 0, options = {}) {
1
+ function debounce(func, wait = 0, options = {}) {
4
2
  if (typeof options !== 'object') {
5
3
  options = {};
6
4
  }
5
+ let pendingArgs = null;
6
+ let pendingThis = null;
7
+ let lastCallTime = null;
8
+ let debounceStartTime = 0;
9
+ let timeoutId = null;
10
+ let lastResult;
7
11
  const { leading = false, trailing = true, maxWait } = options;
8
- const edges = Array(2);
9
- if (leading) {
10
- edges[0] = 'leading';
11
- }
12
- if (trailing) {
13
- edges[1] = 'trailing';
14
- }
15
- let result = undefined;
16
- let pendingAt = null;
17
- const _debounced = debounce$1(function (...args) {
18
- result = func.apply(this, args);
19
- pendingAt = null;
20
- }, debounceMs, { edges });
21
- const debounced = function (...args) {
22
- if (maxWait != null) {
23
- if (pendingAt === null) {
24
- pendingAt = Date.now();
12
+ const hasMaxWait = 'maxWait' in options;
13
+ const maxWaitMs = hasMaxWait ? Math.max(Number(maxWait) || 0, wait) : 0;
14
+ const invoke = (time) => {
15
+ if (pendingArgs !== null) {
16
+ lastResult = func.apply(pendingThis, pendingArgs);
17
+ }
18
+ pendingArgs = pendingThis = null;
19
+ debounceStartTime = time;
20
+ return lastResult;
21
+ };
22
+ const handleLeading = (time) => {
23
+ debounceStartTime = time;
24
+ timeoutId = setTimeout(handleTimeout, wait);
25
+ if (leading && pendingArgs !== null) {
26
+ return invoke(time);
27
+ }
28
+ return lastResult;
29
+ };
30
+ const handleTrailing = (time) => {
31
+ timeoutId = null;
32
+ if (trailing && pendingArgs !== null) {
33
+ return invoke(time);
34
+ }
35
+ return lastResult;
36
+ };
37
+ const checkCanInvoke = (time) => {
38
+ if (lastCallTime === null) {
39
+ return true;
40
+ }
41
+ const timeSinceLastCall = time - lastCallTime;
42
+ const hasDebounceDelayPassed = timeSinceLastCall >= wait || timeSinceLastCall < 0;
43
+ const hasMaxWaitPassed = hasMaxWait && time - debounceStartTime >= maxWaitMs;
44
+ return hasDebounceDelayPassed || hasMaxWaitPassed;
45
+ };
46
+ const calculateRemainingWait = (time) => {
47
+ const timeSinceLastCall = lastCallTime === null ? 0 : time - lastCallTime;
48
+ const remainingDebounceTime = wait - timeSinceLastCall;
49
+ const remainingMaxWaitTime = maxWaitMs - (time - debounceStartTime);
50
+ return hasMaxWait ? Math.min(remainingDebounceTime, remainingMaxWaitTime) : remainingDebounceTime;
51
+ };
52
+ const handleTimeout = () => {
53
+ const currentTime = Date.now();
54
+ if (checkCanInvoke(currentTime)) {
55
+ return handleTrailing(currentTime);
56
+ }
57
+ timeoutId = setTimeout(handleTimeout, calculateRemainingWait(currentTime));
58
+ };
59
+ const debouncedFunction = function (...args) {
60
+ const currentTime = Date.now();
61
+ const canInvoke = checkCanInvoke(currentTime);
62
+ pendingArgs = args;
63
+ pendingThis = this;
64
+ lastCallTime = currentTime;
65
+ if (canInvoke) {
66
+ if (timeoutId === null) {
67
+ return handleLeading(currentTime);
25
68
  }
26
- if (Date.now() - pendingAt >= maxWait) {
27
- result = func.apply(this, args);
28
- pendingAt = Date.now();
29
- _debounced.cancel();
30
- _debounced.schedule();
31
- return result;
69
+ if (hasMaxWait) {
70
+ clearTimeout(timeoutId);
71
+ timeoutId = setTimeout(handleTimeout, wait);
72
+ return invoke(currentTime);
32
73
  }
33
74
  }
34
- _debounced.apply(this, args);
35
- return result;
75
+ if (timeoutId === null) {
76
+ timeoutId = setTimeout(handleTimeout, wait);
77
+ }
78
+ return lastResult;
79
+ };
80
+ debouncedFunction.cancel = () => {
81
+ if (timeoutId !== null) {
82
+ clearTimeout(timeoutId);
83
+ }
84
+ debounceStartTime = 0;
85
+ lastCallTime = pendingArgs = pendingThis = timeoutId = null;
36
86
  };
37
- const flush = () => {
38
- _debounced.flush();
39
- return result;
87
+ debouncedFunction.flush = () => {
88
+ return timeoutId === null ? lastResult : handleTrailing(Date.now());
40
89
  };
41
- debounced.cancel = _debounced.cancel;
42
- debounced.flush = flush;
43
- return debounced;
90
+ return debouncedFunction;
44
91
  }
45
92
 
46
93
  export { debounce };
@@ -5,14 +5,11 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
5
5
  const debounce = require('./debounce.js');
6
6
 
7
7
  function throttle(func, throttleMs = 0, options = {}) {
8
- if (typeof options !== 'object') {
9
- options = {};
10
- }
11
8
  const { leading = true, trailing = true } = options;
12
9
  return debounce.debounce(func, throttleMs, {
13
10
  leading,
14
- trailing,
15
11
  maxWait: throttleMs,
12
+ trailing,
16
13
  });
17
14
  }
18
15
 
@@ -1,14 +1,11 @@
1
1
  import { debounce } from './debounce.mjs';
2
2
 
3
3
  function throttle(func, throttleMs = 0, options = {}) {
4
- if (typeof options !== 'object') {
5
- options = {};
6
- }
7
4
  const { leading = true, trailing = true } = options;
8
5
  return debounce(func, throttleMs, {
9
6
  leading,
10
- trailing,
11
7
  maxWait: throttleMs,
8
+ trailing,
12
9
  });
13
10
  }
14
11
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "es-toolkit",
3
3
  "description": "A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.",
4
- "version": "1.39.6",
4
+ "version": "1.39.7-dev.1427+a3de4256",
5
5
  "homepage": "https://es-toolkit.dev",
6
6
  "bugs": "https://github.com/toss/es-toolkit/issues",
7
7
  "repository": {