Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import type { CallerTypedArgs } from '../../../_internal/types.ts';

export type DebouncedFunction<F extends (...args: CallerTypedArgs) => void> = {
type DebouncedFunction<F extends (...args: CallerTypedArgs) => void> = {
(...args: Parameters<F>): void;
cancel: () => void;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { beforeEach, describe, expect, expectTypeOf, it, vi } from 'vitest';

import { renderHookSSR } from '../../../_internal/test-utils/renderHookSSR.tsx';

import { DebouncedFunction } from './debounce.ts';
import { useDebounce } from './useDebounce.ts';

describe('useDebounce', () => {
Expand Down Expand Up @@ -83,7 +82,7 @@ describe('useDebounce', () => {

const { result } = renderHookSSR(() => useDebounce(callback, 100));

expectTypeOf(result.current).toEqualTypeOf<DebouncedFunction<typeof callback>>();
expectTypeOf(result.current).toEqualTypeOf<{ (value: string): void; cancel: () => void }>();
});

it('should cleanup on unmount', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useMemo } from 'react';
import type { CallerTypedArgs } from '../../../_internal/types.ts';
import { usePreservedCallback } from '../../usePreservedCallback/usePreservedCallback.ts';

import { debounce, DebouncedFunction } from './debounce.ts';
import { debounce } from './debounce.ts';

type DebounceOptions = {
leading?: boolean;
Expand All @@ -25,7 +25,7 @@ export function useDebounce<F extends (...args: CallerTypedArgs) => unknown>(
callback: F,
debounceMs: number,
options: DebounceOptions = {}
): DebouncedFunction<F> {
): { (...args: Parameters<F>): void; cancel: () => void } {
const preservedCallback = usePreservedCallback(callback) as F;

const { leading = false, trailing = true } = options;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ export function useThrottle<F extends (...args: CallerTypedArgs) => unknown>(
callback: F,
throttleMs: number,
{ leading = true, trailing = true }: ThrottleOptions = {}
) {
): {
(...args: Parameters<F>): void;
cancel: () => void;
} {
const preservedCallback = usePreservedCallback(callback);

const edges = useMemo(() => {
Expand Down
93 changes: 0 additions & 93 deletions packages/react-simplikit/src/hooks/useDebounce/debounce.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { beforeEach, describe, expect, expectTypeOf, it, vi } from 'vitest';

import { renderHookSSR } from '../../_internal/test-utils/renderHookSSR.tsx';

import { DebouncedFunction } from './debounce.ts';
import { useDebounce } from './useDebounce.ts';

describe('useDebounce', () => {
Expand Down Expand Up @@ -83,7 +82,7 @@ describe('useDebounce', () => {

const { result } = renderHookSSR(() => useDebounce(callback, 100));

expectTypeOf(result.current).toEqualTypeOf<DebouncedFunction<typeof callback>>();
expectTypeOf(result.current).toEqualTypeOf<{ (value: string): void; cancel: () => void }>();
});

it('should cleanup on unmount', async () => {
Expand Down
52 changes: 2 additions & 50 deletions packages/react-simplikit/src/hooks/useDebounce/useDebounce.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
import { useEffect } from 'react';
import { useMemo } from 'react';

import type { CallerTypedArgs } from '../../_internal/types.ts';
import { usePreservedCallback } from '../usePreservedCallback/index.ts';

import { debounce } from './debounce.ts';

type DebounceOptions = {
leading?: boolean;
trailing?: boolean;
};
import { useDebounce as internalUseDebounce } from '../_internal/useDebounce/useDebounce.ts';

/**
* @description
Expand Down Expand Up @@ -66,41 +55,4 @@ type DebounceOptions = {
* [`useDebouncedState`](/hooks/useDebouncedState) to debounce a state update,
* or [`useDebouncedValue`](/hooks/useDebouncedValue) to get a debounced value.
*/
export function useDebounce<F extends (...args: CallerTypedArgs) => unknown>(
callback: F,
debounceMs: number,
options: DebounceOptions = {}
// Written out because `DebouncedFunction` is not exported from the package entry, so consumers emitting declarations cannot name it (TS2742)
): { (...args: Parameters<F>): void; cancel: () => void } {
const preservedCallback = usePreservedCallback(callback) as F;

const { leading = false, trailing = true } = options;

const edges = useMemo(() => {
const _edges: Array<'leading' | 'trailing'> = [];
if (leading) {
_edges.push('leading');
}

if (trailing) {
_edges.push('trailing');
}

return _edges;
}, [leading, trailing]);

const debounced = useMemo(() => {
return debounce<F>(preservedCallback, debounceMs, { edges });
}, [preservedCallback, debounceMs, edges]);

useEffect(
function cancelDebouncedOnUnmount() {
return () => {
debounced.cancel();
};
},
[debounced]
);

return debounced;
}
export const useDebounce = internalUseDebounce;
Loading
Loading