Skip to content

Commit 507137d

Browse files
missinglinkmxlje
authored andcommitted
feat(throttle): use throttle instead of debounce
1 parent 2eb1130 commit 507137d

File tree

5 files changed

+21
-17
lines changed

5 files changed

+21
-17
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ All configuration happens through **attributes** on the element. `api_key` is th
3939
4040
Your Geocode Earth API key. [Sign up for a free trial »](https://geocode.earth)
4141

42-
### `debounce`
42+
### `throttle`
4343
> defaults to `300`
4444
4545
Used to prevent firing a request for every keystroke as the user types, in milliseconds.
46+
This is passed directly to the [`_.throttle`](https://lodash.com/docs/4.17.15#throttle) function.
4647

4748
### `size`
4849
> defaults to `10`

package-lock.json

Lines changed: 11 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
"dependencies": {
1212
"@geocodeearth/core-js": "^0.0.7",
1313
"downshift": "6.1.3",
14-
"lodash.debounce": "^4.0.8",
1514
"lodash.escape": "^4.0.1",
1615
"lodash.template": "^4.5.0",
16+
"lodash.throttle": "^4.1.1",
1717
"lodash.unescape": "^4.0.1",
1818
"react": "^17.0.2",
1919
"react-dom": "^17.0.2"

src/autocomplete/autocomplete.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useState, useMemo, useCallback, useRef, useEffect } from 'react'
22
import { useCombobox } from 'downshift'
33
import { createAutocomplete } from '@geocodeearth/core-js'
4-
import debounce from 'lodash.debounce'
4+
import throttle from 'lodash.throttle'
55
import css from './autocomplete.css'
66
import strings from '../strings'
77
import { LocationMarker, Loading, Search as SearchIcon } from '../icons'
@@ -19,7 +19,7 @@ export default ({
1919
placeholder = strings.inputPlaceholder,
2020
value = '',
2121
autoFocus = false,
22-
debounce: debounceWait = 200,
22+
throttle: throttleWait = 200,
2323
onSelect: userOnSelectItem,
2424
onChange: userOnChange,
2525
onError: userOnError,
@@ -59,8 +59,8 @@ export default ({
5959
.catch(onError)
6060
}, [autocomplete])
6161

62-
const debouncedSearch = useCallback(
63-
debounce(search, debounceWait, { trailing: true }),
62+
const throttledSearch = useCallback(
63+
throttle(search, throttleWait, { leading: true, trailing: true }),
6464
[search]
6565
)
6666

@@ -88,7 +88,7 @@ export default ({
8888

8989
if (searchOn.includes(type)) {
9090
setIsLoading(true)
91-
debouncedSearch(term)
91+
throttledSearch(term)
9292
}
9393
}
9494

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class GEAutocomplete extends HTMLElement {
5656
'api_key',
5757
'placeholder',
5858
'autofocus',
59-
'debounce',
59+
'throttle',
6060
'lang',
6161
'size',
6262
'value',
@@ -93,7 +93,7 @@ class GEAutocomplete extends HTMLElement {
9393
apiKey: this.getAttribute('api_key')?.trim(),
9494
placeholder: this.getAttribute('placeholder'),
9595
autoFocus: this.getAttribute('autofocus') !== null,
96-
debounce: parseInt(this.getAttribute('debounce')),
96+
throttle: parseInt(this.getAttribute('throttle')),
9797
value: this.value,
9898
params: compact({
9999
lang: this.getAttribute('lang'),

0 commit comments

Comments
 (0)