Skip to content

Commit f841b11

Browse files
committed
chore: Debounce requests
1 parent 1ba363c commit f841b11

File tree

5 files changed

+19
-22
lines changed

5 files changed

+19
-22
lines changed

src/hooks/useCollections.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ describe('useCollections', () => {
1313
const { result, waitForNextUpdate } = renderHook(
1414
() => useCollections(stacApi)
1515
);
16-
expect(result.current.state).toEqual('LOADING');
1716
await waitForNextUpdate();
1817
expect(fetch.mock.calls[0][0]).toEqual('https://fake-stac-api.net/collections');
1918
expect(result.current.collections).toEqual({ data: '12345' });

src/hooks/useCollections.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { useCallback, useEffect, useState } from 'react';
1+
import { useCallback, useEffect, useState, useMemo } from 'react';
22
import StacApi from '../stac-api';
33
import type { GenericObject, LoadingState } from '../types';
4+
import debounce from '../utils/debounce';
45

56
type StacCollectionsHook = {
67
collections?: GenericObject,
@@ -12,7 +13,7 @@ function useCollections(stacApi: StacApi): StacCollectionsHook {
1213
const [ collections, setCollections ] = useState<GenericObject>();
1314
const [ state, setState ] = useState<LoadingState>('IDLE');
1415

15-
const getCollections = useCallback(
16+
const _getCollections = useCallback(
1617
() => {
1718
setState('LOADING');
1819
setCollections(undefined);
@@ -24,8 +25,9 @@ function useCollections(stacApi: StacApi): StacCollectionsHook {
2425
},
2526
[stacApi]
2627
);
28+
const getCollections = useMemo(() => debounce(_getCollections), [_getCollections]);
2729

28-
useEffect(() => getCollections() ,[getCollections]);
30+
useEffect(() => getCollections(), [getCollections]);
2931

3032
return {
3133
collections,

src/hooks/useStacSearch.test.ts

-15
Original file line numberDiff line numberDiff line change
@@ -111,21 +111,6 @@ describe('useStacSearch', () => {
111111
expect(result.current.results).toEqual({ data: '12345' });
112112
});
113113

114-
it('sets state', async () => {
115-
fetch.mockResponseOnce(JSON.stringify({ data: '12345' }));
116-
117-
const { result, waitForNextUpdate } = renderHook(
118-
() => useStacSearch(stacApi)
119-
);
120-
121-
act(() => result.current.setDateRangeTo('2022-05-17'));
122-
act(() => result.current.submit());
123-
expect(result.current.state).toEqual('LOADING');
124-
await waitForNextUpdate();
125-
expect(result.current.results).toEqual({ data: '12345' });
126-
expect(result.current.state).toEqual('IDLE');
127-
});
128-
129114
it('handles error with JSON response', async () => {
130115
fetch.mockResponseOnce(JSON.stringify({ error: 'Wrong query' }), { status: 400, statusText: 'Bad Request' });
131116

src/hooks/useStacSearch.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { useCallback, useState } from 'react';
1+
import { useCallback, useState, useMemo } from 'react';
22
import StacApi from '../stac-api';
3-
3+
import debounce from '../utils/debounce';
44
import type { ApiError, LoadingState } from '../types';
55
import type {
66
Link,
@@ -104,12 +104,13 @@ function useStacSearch(stacApi: StacApi): StacSearchHook {
104104
[flipPage, previousPageConfig]
105105
);
106106

107-
const submit = useCallback(
107+
const _submit = useCallback(
108108
() => {
109109
const payload = getSearchPayload();
110110
executeSearch(payload);
111111
}, [executeSearch, getSearchPayload]
112112
);
113+
const submit = useMemo(() => debounce(_submit), [_submit]);
113114

114115
return {
115116
submit,

src/utils/debounce.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const debounce = <F extends ((...args: any) => any)>(fn: F, ms = 250) => {
2+
let timeoutId: ReturnType<typeof setTimeout>;
3+
4+
return function (this: any, ...args: any[]) {
5+
clearTimeout(timeoutId);
6+
timeoutId = setTimeout(() => fn.apply(this, args), ms);
7+
};
8+
};
9+
10+
export default debounce;

0 commit comments

Comments
 (0)