* _getSourceAsync * Create a new cache to hold data for the given template * @param {string} template - A url template for fetching data (e.g. a z/x/y tileserver or .pmtiles) * @return Promise resolved to the source object once it is ready to use
(template)
| 189 | * @return Promise resolved to the source object once it is ready to use |
| 190 | */ |
| 191 | _getSourceAsync(template) { |
| 192 | if (!template) return Promise.reject(new Error('No template')); |
| 193 | |
| 194 | let source = this._sources.get(template); |
| 195 | |
| 196 | if (!source) { // create it |
| 197 | const url = new URL(template); |
| 198 | const hostname = url.hostname; |
| 199 | const filename = url.pathname.split('/').at(-1); |
| 200 | |
| 201 | source = { |
| 202 | id: utilHashcode(template).toString(), |
| 203 | displayName: hostname, |
| 204 | template: template, |
| 205 | inflight: new Map(), // Map(tileID -> AbortController) |
| 206 | loaded: new Map(), // Map(tileID -> Tile) |
| 207 | zoomCache: new Map(), // Map(zoom -> Object zoomCache) |
| 208 | lastv: null // viewport version last time we fetched data |
| 209 | }; |
| 210 | |
| 211 | this._sources.set(template, source); |
| 212 | |
| 213 | // Special handling for PMTiles sources |
| 214 | // Create a PMTiles instance and fetch the header so we know more about the source. |
| 215 | if (filename && /\.pmtiles$/.test(filename)) { |
| 216 | source.displayName = filename; |
| 217 | source.pmtiles = new PMTiles(template); |
| 218 | source.readyPromise = source.pmtiles.getHeader() |
| 219 | .then(header => source.header = header) |
| 220 | .then(() => Promise.resolve(source)); |
| 221 | |
| 222 | } else { |
| 223 | source.readyPromise = Promise.resolve(source); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | return source.readyPromise; |
| 228 | } |
| 229 | |
| 230 | |
| 231 | /** |