Skip to content

Build your personal knowledge base with Trilium Notes

fix(geomap): give back the slot of a boundary lookup nobody waits for

Elian Doran57cef2be5359
diff --git a/apps/client/src/widgets/collections/geomap/geocoding.ts b/apps/client/src/widgets/collections/geomap/geocoding.ts
index 298fac6608..7930f83e3d 100644
--- a/apps/client/src/widgets/collections/geomap/geocoding.ts
+++ b/apps/client/src/widgets/collections/geomap/geocoding.ts
@@ -59,8 +59,11 @@ export interface GeoSearchResult {
*
* A call rather than a value, so the boundary is asked for only once a place has been picked: a
* search returns several places and at most one of them is ever looked at.
+ *
+ * A lookup given up on through `signal` answers `null` and gives up its place in the provider's
+ * request queue, so what is asked for next is not held behind it.
*/
- outline?: () => Promise<GeoJSON.Geometry | null>;
+ outline?: (signal?: AbortSignal) => Promise<GeoJSON.Geometry | null>;
}
export interface GeocodingProvider {
diff --git a/apps/client/src/widgets/collections/geomap/index.tsx b/apps/client/src/widgets/collections/geomap/index.tsx
index dc25526426..1160b55790 100644
--- a/apps/client/src/widgets/collections/geomap/index.tsx
+++ b/apps/client/src/widgets/collections/geomap/index.tsx
@@ -86,6 +86,9 @@ export default function GeoView({ note, noteIds, viewConfig, saveConfig }: ViewM
// drawn around whatever took its place.
const latestPlacePick = useRef(0);
const outlineTimer = useRef<ReturnType<typeof setTimeout>>();
+ // Gives up a boundary lookup already under way, which hands back its place in the geocoder's
+ // request queue: the next search would otherwise wait out a boundary nobody is looking at.
+ const outlineRequest = useRef<AbortController>();
// Held still between renders: the pin's layer is rebuilt whenever it is handed a different one,
// and an array literal is different every time (see PlaceMarker).
const placeCenter = useMemo<[number, number] | null>(
@@ -130,12 +133,16 @@ export default function GeoView({ note, noteIds, viewConfig, saveConfig }: ViewM
const forgetPlace = useCallback(() => {
latestPlacePick.current++;
clearTimeout(outlineTimer.current);
+ outlineRequest.current?.abort();
setPickedPlace(undefined);
setPlaceOutline(undefined);
}, []);
// Nothing is left waiting to be fetched for a map that is no longer on the screen.
- useEffect(() => () => clearTimeout(outlineTimer.current), []);
+ useEffect(() => () => {
+ clearTimeout(outlineTimer.current);
+ outlineRequest.current?.abort();
+ }, []);
/** Opens the pane on a note, which sends away the searched place the panel would otherwise share
* a corner with. */
@@ -151,6 +158,7 @@ export default function GeoView({ note, noteIds, viewConfig, saveConfig }: ViewM
const pickPlace = useCallback((place: GeoSearchResult | null) => {
const pickId = ++latestPlacePick.current;
clearTimeout(outlineTimer.current);
+ outlineRequest.current?.abort();
setPickedPlace(place ?? undefined);
setPlaceOutline(undefined);
if (place) {
@@ -164,7 +172,9 @@ export default function GeoView({ note, noteIds, viewConfig, saveConfig }: ViewM
// pass. The geocoder answers one request a second and the searches queue behind the same
// count, so a boundary nobody waited to see would be waited out by the next search.
outlineTimer.current = setTimeout(() => {
- fetchOutline()
+ const request = new AbortController();
+ outlineRequest.current = request;
+ fetchOutline(request.signal)
.then((outline) => {
if (outline && latestPlacePick.current === pickId) {
setPlaceOutline(outline);
diff --git a/apps/client/src/widgets/collections/geomap/nominatim.spec.ts b/apps/client/src/widgets/collections/geomap/nominatim.spec.ts
index 64b65190e6..eb79dc8b95 100644
--- a/apps/client/src/widgets/collections/geomap/nominatim.spec.ts
+++ b/apps/client/src/widgets/collections/geomap/nominatim.spec.ts
@@ -395,4 +395,49 @@ describe("Nominatim geocoding", () => {
expect(requestedAt[1] - requestedAt[0]).toBeGreaterThanOrEqual(1000);
});
+
+ it("moves the requests behind a boundary lookup up when it is given up on", async () => {
+ respondWith([ place() ]);
+ const [ berlin ] = await search("berlin");
+
+ const requestedAt: number[] = [];
+ vi.stubGlobal("fetch", vi.fn(async () => {
+ requestedAt.push(Date.now());
+ return { ok: true, status: 200, statusText: "", json: async () => [] };
+ }));
+
+ // A lookup between two searches, given up on before its slot comes up — which is what
+ // stepping onto another result does.
+ const first = provider.search("paris");
+ const abandoned = new AbortController();
+ const outline = berlin.outline?.(abandoned.signal);
+ const second = provider.search("rome");
+ abandoned.abort();
+
+ await vi.runAllTimersAsync();
+ await Promise.all([ first, second ]);
+
+ expect(await outline).toBeNull();
+ // The searches, a second apart: the lookup neither goes out nor is waited out.
+ expect(requestedAt).toHaveLength(2);
+ expect(requestedAt[1] - requestedAt[0]).toBeGreaterThanOrEqual(1000);
+ expect(requestedAt[1] - requestedAt[0]).toBeLessThan(2000);
+ });
+
+ it("gives up a boundary lookup already out without reporting it as a failure", async () => {
+ respondWith([ place() ]);
+ const [ berlin ] = await search("berlin");
+
+ // Never answers of its own accord, so the lookup is still out when it is given up on.
+ vi.stubGlobal("fetch", vi.fn((_url: string, init?: { signal?: AbortSignal }) => new Promise((_resolve, reject) => {
+ init?.signal?.addEventListener("abort", () => reject(new DOMException("Aborted", "AbortError")));
+ })));
+
+ const request = new AbortController();
+ const outline = berlin.outline?.(request.signal);
+ await vi.runAllTimersAsync();
+ request.abort();
+
+ expect(await outline).toBeNull();
+ });
});
diff --git a/apps/client/src/widgets/collections/geomap/nominatim.ts b/apps/client/src/widgets/collections/geomap/nominatim.ts
index 4b64aa371f..185534d8b6 100644
--- a/apps/client/src/widgets/collections/geomap/nominatim.ts
+++ b/apps/client/src/widgets/collections/geomap/nominatim.ts
@@ -38,9 +38,15 @@ const MAX_RESULTS = 8;
*/
const MIN_REQUEST_INTERVAL = 1000;
-/** When the next request is allowed to go out, moved forward by every request that is queued. */
+/** When the next request may go out, moved forward as each one is let through. */
let nextRequestAt = 0;
+/** The requests waiting for a slot, in the order they asked for one. */
+const waiting: { resolve(letThrough: boolean): void }[] = [];
+
+/** Holds the wait of whichever request stands at the head of {@link waiting}. */
+let releaseTimer: ReturnType<typeof setTimeout> | undefined;
+
/**
* Nominatim, the geocoder run by the OpenStreetMap Foundation.
*
@@ -200,7 +206,7 @@ function toSearchResult(place: NominatimPlace): GeoSearchResult | null {
lng,
bounds,
icon: placeIcon({ category: place.category, type: place.type, addressType: place.addresstype }),
- outline: osmId && hasBoundary ? () => fetchOutline(osmId, bounds) : undefined
+ outline: osmId && hasBoundary ? (signal?: AbortSignal) => fetchOutline(osmId, bounds, signal) : undefined
};
}
@@ -294,7 +300,7 @@ function clamp(value: number, least: number, most: number) {
* A lookup of its own rather than `polygon_geojson` on the search: a search answers with eight places
* and the boundaries of the seven that go unlooked-at are the bulk of what would be carried.
*/
-async function fetchOutline(osmId: string, bounds: GeoBounds | undefined): Promise<GeoJSON.Geometry | null> {
+async function fetchOutline(osmId: string, bounds: GeoBounds | undefined, signal?: AbortSignal): Promise<GeoJSON.Geometry | null> {
const params = new URLSearchParams({
osm_ids: osmId,
format: "jsonv2",
@@ -302,8 +308,21 @@ async function fetchOutline(osmId: string, bounds: GeoBounds | undefined): Promi
polygon_threshold: polygonThreshold(bounds)
});
- await waitForRequestSlot();
- const response = await fetch(`${NOMINATIM_LOOKUP_URL}?${params}`);
+ if (!await waitForRequestSlot(signal)) {
+ return null;
+ }
+
+ let response: Response;
+ try {
+ response = await fetch(`${NOMINATIM_LOOKUP_URL}?${params}`, { signal });
+ } catch (e) {
+ // Given up on while in flight, which is the caller's own doing rather than a failure.
+ if (signal?.aborted) {
+ return null;
+ }
+ throw e;
+ }
+
if (!response.ok) {
throw new Error(`Nominatim answered ${response.status} ${response.statusText}`);
}
@@ -340,17 +359,54 @@ function polygonThreshold(bounds: GeoBounds | undefined) {
}
/**
- * Holds a request back until the interval the policy asks for has passed since the last one.
+ * Holds a request back until the interval the policy asks for has passed since the last one, and
+ * answers whether it may go out.
*
- * The slot is claimed before the wait rather than after it, so two searches started at once queue
- * behind each other instead of both finding the same slot free.
+ * Requests are let through in the order they asked, so two started at once queue behind each other
+ * instead of both finding the same slot free. One given up on through `signal` leaves the queue and
+ * answers `false`: the requests behind it move up rather than waiting out a request never sent,
+ * which is what keeps a search from queueing behind boundaries nobody is looking at any more.
*/
-async function waitForRequestSlot() {
+async function waitForRequestSlot(signal?: AbortSignal): Promise<boolean> {
+ if (signal?.aborted) {
+ return false;
+ }
+
+ // The slot at hand is taken there and then, so a lone request is not held for a turn of the
+ // event loop before going out.
const now = Date.now();
- const slot = Math.max(now, nextRequestAt);
- nextRequestAt = slot + MIN_REQUEST_INTERVAL;
+ if (!waiting.length && now >= nextRequestAt) {
+ nextRequestAt = now + MIN_REQUEST_INTERVAL;
+ return true;
+ }
- if (slot > now) {
- await new Promise((resolve) => setTimeout(resolve, slot - now));
+ return new Promise<boolean>((resolve) => {
+ const request = { resolve };
+ waiting.push(request);
+ signal?.addEventListener("abort", () => {
+ const queued = waiting.indexOf(request);
+ if (queued >= 0) {
+ waiting.splice(queued, 1);
+ resolve(false);
+ }
+ }, { once: true });
+ releaseNextRequest();
+ });
+}
+
+/** Lets the request at the head of the queue through once its slot comes up, and then the next. */
+function releaseNextRequest() {
+ if (releaseTimer !== undefined || !waiting.length) {
+ return;
}
+
+ releaseTimer = setTimeout(() => {
+ releaseTimer = undefined;
+ const request = waiting.shift();
+ if (request) {
+ nextRequestAt = Date.now() + MIN_REQUEST_INTERVAL;
+ request.resolve(true);
+ }
+ releaseNextRequest();
+ }, Math.max(0, nextRequestAt - Date.now()));
}