-
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(Cmdk): support loading results externally
- Loading branch information
1 parent
3b9994e
commit 6489f97
Showing
8 changed files
with
266 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
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,60 @@ | ||
import { useEffect, useMemo, useRef } from "react"; | ||
|
||
import debounce from "lodash.debounce"; | ||
|
||
import { useUnmount } from "./useUnmount"; | ||
|
||
type DebounceOptions = { | ||
leading?: boolean; | ||
maxWait?: number; | ||
trailing?: boolean; | ||
}; | ||
|
||
type ControlFunctions = { | ||
cancel: () => void; | ||
flush: () => void; | ||
isPending: () => boolean; | ||
}; | ||
|
||
export type DebouncedState<T extends (...args: any) => ReturnType<T>> = (( | ||
...args: Parameters<T> | ||
) => ReturnType<T> | undefined) & | ||
ControlFunctions; | ||
|
||
export function useDebounceCallback<T extends (...args: any) => ReturnType<T>>( | ||
func: T, | ||
delay = 500, | ||
options: DebounceOptions = {} | ||
): DebouncedState<T> { | ||
const debouncedFunc = useRef<ReturnType<typeof debounce>>(); | ||
|
||
useUnmount(() => { | ||
if (debouncedFunc.current) { | ||
debouncedFunc.current.cancel(); | ||
} | ||
}); | ||
|
||
const debounced = useMemo(() => { | ||
const debouncedFuncInstance = debounce(func, delay, options); | ||
|
||
const wrappedFunc: DebouncedState<T> = (...args: Parameters<T>) => | ||
debouncedFuncInstance(...args); | ||
|
||
wrappedFunc.cancel = () => { | ||
debouncedFuncInstance.cancel(); | ||
}; | ||
|
||
wrappedFunc.isPending = () => !!debouncedFunc.current; | ||
|
||
wrappedFunc.flush = () => debouncedFuncInstance.flush(); | ||
|
||
return wrappedFunc; | ||
}, [func, delay, options]); | ||
|
||
// Update the debounced function ref whenever func, wait, or options change | ||
useEffect(() => { | ||
debouncedFunc.current = debounce(func, delay, options); | ||
}, [func, delay, options]); | ||
|
||
return debounced; | ||
} |
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,39 @@ | ||
import { useRef, useState } from "react"; | ||
|
||
import type { DebouncedState } from "./useDebounceCallback"; | ||
import { useDebounceCallback } from "./useDebounceCallback"; | ||
|
||
type UseDebounceValueOptions<T> = { | ||
equalityFn?: (left: T, right: T) => boolean; | ||
leading?: boolean; | ||
maxWait?: number; | ||
trailing?: boolean; | ||
}; | ||
|
||
export function useDebounceValue<T>( | ||
initialValue: T | (() => T), | ||
delay: number, | ||
options?: UseDebounceValueOptions<T> | ||
): [T, DebouncedState<(value: T) => void>] { | ||
const eq = options?.equalityFn ?? ((left: T, right: T) => left === right); | ||
const unwrappedInitialValue = | ||
initialValue instanceof Function ? initialValue() : initialValue; | ||
const [debouncedValue, setDebouncedValue] = useState<T>( | ||
unwrappedInitialValue | ||
); | ||
const previousValueRef = useRef<T | undefined>(unwrappedInitialValue); | ||
|
||
const updateDebouncedValue = useDebounceCallback( | ||
setDebouncedValue, | ||
delay, | ||
options | ||
); | ||
|
||
// Update the debounced value if the initial value changes | ||
if (!eq(previousValueRef.current as T, unwrappedInitialValue)) { | ||
updateDebouncedValue(unwrappedInitialValue); | ||
previousValueRef.current = unwrappedInitialValue; | ||
} | ||
|
||
return [debouncedValue, updateDebouncedValue]; | ||
} |
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,14 @@ | ||
import { useEffect, useRef } from "react"; | ||
|
||
export function useUnmount(func: () => void) { | ||
const funcRef = useRef(func); | ||
|
||
funcRef.current = func; | ||
|
||
useEffect( | ||
() => () => { | ||
funcRef.current(); | ||
}, | ||
[] | ||
); | ||
} |