Skip to content

Commit

Permalink
useDebouncedViewportSearch: removed cancel effect
Browse files Browse the repository at this point in the history
  • Loading branch information
bmingles committed May 8, 2023
1 parent 7d56f97 commit 77a3ae4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
12 changes: 8 additions & 4 deletions packages/jsapi-components/src/useDebouncedViewportSearch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,21 @@ it.each(['', ' '])(
}
);

it('should cancel debounce on unmount', () => {
it('should allow cancelling debounced search function call', () => {
const searchText = 'mock.searchText';
const debounceMs = 400;

const { result, unmount } = renderHook(() =>
const { result } = renderHook(() =>
useDebouncedViewportSearch(viewportData, 'mock.column', debounceMs)
);

result.current(searchText);
const searchFn = result.current;

searchFn(searchText);
jest.advanceTimersByTime(5);
unmount();

searchFn.cancel();

jest.advanceTimersByTime(debounceMs);

expect(TableUtils.makeFilterValue).not.toHaveBeenCalled();
Expand Down
17 changes: 8 additions & 9 deletions packages/jsapi-components/src/useDebouncedViewportSearch.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import type { DebouncedFunc } from 'lodash';
import debounce from 'lodash.debounce';
import type { Table, TreeTable } from '@deephaven/jsapi-types';
import { TableUtils } from '@deephaven/jsapi-utils';
import { useEffect, useMemo } from 'react';
import Log from '@deephaven/log';
import { useMemo } from 'react';
import { UseViewportDataResult } from './useViewportData';

const log = Log.module('useDebouncedViewportSearch');

export const DEBOUNCE_VIEWPORT_SEARCH_MS = 200;

/**
Expand All @@ -20,10 +24,12 @@ export default function useDebouncedViewportSearch<
viewportData: UseViewportDataResult<I, T>,
columnName: string,
debounceMs = DEBOUNCE_VIEWPORT_SEARCH_MS
): (searchText: string) => void {
): DebouncedFunc<(searchText: string) => void> {
const debouncedSearch = useMemo(
() =>
debounce((searchText: string) => {
log.debug(`Applying debounced searchText '${searchText}'`);

if (viewportData.table == null) {
return;
}
Expand All @@ -47,12 +53,5 @@ export default function useDebouncedViewportSearch<
[columnName, debounceMs, viewportData]
);

useEffect(
() => () => {
debouncedSearch.cancel();
},
[debouncedSearch]
);

return debouncedSearch;
}

0 comments on commit 77a3ae4

Please sign in to comment.