Skip to content

Commit f475124

Browse files
committed
add debounce
1 parent 1d0cd39 commit f475124

1 file changed

Lines changed: 44 additions & 28 deletions

File tree

  • src/components/geocode-autocomplete

src/components/geocode-autocomplete/index.ts

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,27 @@ type Address = {
1313
type ArrowStyleEnum = "default" | "light";
1414
type LabelStyleEnum = "responsive" | "static";
1515

16+
// debounce function
17+
function debounce(
18+
fn: (...args: any[]) => void | Promise<void>,
19+
delay: number,
20+
): (...args: any[]) => void {
21+
let timer: NodeJS.Timeout | null = null;
22+
let isFirstCall = true;
23+
return function (this: any, ...args: any[]) {
24+
if (isFirstCall) {
25+
fn.apply(this, args);
26+
isFirstCall = false;
27+
return;
28+
}
29+
30+
if (timer) clearTimeout(timer as NodeJS.Timeout);
31+
timer = setTimeout(() => {
32+
fn.apply(this, args);
33+
}, delay);
34+
};
35+
}
36+
1637
@customElement("geocode-autocomplete")
1738
export class GeocodeAutocomplete extends LitElement {
1839
// ref https://github.com/e111077/vite-lit-element-ts-sass/issues/3
@@ -22,7 +43,7 @@ export class GeocodeAutocomplete extends LitElement {
2243
id = "geocode";
2344

2445
@property({ type: String })
25-
label = "Type an address";
46+
label = "Search for an address";
2647

2748
@property({ type: String })
2849
initialAddress = "";
@@ -46,9 +67,6 @@ export class GeocodeAutocomplete extends LitElement {
4667
@state()
4768
private _addressesMatching: Address[] = [];
4869

49-
@state()
50-
private _options: string[] = [];
51-
5270
@state()
5371
private _osError: string | undefined = undefined;
5472

@@ -73,13 +91,12 @@ export class GeocodeAutocomplete extends LitElement {
7391
element: this.renderRoot.querySelector(`#${this.id}-container`),
7492
id: this.id,
7593
required: true,
76-
source: (query: string, populateResults: any) => {
77-
// min query length before fetching
78-
if (query.length > 5) {
79-
this._fetchData(query);
80-
populateResults(this._options);
94+
source: debounce((query: string, populateResults: any) => {
95+
// min query length of 3 before fetching
96+
if (query.length >= 3) {
97+
this._fetchData(query, populateResults);
8198
}
82-
},
99+
}, 500),
83100
defaultValue: this.initialAddress,
84101
showAllValues: true,
85102
displayMenu: "overlay",
@@ -102,7 +119,10 @@ export class GeocodeAutocomplete extends LitElement {
102119
});
103120
}
104121

105-
async _fetchData(input: string = "") {
122+
async _fetchData(
123+
input: string = "",
124+
populateResults: (values: string[]) => void,
125+
) {
106126
const isUsingOS = Boolean(this.osApiKey || this.osProxyEndpoint);
107127
if (!isUsingOS)
108128
throw Error("OS Places API key or OS proxy endpoint not found");
@@ -111,7 +131,9 @@ export class GeocodeAutocomplete extends LitElement {
111131
const params: Record<string, string> = {
112132
query: input,
113133
dataset: "LPI",
134+
fq: "LPI_LOGICAL_STATUS_CODE:1",
114135
};
136+
115137
const url = getServiceURL({
116138
service: "find",
117139
apiKey: this.osApiKey,
@@ -123,8 +145,7 @@ export class GeocodeAutocomplete extends LitElement {
123145
.then((resp) => resp.json())
124146
.then((data) => {
125147
// reset options on every fetch
126-
this._options = [];
127-
this._addressesMatching = [];
148+
populateResults([]);
128149

129150
// handle error formats returned by OS
130151
if (data.error || data.fault) {
@@ -137,21 +158,16 @@ export class GeocodeAutocomplete extends LitElement {
137158
if (data.results) {
138159
this._addressesMatching = data.results;
139160

140-
data.results
141-
.filter(
142-
(address: Address) =>
143-
address.LPI.LPI_LOGICAL_STATUS_CODE_DESCRIPTION === "APPROVED",
144-
)
145-
.map((address: Address) => {
146-
this._options.push(
147-
address.LPI.ADDRESS.slice(
148-
0,
149-
address.LPI.ADDRESS.lastIndexOf(
150-
`, ${address.LPI.ADMINISTRATIVE_AREA}`,
151-
),
152-
),
153-
);
154-
});
161+
let options = data.results.map((address: Address) =>
162+
address.LPI.ADDRESS.slice(
163+
0,
164+
address.LPI.ADDRESS.lastIndexOf(
165+
`, ${address.LPI.ADMINISTRATIVE_AREA}`,
166+
),
167+
),
168+
);
169+
170+
populateResults(options.slice(0, 5));
155171
}
156172
})
157173
.catch((error) => console.log(error));

0 commit comments

Comments
 (0)