Skip to content

Commit fdc9117

Browse files
author
babin
committed
main 🧊 rework throttle, fix use element size
1 parent 64bc8c1 commit fdc9117

File tree

5 files changed

+25
-13
lines changed

5 files changed

+25
-13
lines changed

‎src/hooks/useElementSize/useElementSize.demo.tsx‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const Demo = () => {
88
<p>Resize the box to see changes</p>
99
<textarea
1010
ref={elementSize.ref}
11-
className='200px 200px'
1211
style={{ resize: 'both' }}
1312
value={`width: ${elementSize.value.width}\nheight: ${elementSize.value.height}`}
1413
/>

‎src/hooks/useElementSize/useElementSize.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ export const useElementSize = ((...params: any[]) => {
6868
useEffect(() => {
6969
if (!target && !internalRef) return;
7070
const element = (target ? getElement(target) : internalRef) as Element;
71-
if (element) return;
71+
72+
if (!element) return;
7273
const observer = new ResizeObserver(([entry]) => {
7374
const { inlineSize: width, blockSize: height } = entry.borderBoxSize[0];
74-
7575
setSize({ width, height });
7676
});
7777

‎src/hooks/useThrottleValue/useThrottleValue.demo.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useThrottleValue } from './useThrottleValue';
44
const Demo = () => {
55
const counter = useCounter();
66

7-
const throttledCounterCount = useThrottleValue(counter.value, 500);
7+
const throttledCounterCount = useThrottleValue(counter.value, 2000);
88

99
return (
1010
<div>

‎src/hooks/useThrottleValue/useThrottleValue.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import { useThrottleCallback } from '../useThrottleCallback/useThrottleCallback'
1717
*/
1818
export const useThrottleValue = <Value>(value: Value, delay: number) => {
1919
const previousValueRef = useRef(value);
20-
const [throttledValue, setDebounceValue] = useState(value);
20+
const [throttledValue, setThrottleValue] = useState(value);
2121

22-
const throttledSetState = useThrottleCallback(setDebounceValue, delay);
22+
const throttledSetState = useThrottleCallback(setThrottleValue, delay);
2323

2424
useEffect(() => {
2525
if (previousValueRef.current === value) return;

‎src/utils/helpers/throttle.ts‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,27 @@ export const throttle = <Params extends any[]>(
33
delay: number
44
): ((...args: Params) => void) => {
55
let isCalled = false;
6+
let lastArgs: Params | null = null;
67

7-
return function (this: any, ...args) {
8-
if (!isCalled) {
9-
callback.apply(this, args);
10-
isCalled = true;
11-
setTimeout(() => {
12-
isCalled = false;
13-
}, delay);
8+
const timer = () => {
9+
if (!lastArgs) {
10+
isCalled = false;
11+
return;
1412
}
13+
14+
callback.apply(this, lastArgs);
15+
lastArgs = null;
16+
setTimeout(timer, delay);
17+
};
18+
19+
return function (this: any, ...args: Params) {
20+
if (isCalled) {
21+
lastArgs = args;
22+
return;
23+
}
24+
25+
callback.apply(this, args);
26+
isCalled = true;
27+
setTimeout(timer, delay);
1528
};
1629
};

0 commit comments

Comments
 (0)