Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions composables/useStationSearchIndex.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createCachedLoader } from '~/utils/cached-loader.mjs';
import { createSearchIndexLoadState } from '~/utils/search-index-load-state.mjs';

export interface StationSearchItem {
objectID: string;
Expand Down Expand Up @@ -36,19 +37,20 @@ export function useStationSearchIndex() {
const index = shallowRef<StationSearchItem[]>([]);
const loading = ref(false);
const error = ref(false);
const loadState = createSearchIndexLoadState();
let disposed = false;
let requested: Promise<void> | undefined;

// Never fetch a multi-station catalog simply because the global header mounts.
// Load on search focus/open; the cached loader deduplicates across mobile
// modal and desktop header, and failed requests remain retryable.
const load = (): Promise<void> => {
if (index.value.length) return Promise.resolve();
if (loadState.isLoaded()) return Promise.resolve();
if (requested) return requested;
loading.value = true;
error.value = false;
requested = loaderFor(searchUrl)()
.then(items => { if (!disposed) index.value = items; })
.then(items => { if (!disposed) { index.value = items; loadState.markLoaded(); } })
.catch(() => { if (!disposed) error.value = true; })
.finally(() => {
requested = undefined;
Expand Down
27 changes: 27 additions & 0 deletions tests/search-index-load-state.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { createSearchIndexLoadState } from '../utils/search-index-load-state.mjs';

test('empty successful search index is still considered loaded', () => {
const state = createSearchIndexLoadState();
assert.equal(state.isLoaded(), false);
const items = [];
assert.equal(items.length, 0);
state.markLoaded();
assert.equal(state.isLoaded(), true);
});

test('failed search index load stays retryable', () => {
const state = createSearchIndexLoadState();
assert.equal(state.isLoaded(), false);
// A rejected request must not call markLoaded().
assert.equal(state.isLoaded(), false);
});

test('search index load state is isolated per component instance', () => {
const first = createSearchIndexLoadState();
const second = createSearchIndexLoadState();
first.markLoaded();
assert.equal(first.isLoaded(), true);
assert.equal(second.isLoaded(), false);
});
8 changes: 8 additions & 0 deletions utils/search-index-load-state.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** Distinguish a successfully loaded empty catalog from one not yet loaded. */
export function createSearchIndexLoadState() {
let loaded = false;
return {
isLoaded: () => loaded,
markLoaded: () => { loaded = true; },
};
}
Loading