-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[@mantine/hooks] use-throttled-*: Emit updates on trailing edges
Fixes: #6220
- Loading branch information
Showing
6 changed files
with
94 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 45 additions & 10 deletions
55
packages/@mantine/hooks/src/use-throttled-callback/use-throttled-callback.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,58 @@ | ||
import { useCallback, useRef } from 'react'; | ||
import { useCallback, useEffect, useRef } from 'react'; | ||
import { useCallbackRef } from '../use-callback-ref/use-callback-ref'; | ||
|
||
export function useThrottledCallback<T extends (...args: any[]) => any>(callback: T, wait: number) { | ||
export function useThrottledCallbackWithClearTimeout<T extends (...args: any[]) => any>( | ||
callback: T, | ||
wait: number | ||
) { | ||
const handleCallback = useCallbackRef(callback); | ||
const latestInArgsRef = useRef<Parameters<T>>(); | ||
const latestOutArgsRef = useRef<Parameters<T>>(); | ||
const active = useRef(true); | ||
const timeout = useRef<number>(-1); | ||
const waitRef = useRef(wait); | ||
const timeoutRef = useRef<number>(-1); | ||
|
||
const clearTimeout = () => window.clearTimeout(timeoutRef.current); | ||
|
||
const callThrottledCallback = useCallback( | ||
(...args: Parameters<T>) => { | ||
handleCallback(...args); | ||
latestInArgsRef.current = args; | ||
latestOutArgsRef.current = args; | ||
active.current = false; | ||
}, | ||
[handleCallback] | ||
); | ||
|
||
const timerCallback = useCallback(() => { | ||
if (latestInArgsRef.current && latestInArgsRef.current !== latestOutArgsRef.current) { | ||
callThrottledCallback(...latestInArgsRef.current); | ||
|
||
timeoutRef.current = window.setTimeout(timerCallback, waitRef.current); | ||
} else { | ||
active.current = true; | ||
} | ||
}, [callThrottledCallback]); | ||
|
||
const throttled = useCallback( | ||
(...args: Parameters<T>) => { | ||
if (active.current) { | ||
active.current = false; | ||
handleCallback(...args); | ||
timeout.current = window.setTimeout(() => { | ||
active.current = true; | ||
}, wait); | ||
callThrottledCallback(...args); | ||
timeoutRef.current = window.setTimeout(timerCallback, waitRef.current); | ||
} else { | ||
latestInArgsRef.current = args; | ||
} | ||
}, | ||
[wait] | ||
[callThrottledCallback, timerCallback] | ||
); | ||
|
||
return throttled; | ||
useEffect(() => { | ||
waitRef.current = wait; | ||
}, [wait]); | ||
|
||
return [throttled, clearTimeout] as const; | ||
} | ||
|
||
export function useThrottledCallback<T extends (...args: any[]) => any>(callback: T, wait: number) { | ||
return useThrottledCallbackWithClearTimeout(callback, wait)[0]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 4 additions & 20 deletions
24
packages/@mantine/hooks/src/use-throttled-state/use-throttled-state.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,12 @@ | ||
import { SetStateAction, useCallback, useEffect, useRef, useState } from 'react'; | ||
import { useEffect, useState } from 'react'; | ||
import { useThrottledCallbackWithClearTimeout } from '../use-throttled-callback/use-throttled-callback'; | ||
|
||
export function useThrottledState<T = any>(defaultValue: T, wait: number) { | ||
const [value, setValue] = useState(defaultValue); | ||
const timeoutRef = useRef<number | null>(null); | ||
const active = useRef(true); | ||
|
||
const clearTimeout = () => window.clearTimeout(timeoutRef.current!); | ||
|
||
const throttledSetValue = useCallback( | ||
(newValue: SetStateAction<T>) => { | ||
if (active.current) { | ||
setValue(newValue); | ||
clearTimeout(); | ||
active.current = false; | ||
|
||
timeoutRef.current = window.setTimeout(() => { | ||
active.current = true; | ||
}, wait); | ||
} | ||
}, | ||
[wait] | ||
); | ||
const [setThrottledValue, clearTimeout] = useThrottledCallbackWithClearTimeout(setValue, wait); | ||
|
||
useEffect(() => clearTimeout, []); | ||
|
||
return [value, throttledSetValue] as const; | ||
return [value, setThrottledValue] as const; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 10 additions & 12 deletions
22
packages/@mantine/hooks/src/use-throttled-value/use-throttled-value.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,23 @@ | ||
import { useEffect, useRef, useState } from 'react'; | ||
import { useThrottledCallbackWithClearTimeout } from '../use-throttled-callback/use-throttled-callback'; | ||
|
||
export function useThrottledValue<T>(value: T, wait: number) { | ||
const [throttledValue, setThrottledValue] = useState(value); | ||
const valueRef = useRef(value); | ||
const active = useRef(true); | ||
const timeoutRef = useRef<number>(-1); | ||
|
||
const [throttledSetValue, clearTimeout] = useThrottledCallbackWithClearTimeout( | ||
setThrottledValue, | ||
wait | ||
); | ||
|
||
useEffect(() => { | ||
if (active.current && valueRef.current !== value) { | ||
setThrottledValue(value); | ||
if (value !== valueRef.current) { | ||
valueRef.current = value; | ||
window.clearTimeout(timeoutRef.current); | ||
active.current = false; | ||
|
||
timeoutRef.current = window.setTimeout(() => { | ||
active.current = true; | ||
}, wait); | ||
throttledSetValue(value); | ||
} | ||
}, [value]); | ||
}, [throttledSetValue, value]); | ||
|
||
useEffect(() => () => window.clearTimeout(timeoutRef.current), []); | ||
useEffect(() => clearTimeout, []); | ||
|
||
return throttledValue; | ||
} |