UNPKG

@trailstash/ultra

Version:

A web based tool for making MapLibre GL maps with data from sources such as Overpass, GeoJSON, GPX, KML, TCX, etc

58 lines (50 loc) 1.72 kB
export default class AutoProvider { #fitBounds; #layers; #popupContextBuilder; #popupTemplate; #invalidateCacheOnBBox; #providers; constructor(providers = {}) { //super(); this.#providers = providers; } async source(query, controller, { server, bounds }) { console.log(Array.from(Object.keys(this.#providers))); for (const [type, provider] of Object.entries(this.#providers)) { if (!provider.detect) { continue; } console.log("trying", type); if (await Promise.resolve(provider.detect(query, bounds))) { console.log("detected", type); this.#fitBounds = provider.fitBounds; this.#layers = provider.layers; this.#popupContextBuilder = provider.popupContextBuilder; this.#popupTemplate = provider.popupTemplate; this.#invalidateCacheOnBBox = provider.invalidateCacheOnBBox; return provider.source(query, controller, { server, bounds }); } } throw new Error("Could not determine query type!"); } get fitBounds() { return this.#fitBounds; } get layers() { return this.#layers; } get popupContextBuilder() { return this.#popupContextBuilder; } get popupTemplate() { return this.#popupTemplate; } get invalidateCacheOnBBox() { // This isn't ideal, because this is called _before_ `source`, but it will work because it's either: // * undefined & the first run, so bc first run it always will execute anyway // * different type, which might mean a different invalidateCacheOnBBox value, but we always run on new type anyway // * type unchanged, & subsequent run, so actually valid value return this.#invalidateCacheOnBBox; } }