Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/plugins/reverseGeocoder/utils/reverseGeocode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,21 @@ const parser = new Parser({
tagNameProcessors: [processors.stripPrefix],
})

let abortController: AbortController | null = null

export async function reverseGeocode(
url: string,
coordinate: [number, number]
): Promise<ReverseGeocoderFeature> {
if (abortController) {
abortController.abort()
abortController = null
}
abortController = new AbortController()
const response = await fetch(url, {
method: 'POST',
body: buildPostBody(coordinate),
signal: abortController.signal,
})

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also put abortController = null here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we would not be able to call abortController.abort() for the next call. The way it is added in this plugin is in line with e.g. addressSearch

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we await this fetch call. So after that fetch, the abortController has no effect anymore, does it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably, but including this change we got 5 instances of this pattern already without the added null assignment after the call. If you deem this an issue, we should be changing that everywhere.

const parsedBody = await parser.parseStringPromise(await response.text())
Expand Down Expand Up @@ -142,6 +150,7 @@ if (import.meta.vitest) {
expect(fetchMock).toHaveBeenCalledWith(testUrl, {
method: 'POST',
body: buildPostBody(testCoordinates),
signal: abortController?.signal,
})

expect(feature).toEqual<ReverseGeocoderFeature>({
Expand Down
Loading