mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 16:10:07 +00:00
perf(atlas): keep the globe's geometry for the next visit
Every visit read the manifest and the world file again and triangulated the countries in the worker, which showed the loading notice for about a second. The manifest and the built world meshes are now kept for the tab's lifetime, keyed by the content-hashed file name, so coming back puts the globe up at once. A page load starts empty, so a deploy's new geometry is read as before.
This commit is contained in:
@@ -93,6 +93,16 @@ const WORLD_LOD_ZOOM: Array<[string, number]> = [
|
||||
['l0', 0],
|
||||
];
|
||||
const MAX_EDGE: Record<string, number> = { l0: 8, l1: 4, l2: 2, r0: 3, r1: 1.5 };
|
||||
|
||||
/**
|
||||
* What one visit to the Atlas loaded and built, kept for the next one in this
|
||||
* tab: the manifest and the built world meshes, by file name (the name carries
|
||||
* the content hash). Coming back then puts the globe up at once, instead of
|
||||
* reading and triangulating the world again behind a loading notice. A new
|
||||
* page load starts empty, so a deploy's new geometry is read as before.
|
||||
*/
|
||||
let keptManifest: Promise<AtlasMeshManifest> | null = null;
|
||||
const keptWorlds = new Map<string, BuiltLayer>();
|
||||
const FIRST_REGION_ID = 512;
|
||||
|
||||
export class GlobeEngine {
|
||||
@@ -206,7 +216,14 @@ export class GlobeEngine {
|
||||
|
||||
private async boot(): Promise<void> {
|
||||
try {
|
||||
this.manifest = (await this.fetchRetrying('manifest')) as AtlasMeshManifest;
|
||||
keptManifest ??= this.fetchRetrying('manifest') as Promise<AtlasMeshManifest>;
|
||||
try {
|
||||
this.manifest = await keptManifest;
|
||||
} catch (err) {
|
||||
// A failed read is not kept: the next visit asks again.
|
||||
keptManifest = null;
|
||||
throw err;
|
||||
}
|
||||
await this.loadWorld('l0');
|
||||
this.revealStart = this.now();
|
||||
this.opts.onReady?.();
|
||||
@@ -220,8 +237,13 @@ export class GlobeEngine {
|
||||
if (!this.manifest || this.renderer.hasWorld(lod) || this.worldLoading.has(lod)) return;
|
||||
this.worldLoading.add(lod);
|
||||
try {
|
||||
const topo = (await this.fetchRetrying(`world/${this.manifest.world[lod]}`)) as Topology;
|
||||
const layer = await this.build(topo, 'countries', MAX_EDGE[lod], 0);
|
||||
const file = this.manifest.world[lod];
|
||||
let layer = keptWorlds.get(file);
|
||||
if (!layer) {
|
||||
const topo = (await this.fetchRetrying(`world/${file}`)) as Topology;
|
||||
layer = await this.build(topo, 'countries', MAX_EDGE[lod], 0);
|
||||
keptWorlds.set(file, layer);
|
||||
}
|
||||
if (!this.alive) return;
|
||||
this.renderer.setWorld(lod, layer);
|
||||
this.kick();
|
||||
|
||||
Reference in New Issue
Block a user