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 });
|
||||
}
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import { resolveEngine } from '../engine';
|
||||
import { writeTransaction } from '../engine/write-transaction';
|
||||
import type { DB } from '../kysely/db';
|
||||
import type { EntityManager } from '@mikro-orm/core';
|
||||
import { Seeder } from '@mikro-orm/seeder';
|
||||
@@ -12,27 +10,33 @@ const STYLE = 'default_user_setting_maplibre_style';
|
||||
/** Give a new installation a starting basemap before its first user is created. */
|
||||
export class MapDefaultsSeeder extends Seeder {
|
||||
async run(em: EntityManager): Promise<void> {
|
||||
await writeTransaction(em, resolveEngine(), async (tx) => {
|
||||
const db = (tx as SqlEntityManager).getKysely<Pick<DB, 'app_settings' | 'schema_version' | 'users'>>();
|
||||
// SchemaVersionSeeder writes this marker after the first bootstrap even on
|
||||
// SSO-only installations that are still waiting for their first account.
|
||||
const initialized = await db.selectFrom('schema_version').select('id').limit(1).executeTakeFirst();
|
||||
if (initialized) return;
|
||||
const user = await db.selectFrom('users').select('id').limit(1).executeTakeFirst();
|
||||
if (user) return;
|
||||
const db = (em as SqlEntityManager).getKysely<Pick<DB, 'app_settings' | 'schema_version' | 'users'>>();
|
||||
const defaults = db
|
||||
.selectNoFrom((eb) => [eb.val<string>(PROVIDER).as('key'), eb.val(JSON.stringify('maplibre-gl')).as('value')])
|
||||
.unionAll(
|
||||
db.selectNoFrom((eb) => [
|
||||
eb.val<string>(STYLE).as('key'),
|
||||
eb.val(JSON.stringify(OPENFREEMAP_DEFAULT_STYLE)).as('value'),
|
||||
]),
|
||||
)
|
||||
.as('map_defaults');
|
||||
|
||||
const provider = await db
|
||||
.insertInto('app_settings')
|
||||
.values({ key: PROVIDER, value: JSON.stringify('maplibre-gl') })
|
||||
.onConflict((conflict) => conflict.column('key').doNothing())
|
||||
.returning('key')
|
||||
.executeTakeFirst();
|
||||
if (!provider) return;
|
||||
await db
|
||||
.insertInto('app_settings')
|
||||
.values({ key: STYLE, value: JSON.stringify(OPENFREEMAP_DEFAULT_STYLE) })
|
||||
.onConflict((conflict) => conflict.column('key').doNothing())
|
||||
.execute();
|
||||
});
|
||||
// SchemaVersionSeeder marks completed first boots, including SSO-only installs
|
||||
// still waiting for a user. Check and insert together on the bound database.
|
||||
await db
|
||||
.insertInto('app_settings')
|
||||
.columns(['key', 'value'])
|
||||
.expression(
|
||||
db
|
||||
.selectFrom(defaults)
|
||||
.select(['key', 'value'])
|
||||
.where(({ not, exists, selectFrom }) => not(exists(selectFrom('schema_version').select('id'))))
|
||||
.where(({ not, exists, selectFrom }) => not(exists(selectFrom('users').select('id'))))
|
||||
.where(({ not, exists, selectFrom }) =>
|
||||
not(exists(selectFrom('app_settings').select('key').where('key', '=', PROVIDER))),
|
||||
),
|
||||
)
|
||||
.onConflict((conflict) => conflict.column('key').doNothing())
|
||||
.execute();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user