Fix race condition in lazy-loaded geo search module (#4486)
This commit is contained in:
@@ -20,13 +20,19 @@ export type GroupedSearchResults = {
|
|||||||
cities: SearchResultItem[];
|
cities: SearchResultItem[];
|
||||||
};
|
};
|
||||||
|
|
||||||
let cscModule: typeof import("country-state-city") | null = null;
|
// Store the promise so concurrent calls share one import
|
||||||
|
let cscModulePromise: Promise<typeof import("country-state-city")> | null =
|
||||||
|
null;
|
||||||
|
|
||||||
async function loadCsc() {
|
function loadCsc() {
|
||||||
if (!cscModule) {
|
if (!cscModulePromise) {
|
||||||
cscModule = await import("country-state-city");
|
cscModulePromise = import("country-state-city").catch((error) => {
|
||||||
|
// Reset on failure so next user action can retry
|
||||||
|
cscModulePromise = null;
|
||||||
|
throw error;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
return cscModule;
|
return cscModulePromise;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function searchGeoData(
|
export async function searchGeoData(
|
||||||
|
|||||||
Reference in New Issue
Block a user