Skip to content

Commit

Permalink
do not clear the queue as it may still be in use
Browse files Browse the repository at this point in the history
  • Loading branch information
ibnlanre committed Nov 20, 2023
1 parent e5e5574 commit f7d832b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
1 change: 0 additions & 1 deletion src/component/atom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ export function atom<
const executeQueue = (useArgs: UseArgs) => {
dispose("rerun");
queue.forEach((fn) => fn(...useArgs));
queue.clear();
};

const setValueWithArgs = (value: SetStateAction<State>) => {
Expand Down
9 changes: 6 additions & 3 deletions src/utilities/useShallowEffect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ function useShallowCompare(dependencies?: DependencyList): [number] {
*
* @returns {() => () => void} A function to trigger the debounced effect.
*/
const debounce = (effect: EffectCallback, delay: number): (() => void) => {
const debounce = (effect: EffectCallback, delay: number) => {
let destructor: ReturnType<EffectCallback>;
let timeout: NodeJS.Timeout;

const later = (): (() => void) => {
const later = () => {
if (timeout) clearTimeout(timeout);

timeout = setTimeout(() => {
Expand Down Expand Up @@ -109,5 +109,8 @@ export function useDebouncedShallowEffect(
dependencies: DependencyList = [],
delay: number
): void {
useEffect(debounce(effect, delay), useShallowCompare(dependencies));
useEffect(
delay ? debounce(effect, delay) : effect,
useShallowCompare(dependencies)
);
}

0 comments on commit f7d832b

Please sign in to comment.