forked from TabbyML/tabby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(ui): add loading debounce (TabbyML#1569)
* feat(ui): useDebounce * update * update
- Loading branch information
Showing
7 changed files
with
136 additions
and
44 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
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
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
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import React from 'react' | ||
import { debounce, type DebounceSettings } from 'lodash-es' | ||
|
||
import { useLatest } from './use-latest' | ||
import { useUnmount } from './use-unmount' | ||
|
||
type noop = (...args: any[]) => any | ||
|
||
// interface UseDebounceOptions<T = any> extends DebounceSettings { | ||
// onFire?: (value: T) => void | ||
// } | ||
|
||
function useDebounceCallback<T extends noop>( | ||
fn: T, | ||
wait: number, | ||
options?: DebounceSettings | ||
) { | ||
const fnRef = useLatest(fn) | ||
const debounced = React.useMemo( | ||
() => | ||
debounce( | ||
(...args: Parameters<T>): ReturnType<T> => { | ||
return fnRef.current(...args) | ||
}, | ||
wait, | ||
options | ||
), | ||
[] | ||
) | ||
|
||
useUnmount(() => debounced.cancel()) | ||
|
||
return { | ||
run: debounced, | ||
cancel: debounced.cancel, | ||
flush: debounced.flush | ||
} | ||
} | ||
|
||
function useDebounceValue<T>( | ||
value: T, | ||
wait: number, | ||
options?: DebounceSettings | ||
): [T, React.Dispatch<React.SetStateAction<T>>] { | ||
const [debouncedValue, setDebouncedValue] = React.useState(value) | ||
|
||
const { run } = useDebounceCallback( | ||
() => { | ||
setDebouncedValue(value) | ||
}, | ||
wait, | ||
options | ||
) | ||
|
||
React.useEffect(() => { | ||
run() | ||
}, [value]) | ||
|
||
return [debouncedValue, setDebouncedValue] | ||
} | ||
|
||
export { useDebounceCallback, useDebounceValue } |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import React from 'react' | ||
|
||
function useLatest<T>(value: T) { | ||
const ref = React.useRef(value) | ||
ref.current = value | ||
|
||
return ref | ||
} | ||
|
||
export { useLatest } |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import React from 'react' | ||
|
||
import { useLatest } from './use-latest' | ||
|
||
const useUnmount = (fn: () => void) => { | ||
const fnRef = useLatest(fn) | ||
|
||
React.useEffect( | ||
() => () => { | ||
fnRef.current() | ||
}, | ||
[] | ||
) | ||
} | ||
|
||
export { useUnmount } |