Skip to main content

The map adapter (part 1 of 2)

Part of Architecture, which is built from ARCHITECTURE.md in the repository, where it is edited.

MapLibre GL JS is the only map engine, and the ui never imports it. The adapter and the engine styles are in packages/adapter-maplibre, whose entry module exports createMapLibreMapAdapter, its options type MapLibreMapAdapterOptions and TILE_LAYER_SLOW_MS, and nothing else, so the class, the loader and the test options can change; its built declarations do not import maplibre-gl. The host imports it in createServices() only. MapView takes an adapter factory (() => MapAdapter) from the services, mounts one adapter in a host element and wires events to the rest of the ui. Changing basemap calls setBasemap on that adapter. It avoids transformStyle because a swap with it set waits for the current style, so a failed swap could not put the last basemap back. The adapter builds an inline raster style for a raster source and loads the style URL for a vector source. For a hybrid source it loads the label style and, in its style.load handler, before data layers, keeps only the line and symbol layers that draw from one of labelSourceLayers, less those in labelOmitLayers, so no area of the style (land, water, buildings, road areas) covers the imagery, and removes any source only the removed layers used. It then adds the imagery as a raster source capped at maxTileZoom and a layer under the rest, so MapLibre enlarges the deepest real level instead of asking for more, and under that a background layer in the basemap's swatch colour, so a dark basemap is not pale before its imagery draws. If that fails, a pending mount or swap fails with it, so a swap puts the last basemap back. No code names a basemap. MapLibreMapAdapter.ts keeps the map and the lifecycle (mount, destroy, the pending style load and style.load) and holds one module for each other job: the camera (camera.ts), the flat or globe placement of overlay marks (overlayPlacement.ts), the options of the map on screen (engineMap.ts), the engine loader (engineLoader.ts, which is the only adapter module that imports maplibre-gl at run time), and the modules named below for style, data layers, edit handles, image export and failures. The factory createMapLibreMapAdapter takes one option, loadWorkerUrl, a function that resolves to the URL of the engine's worker as the host serves it. The adapter does not import the worker: Vite's ?worker&url query lives in the host's packages/web/src/adapters/maplibre/workerUrl.ts, which adds the policy's cache key to the URL and which createServices() passes in, so the package holds nothing that only Vite understands. loadEngine calls it when the first map mounts, before it calls the engine's setWorkerUrl; a rejected loader fails the mount as load-failed and is asked again on the next one. The timing options the tests use (workerFailureGraceMs, imageTilesWaitMs, tileLayerSlowMs) are a second constructor argument that the factory does not take, and TILE_LAYER_SLOW_MS is the only constant MapLibreMapAdapter.ts re-exports, which the entry module exports in turn, for e2eTimers.test.ts. The style code is in basemapStyle.ts, as functions of the map and a basemap that hold no state: styleFor, composeHybrid and applyZoomLimits. The adapter calls them when it draws a basemap and in its style.load handler.