Abort search requests when clearing search box

Also adds abort support for Photon queries
This commit is contained in:
2026-03-23 18:07:29 +04:00
parent 8478e00253
commit 86b20fd474
4 changed files with 78 additions and 2 deletions

View File

@@ -5,6 +5,15 @@ import { humanizeOsmTag } from '../utils/format-text';
export default class PhotonService extends Service {
@service settings;
controller = null;
cancelAll() {
if (this.controller) {
this.controller.abort();
this.controller = null;
}
}
get baseUrl() {
return this.settings.photonApi;
}
@@ -12,6 +21,12 @@ export default class PhotonService extends Service {
async search(query, lat, lon, limit = 10) {
if (!query || query.length < 2) return [];
if (this.controller) {
this.controller.abort();
}
this.controller = new AbortController();
const signal = this.controller.signal;
const params = new URLSearchParams({
q: query,
limit: String(limit),
@@ -25,7 +40,7 @@ export default class PhotonService extends Service {
const url = `${this.baseUrl}?${params.toString()}`;
try {
const res = await this.fetchWithRetry(url);
const res = await this.fetchWithRetry(url, { signal });
if (!res.ok) {
throw new Error(`Photon request failed with status ${res.status}`);
}
@@ -35,6 +50,9 @@ export default class PhotonService extends Service {
return data.features.map((f) => this.normalizeFeature(f));
} catch (e) {
if (e.name === 'AbortError') {
return [];
}
console.error('Photon search error:', e);
// Return empty array on error so UI doesn't break
return [];