mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
fix(ci): use the bound database for map defaults and retry preloads
Write both defaults in one guarded statement so a SQLite source imported by the Postgres suite needs no Postgres lock. Release a failed speculative chunk promise so opening the map can retry it.
This commit is contained in:
@@ -48,7 +48,7 @@ export function preloadTripMapRenderer(provider: string | undefined, token: stri
|
||||
const engine = pickGlRenderer(provider, token)
|
||||
if (!engine) return
|
||||
const view = engine === 'maplibre-gl' ? MapViewGLMaplibre : MapViewGLMapbox
|
||||
void view.preload().catch((error: unknown) => console.debug('[map] renderer preload failed', error))
|
||||
void view.preload()
|
||||
}
|
||||
|
||||
export const JourneyMapGLMapbox = lazyWithRetry(async () => {
|
||||
|
||||
@@ -46,14 +46,22 @@ describe('lazyWithRetry', () => {
|
||||
it('shares the preloaded promise with the lazy component', async () => {
|
||||
const load = vi.fn(async () => ({ default: () => <div>Map view</div> }))
|
||||
const Page = lazyWithRetry(load)
|
||||
const pending = Page.preload()
|
||||
expect(Page.preload()).toBe(pending)
|
||||
await pending
|
||||
await Promise.all([Page.preload(), Page.preload()])
|
||||
render(<Suspense><Page /></Suspense>)
|
||||
expect(await screen.findByText('Map view')).toBeInTheDocument()
|
||||
expect(load).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('lets navigation retry after a speculative preload fails', async () => {
|
||||
const load = vi.fn(async () => ({ default: () => <div>Loaded after navigation</div> }))
|
||||
load.mockRejectedValueOnce(new Error('preload was interrupted'))
|
||||
const Page = lazyWithRetry(load)
|
||||
await Page.preload()
|
||||
render(<Suspense><Page /></Suspense>)
|
||||
expect(await screen.findByText('Loaded after navigation')).toBeInTheDocument()
|
||||
expect(load).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
|
||||
it('FE-UTIL-LAZYRETRY-006: renders the default export once the chunk arrives', async () => {
|
||||
const Page = lazyWithRetry(async () => ({ default: () => <div>Atlas</div> }))
|
||||
render(
|
||||
|
||||
@@ -46,9 +46,9 @@ export function retryUrlFor(error: unknown): string | null {
|
||||
|
||||
export function lazyWithRetry<T extends ComponentType<any>>(
|
||||
load: () => Promise<{ default: T }>
|
||||
): LazyExoticComponent<T> & { preload: () => Promise<{ default: T }> } {
|
||||
): LazyExoticComponent<T> & { preload: () => Promise<void> } {
|
||||
let pending: Promise<{ default: T }> | undefined;
|
||||
const preload = () => (pending ??= load()
|
||||
const start = () => (pending ??= load()
|
||||
.catch((error: unknown) => {
|
||||
const url = retryUrlFor(error);
|
||||
// Rethrown on purpose: React.lazy hands the rejection to the nearest
|
||||
@@ -65,5 +65,13 @@ export function lazyWithRetry<T extends ComponentType<any>>(
|
||||
clearChunkReloadMarker();
|
||||
return module;
|
||||
}));
|
||||
return Object.assign(lazy(preload), { preload });
|
||||
const preload = async () => {
|
||||
try {
|
||||
await start();
|
||||
} catch {
|
||||
// A failed speculative load leaves the actual navigation free to try again.
|
||||
pending = undefined;
|
||||
}
|
||||
};
|
||||
return Object.assign(lazy(start), { preload });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user