Skip to content

Commit 19bbfc6

Browse files
committed
fix(reverseGeocoder): abort ongoing requests if a new reverse geocode request comes in
1 parent 9b85c7f commit 19bbfc6

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/plugins/reverseGeocoder/utils/reverseGeocode.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,21 @@ const parser = new Parser({
3232
tagNameProcessors: [processors.stripPrefix],
3333
})
3434

35+
let abortController: AbortController | null = null
36+
3537
export async function reverseGeocode(
3638
url: string,
3739
coordinate: [number, number]
3840
): Promise<ReverseGeocoderFeature> {
41+
if (abortController) {
42+
abortController.abort()
43+
abortController = null
44+
}
45+
abortController = new AbortController()
3946
const response = await fetch(url, {
4047
method: 'POST',
4148
body: buildPostBody(coordinate),
49+
signal: abortController.signal,
4250
})
4351

4452
const parsedBody = await parser.parseStringPromise(await response.text())
@@ -142,6 +150,7 @@ if (import.meta.vitest) {
142150
expect(fetchMock).toHaveBeenCalledWith(testUrl, {
143151
method: 'POST',
144152
body: buildPostBody(testCoordinates),
153+
signal: abortController?.signal,
145154
})
146155

147156
expect(feature).toEqual<ReverseGeocoderFeature>({

0 commit comments

Comments
 (0)