From 9e6584be4c8ec5db6887071aa220f609a024fa06 Mon Sep 17 00:00:00 2001 From: Matthew Mamonov Date: Fri, 27 Jan 2023 15:33:24 +0700 Subject: [PATCH 1/6] Combine ResourceType interface and const into a single enum Move it from ajax.ts to request_manager.ts --- src/source/geojson_source.ts | 4 +-- src/source/image_source.test.ts | 8 +++--- src/source/image_source.ts | 5 ++-- src/source/load_tilejson.ts | 5 ++-- src/source/raster_dem_tile_source.test.ts | 6 ++--- src/source/raster_dem_tile_source.ts | 5 ++-- src/source/raster_tile_source.test.ts | 6 ++--- src/source/raster_tile_source.ts | 5 ++-- src/source/vector_tile_source.test.ts | 6 ++--- src/source/vector_tile_source.ts | 4 +-- src/source/video_source.ts | 5 ++-- src/style/load_glyph_range.test.ts | 4 +-- src/style/load_glyph_range.ts | 5 ++-- src/style/load_sprite.test.ts | 18 ++++++------- src/style/load_sprite.ts | 7 ++--- src/style/style.test.ts | 8 +++--- src/style/style.ts | 5 ++-- src/ui/map.ts | 8 +++--- src/util/ajax.ts | 33 ----------------------- src/util/request_manager.ts | 21 +++++++++++---- 20 files changed, 77 insertions(+), 91 deletions(-) diff --git a/src/source/geojson_source.ts b/src/source/geojson_source.ts index df660cb417..d4997dbb9b 100644 --- a/src/source/geojson_source.ts +++ b/src/source/geojson_source.ts @@ -2,7 +2,7 @@ import {Event, ErrorEvent, Evented} from '../util/evented'; import {extend} from '../util/util'; import EXTENT from '../data/extent'; -import {ResourceType} from '../util/ajax'; +import {MapLibreResourceType} from '../util/request_manager'; import browser from '../util/browser'; import type {Source} from './source'; @@ -323,7 +323,7 @@ class GeoJSONSource extends Evented implements Source { if (diff) { options.dataDiff = diff; } else if (typeof this._data === 'string') { - options.request = this.map._requestManager.transformRequest(browser.resolveURL(this._data as string), ResourceType.Source); + options.request = this.map._requestManager.transformRequest(browser.resolveURL(this._data as string), MapLibreResourceType.Source); options.request.collectResourceTiming = this._collectResourceTiming; } else { options.data = JSON.stringify(this._data); diff --git a/src/source/image_source.test.ts b/src/source/image_source.test.ts index 712b4358fc..af4b543ac8 100644 --- a/src/source/image_source.test.ts +++ b/src/source/image_source.test.ts @@ -3,7 +3,7 @@ import {Evented} from '../util/evented'; import Transform from '../geo/transform'; import {extend} from '../util/util'; import {fakeXhr} from 'nise'; -import {RequestManager} from '../util/request_manager'; +import {MapLibreResourceType, RequestManager} from '../util/request_manager'; import Dispatcher from '../util/dispatcher'; import {stubAjaxGetImage} from '../util/test/util'; @@ -72,7 +72,7 @@ describe('ImageSource', () => { respond(); expect(spy).toHaveBeenCalledTimes(1); expect(spy.mock.calls[0][0]).toBe('/image.png'); - expect(spy.mock.calls[0][1]).toBe('Image'); + expect(spy.mock.calls[0][1]).toBe(MapLibreResourceType.Image); }); test('updates url from updateImage', () => { @@ -83,12 +83,12 @@ describe('ImageSource', () => { respond(); expect(spy).toHaveBeenCalledTimes(1); expect(spy.mock.calls[0][0]).toBe('/image.png'); - expect(spy.mock.calls[0][1]).toBe('Image'); + expect(spy.mock.calls[0][1]).toBe(MapLibreResourceType.Image); source.updateImage({url: '/image2.png'}); respond(); expect(spy).toHaveBeenCalledTimes(2); expect(spy.mock.calls[1][0]).toBe('/image2.png'); - expect(spy.mock.calls[1][1]).toBe('Image'); + expect(spy.mock.calls[1][1]).toBe(MapLibreResourceType.Image); }); test('sets coordinates', () => { diff --git a/src/source/image_source.ts b/src/source/image_source.ts index b463dd2596..69123744f9 100644 --- a/src/source/image_source.ts +++ b/src/source/image_source.ts @@ -1,6 +1,7 @@ import {CanonicalTileID} from './tile_id'; import {Event, ErrorEvent, Evented} from '../util/evented'; -import {getImage, ResourceType} from '../util/ajax'; +import {getImage} from '../util/ajax'; +import {MapLibreResourceType} from '../util/request_manager'; import EXTENT from '../data/extent'; import {RasterBoundsArray} from '../data/array_types.g'; import rasterBoundsAttributes from '../data/raster_bounds_attributes'; @@ -111,7 +112,7 @@ class ImageSource extends Evented implements Source { this.url = this.options.url; - this._request = getImage(this.map._requestManager.transformRequest(this.url, ResourceType.Image), (err, image) => { + this._request = getImage(this.map._requestManager.transformRequest(this.url, MapLibreResourceType.Image), (err, image) => { this._request = null; this._loaded = true; diff --git a/src/source/load_tilejson.ts b/src/source/load_tilejson.ts index 3e0297c805..3cd59dc86d 100644 --- a/src/source/load_tilejson.ts +++ b/src/source/load_tilejson.ts @@ -1,6 +1,7 @@ import {pick, extend} from '../util/util'; -import {getJSON, ResourceType} from '../util/ajax'; +import {getJSON} from '../util/ajax'; +import {MapLibreResourceType} from '../util/request_manager'; import browser from '../util/browser'; import type {RequestManager} from '../util/request_manager'; @@ -34,7 +35,7 @@ export default function loadTileJson( }; if (options.url) { - return getJSON(requestManager.transformRequest(options.url, ResourceType.Source), loaded); + return getJSON(requestManager.transformRequest(options.url, MapLibreResourceType.Source), loaded); } else { return browser.frame(() => loaded(null, options)); } diff --git a/src/source/raster_dem_tile_source.test.ts b/src/source/raster_dem_tile_source.test.ts index 69f1f83277..5fbd3be478 100644 --- a/src/source/raster_dem_tile_source.test.ts +++ b/src/source/raster_dem_tile_source.test.ts @@ -1,7 +1,7 @@ import {fakeServer, FakeServer} from 'nise'; import RasterDEMTileSource from './raster_dem_tile_source'; import {OverscaledTileID} from './tile_id'; -import {RequestManager} from '../util/request_manager'; +import {MapLibreResourceType, RequestManager} from '../util/request_manager'; import Dispatcher from '../util/dispatcher'; import Tile from './tile'; @@ -48,7 +48,7 @@ describe('RasterTileSource', () => { server.respond(); expect(transformSpy.mock.calls[0][0]).toBe('/source.json'); - expect(transformSpy.mock.calls[0][1]).toBe('Source'); + expect(transformSpy.mock.calls[0][1]).toBe(MapLibreResourceType.Source); done(); }); @@ -74,7 +74,7 @@ describe('RasterTileSource', () => { expect(transformSpy).toHaveBeenCalledTimes(1); expect(transformSpy.mock.calls[0][0]).toBe('http://example.com/10/5/5.png'); - expect(transformSpy.mock.calls[0][1]).toBe('Tile'); + expect(transformSpy.mock.calls[0][1]).toBe(MapLibreResourceType.Tile); done(); } diff --git a/src/source/raster_dem_tile_source.ts b/src/source/raster_dem_tile_source.ts index 2182554037..f86867ad6d 100644 --- a/src/source/raster_dem_tile_source.ts +++ b/src/source/raster_dem_tile_source.ts @@ -1,4 +1,5 @@ -import {getImage, ResourceType} from '../util/ajax'; +import {getImage} from '../util/ajax'; +import {MapLibreResourceType} from '../util/request_manager'; import {extend, isImageBitmap} from '../util/util'; import {Evented} from '../util/evented'; import browser from '../util/browser'; @@ -38,7 +39,7 @@ class RasterDEMTileSource extends RasterTileSource implements Source { loadTile(tile: Tile, callback: Callback) { const url = tile.tileID.canonical.url(this.tiles, this.map.getPixelRatio(), this.scheme); - tile.request = getImage(this.map._requestManager.transformRequest(url, ResourceType.Tile), imageLoaded.bind(this)); + tile.request = getImage(this.map._requestManager.transformRequest(url, MapLibreResourceType.Tile), imageLoaded.bind(this)); tile.neighboringTiles = this._getNeighboringTiles(tile.tileID); function imageLoaded(err, img) { diff --git a/src/source/raster_tile_source.test.ts b/src/source/raster_tile_source.test.ts index 6a4cb7a577..20babd772d 100644 --- a/src/source/raster_tile_source.test.ts +++ b/src/source/raster_tile_source.test.ts @@ -1,6 +1,6 @@ import RasterTileSource from './raster_tile_source'; import {OverscaledTileID} from './tile_id'; -import {RequestManager} from '../util/request_manager'; +import {MapLibreResourceType, RequestManager} from '../util/request_manager'; import Dispatcher from '../util/dispatcher'; import {fakeServer, FakeServer} from 'nise'; import Tile from './tile'; @@ -48,7 +48,7 @@ describe('RasterTileSource', () => { server.respond(); expect(transformSpy.mock.calls[0][0]).toBe('/source.json'); - expect(transformSpy.mock.calls[0][1]).toBe('Source'); + expect(transformSpy.mock.calls[0][1]).toBe(MapLibreResourceType.Source); }); test('respects TileJSON.bounds', done => { @@ -126,7 +126,7 @@ describe('RasterTileSource', () => { source.loadTile(tile, () => {}); expect(transformSpy).toHaveBeenCalledTimes(1); expect(transformSpy.mock.calls[0][0]).toBe('http://example.com/10/5/5.png'); - expect(transformSpy.mock.calls[0][1]).toBe('Tile'); + expect(transformSpy.mock.calls[0][1]).toBe(MapLibreResourceType.Tile); done(); } }); diff --git a/src/source/raster_tile_source.ts b/src/source/raster_tile_source.ts index 481972bf55..017d941162 100644 --- a/src/source/raster_tile_source.ts +++ b/src/source/raster_tile_source.ts @@ -1,6 +1,7 @@ import {extend, pick} from '../util/util'; -import {getImage, ResourceType} from '../util/ajax'; +import {getImage} from '../util/ajax'; +import {MapLibreResourceType} from '../util/request_manager'; import {Event, ErrorEvent, Evented} from '../util/evented'; import loadTileJSON from './load_tilejson'; import TileBounds from './tile_bounds'; @@ -103,7 +104,7 @@ class RasterTileSource extends Evented implements Source { loadTile(tile: Tile, callback: Callback) { const url = tile.tileID.canonical.url(this.tiles, this.map.getPixelRatio(), this.scheme); - tile.request = getImage(this.map._requestManager.transformRequest(url, ResourceType.Tile), (err, img, expiry) => { + tile.request = getImage(this.map._requestManager.transformRequest(url, MapLibreResourceType.Tile), (err, img, expiry) => { delete tile.request; if (tile.aborted) { diff --git a/src/source/vector_tile_source.test.ts b/src/source/vector_tile_source.test.ts index 6ce270f4e2..f5df91fb21 100644 --- a/src/source/vector_tile_source.test.ts +++ b/src/source/vector_tile_source.test.ts @@ -4,7 +4,7 @@ import VectorTileSource from './vector_tile_source'; import Tile from './tile'; import {OverscaledTileID} from './tile_id'; import {Evented} from '../util/evented'; -import {RequestManager} from '../util/request_manager'; +import {MapLibreResourceType, RequestManager} from '../util/request_manager'; import fixturesSource from '../../test/unit/assets/source.json' assert {type: 'json'}; import {getMockDispatcher, getWrapDispatcher} from '../util/test/util'; import Map from '../ui/map'; @@ -82,7 +82,7 @@ describe('VectorTileSource', () => { createSource({url: '/source.json'}, transformSpy); server.respond(); - expect(transformSpy).toHaveBeenCalledWith('/source.json', 'Source'); + expect(transformSpy).toHaveBeenCalledWith('/source.json', MapLibreResourceType.Source); }); test('fires event with metadata property', done => { @@ -181,7 +181,7 @@ describe('VectorTileSource', () => { } as any as Tile; source.loadTile(tile, () => {}); expect(transformSpy).toHaveBeenCalledTimes(1); - expect(transformSpy).toHaveBeenCalledWith('http://example.com/10/5/5.png', 'Tile'); + expect(transformSpy).toHaveBeenCalledWith('http://example.com/10/5/5.png', MapLibreResourceType.Tile); done(); } }); diff --git a/src/source/vector_tile_source.ts b/src/source/vector_tile_source.ts index c169154986..f24789e7ad 100644 --- a/src/source/vector_tile_source.ts +++ b/src/source/vector_tile_source.ts @@ -3,7 +3,7 @@ import {Event, ErrorEvent, Evented} from '../util/evented'; import {extend, pick} from '../util/util'; import loadTileJSON from './load_tilejson'; import TileBounds from './tile_bounds'; -import {ResourceType} from '../util/ajax'; +import {MapLibreResourceType} from '../util/request_manager'; import type {Source} from './source'; import type {OverscaledTileID} from './tile_id'; @@ -178,7 +178,7 @@ class VectorTileSource extends Evented implements Source { loadTile(tile: Tile, callback: Callback) { const url = tile.tileID.canonical.url(this.tiles, this.map.getPixelRatio(), this.scheme); const params = { - request: this.map._requestManager.transformRequest(url, ResourceType.Tile), + request: this.map._requestManager.transformRequest(url, MapLibreResourceType.Tile), uid: tile.uid, tileID: tile.tileID, zoom: tile.tileID.overscaledZ, diff --git a/src/source/video_source.ts b/src/source/video_source.ts index 04b11b5efa..cda772d369 100644 --- a/src/source/video_source.ts +++ b/src/source/video_source.ts @@ -1,4 +1,5 @@ -import {getVideo, ResourceType} from '../util/ajax'; +import {getVideo} from '../util/ajax'; +import {MapLibreResourceType} from '../util/request_manager'; import ImageSource from './image_source'; import rasterBoundsAttributes from '../data/raster_bounds_attributes'; @@ -66,7 +67,7 @@ class VideoSource extends ImageSource { this.urls = []; for (const url of options.urls) { - this.urls.push(this.map._requestManager.transformRequest(url, ResourceType.Source).url); + this.urls.push(this.map._requestManager.transformRequest(url, MapLibreResourceType.Source).url); } getVideo(this.urls, (err, video) => { diff --git a/src/style/load_glyph_range.test.ts b/src/style/load_glyph_range.test.ts index 7753cf414d..b9a602766c 100644 --- a/src/style/load_glyph_range.test.ts +++ b/src/style/load_glyph_range.test.ts @@ -1,6 +1,6 @@ import fs from 'fs'; import path from 'path'; -import {RequestManager} from '../util/request_manager'; +import {MapLibreResourceType, RequestManager} from '../util/request_manager'; import loadGlyphRange from './load_glyph_range'; import {fakeXhr} from 'nise'; @@ -19,7 +19,7 @@ test('loadGlyphRange', done => { loadGlyphRange('Arial Unicode MS', 0, 'https://localhost/fonts/v1/{fontstack}/{range}.pbf', manager, (err, result) => { expect(err).toBeFalsy(); expect(transform).toHaveBeenCalledTimes(1); - expect(transform).toHaveBeenCalledWith('https://localhost/fonts/v1/Arial Unicode MS/0-255.pbf', 'Glyphs'); + expect(transform).toHaveBeenCalledWith('https://localhost/fonts/v1/Arial Unicode MS/0-255.pbf', MapLibreResourceType.Glyphs); expect(Object.keys(result)).toHaveLength(223); for (const key in result) { diff --git a/src/style/load_glyph_range.ts b/src/style/load_glyph_range.ts index 69e4e6c861..2905838f8d 100644 --- a/src/style/load_glyph_range.ts +++ b/src/style/load_glyph_range.ts @@ -1,4 +1,5 @@ -import {getArrayBuffer, ResourceType} from '../util/ajax'; +import {getArrayBuffer} from '../util/ajax'; +import {MapLibreResourceType} from '../util/request_manager'; import parseGlyphPBF from './parse_glyph_pbf'; @@ -18,7 +19,7 @@ export default function loadGlyphRange(fontstack: string, const request = requestManager.transformRequest( urlTemplate.replace('{fontstack}', fontstack).replace('{range}', `${begin}-${end}`), - ResourceType.Glyphs + MapLibreResourceType.Glyphs ); getArrayBuffer(request, (err?: Error | null, data?: ArrayBuffer | null) => { diff --git a/src/style/load_sprite.test.ts b/src/style/load_sprite.test.ts index 8ff0569efd..d0bafc0793 100644 --- a/src/style/load_sprite.test.ts +++ b/src/style/load_sprite.test.ts @@ -1,6 +1,6 @@ import fs from 'fs'; import path from 'path'; -import {RequestManager} from '../util/request_manager'; +import {MapLibreResourceType, RequestManager} from '../util/request_manager'; import loadSprite from './load_sprite'; import {fakeXhr} from 'nise'; import {RGBAImage} from '../util/image'; @@ -30,8 +30,8 @@ describe('loadSprite', () => { expect(err).toBeFalsy(); expect(transform).toHaveBeenCalledTimes(2); - expect(transform).toHaveBeenNthCalledWith(1, 'http://localhost:9966/test/unit/assets/sprite1.json', 'SpriteJSON'); - expect(transform).toHaveBeenNthCalledWith(2, 'http://localhost:9966/test/unit/assets/sprite1.png', 'SpriteImage'); + expect(transform).toHaveBeenNthCalledWith(1, 'http://localhost:9966/test/unit/assets/sprite1.json', MapLibreResourceType.SpriteJSON); + expect(transform).toHaveBeenNthCalledWith(2, 'http://localhost:9966/test/unit/assets/sprite1.png', MapLibreResourceType.SpriteImage); expect(Object.keys(result)).toHaveLength(1); expect(Object.keys(result)[0]).toBe('default'); @@ -68,10 +68,10 @@ describe('loadSprite', () => { expect(err).toBeFalsy(); expect(transform).toHaveBeenCalledTimes(4); - expect(transform).toHaveBeenNthCalledWith(1, 'http://localhost:9966/test/unit/assets/sprite1.json', 'SpriteJSON'); - expect(transform).toHaveBeenNthCalledWith(2, 'http://localhost:9966/test/unit/assets/sprite1.png', 'SpriteImage'); - expect(transform).toHaveBeenNthCalledWith(3, 'http://localhost:9966/test/unit/assets/sprite2.json', 'SpriteJSON'); - expect(transform).toHaveBeenNthCalledWith(4, 'http://localhost:9966/test/unit/assets/sprite2.png', 'SpriteImage'); + expect(transform).toHaveBeenNthCalledWith(1, 'http://localhost:9966/test/unit/assets/sprite1.json', MapLibreResourceType.SpriteJSON); + expect(transform).toHaveBeenNthCalledWith(2, 'http://localhost:9966/test/unit/assets/sprite1.png', MapLibreResourceType.SpriteImage); + expect(transform).toHaveBeenNthCalledWith(3, 'http://localhost:9966/test/unit/assets/sprite2.json', MapLibreResourceType.SpriteJSON); + expect(transform).toHaveBeenNthCalledWith(4, 'http://localhost:9966/test/unit/assets/sprite2.png', MapLibreResourceType.SpriteImage); expect(Object.keys(result)).toHaveLength(2); expect(Object.keys(result)[0]).toBe('sprite1'); @@ -180,8 +180,8 @@ describe('loadSprite', () => { expect(err).toBeFalsy(); expect(transform).toHaveBeenCalledTimes(2); - expect(transform).toHaveBeenNthCalledWith(1, 'http://localhost:9966/test/unit/assets/sprite1@2x.json', 'SpriteJSON'); - expect(transform).toHaveBeenNthCalledWith(2, 'http://localhost:9966/test/unit/assets/sprite1@2x.png', 'SpriteImage'); + expect(transform).toHaveBeenNthCalledWith(1, 'http://localhost:9966/test/unit/assets/sprite1@2x.json', MapLibreResourceType.SpriteJSON); + expect(transform).toHaveBeenNthCalledWith(2, 'http://localhost:9966/test/unit/assets/sprite1@2x.png', MapLibreResourceType.SpriteImage); expect(Object.keys(result)).toHaveLength(1); expect(Object.keys(result)[0]).toBe('default'); diff --git a/src/style/load_sprite.ts b/src/style/load_sprite.ts index 09a2d895f5..a04faf4742 100644 --- a/src/style/load_sprite.ts +++ b/src/style/load_sprite.ts @@ -1,4 +1,5 @@ -import {getJSON, getImage, ResourceType} from '../util/ajax'; +import {getJSON, getImage} from '../util/ajax'; +import {MapLibreResourceType} from '../util/request_manager'; import browser from '../util/browser'; import {RGBAImage} from '../util/image'; @@ -28,7 +29,7 @@ export default function loadSprite( for (const {id, url} of sprite) { // eslint-disable-next-line no-loop-func - const newJsonRequestsLength = jsonRequests.push(getJSON(requestManager.transformRequest(requestManager.normalizeSpriteURL(url, format, '.json'), ResourceType.SpriteJSON), (err?: Error | null, data?: any | null) => { + const newJsonRequestsLength = jsonRequests.push(getJSON(requestManager.transformRequest(requestManager.normalizeSpriteURL(url, format, '.json'), MapLibreResourceType.SpriteJSON), (err?: Error | null, data?: any | null) => { jsonRequests.splice(newJsonRequestsLength, 1); if (!error) { error = err; @@ -38,7 +39,7 @@ export default function loadSprite( })); // eslint-disable-next-line no-loop-func - const newImageRequestsLength = imageRequests.push(getImage(requestManager.transformRequest(requestManager.normalizeSpriteURL(url, format, '.png'), ResourceType.SpriteImage), (err, img) => { + const newImageRequestsLength = imageRequests.push(getImage(requestManager.transformRequest(requestManager.normalizeSpriteURL(url, format, '.png'), MapLibreResourceType.SpriteImage), (err, img) => { imageRequests.splice(newImageRequestsLength, 1); if (!error) { error = err; diff --git a/src/style/style.test.ts b/src/style/style.test.ts index 0dae461cc4..4d8de46a91 100644 --- a/src/style/style.test.ts +++ b/src/style/style.test.ts @@ -3,7 +3,7 @@ import SourceCache from '../source/source_cache'; import StyleLayer from './style_layer'; import Transform from '../geo/transform'; import {extend} from '../util/util'; -import {RequestManager} from '../util/request_manager'; +import {MapLibreResourceType, RequestManager} from '../util/request_manager'; import {Event, Evented} from '../util/evented'; import { setRTLTextPlugin, @@ -155,7 +155,7 @@ describe('Style#loadURL', () => { expect(spy).toHaveBeenCalledTimes(1); expect(spy.mock.calls[0][0]).toBe('style.json'); - expect(spy.mock.calls[0][1]).toBe('Style'); + expect(spy.mock.calls[0][1]).toBe(MapLibreResourceType.Style); }); test('validates the style', done => { @@ -316,9 +316,9 @@ describe('Style#loadJSON', () => { style.on('style.load', () => { expect(transformSpy).toHaveBeenCalledTimes(2); expect(transformSpy.mock.calls[0][0]).toBe('http://example.com/sprites/bright-v8.json'); - expect(transformSpy.mock.calls[0][1]).toBe('SpriteJSON'); + expect(transformSpy.mock.calls[0][1]).toBe(MapLibreResourceType.SpriteJSON); expect(transformSpy.mock.calls[1][0]).toBe('http://example.com/sprites/bright-v8.png'); - expect(transformSpy.mock.calls[1][1]).toBe('SpriteImage'); + expect(transformSpy.mock.calls[1][1]).toBe(MapLibreResourceType.SpriteImage); done(); }); diff --git a/src/style/style.ts b/src/style/style.ts index 79daae052f..ede918d234 100644 --- a/src/style/style.ts +++ b/src/style/style.ts @@ -8,7 +8,8 @@ import Light from './light'; import LineAtlas from '../render/line_atlas'; import {pick, clone, extend, deepEqual, filterObject, mapObject} from '../util/util'; import {coerceSpriteToArray} from '../util/style'; -import {getJSON, getReferrer, makeRequest, ResourceType} from '../util/ajax'; +import {getJSON, getReferrer, makeRequest} from '../util/ajax'; +import {MapLibreResourceType} from '../util/request_manager'; import browser from '../util/browser'; import Dispatcher from '../util/dispatcher'; import {validateStyle, emitValidationErrors as _emitValidationErrors} from './validate_style'; @@ -269,7 +270,7 @@ class Style extends Evented { options.validate = typeof options.validate === 'boolean' ? options.validate : true; - const request = this.map._requestManager.transformRequest(url, ResourceType.Style); + const request = this.map._requestManager.transformRequest(url, MapLibreResourceType.Style); this._request = getJSON(request, (error?: Error | null, json?: any | null) => { this._request = null; if (error) { diff --git a/src/ui/map.ts b/src/ui/map.ts index 37918a40cd..2e03c06392 100644 --- a/src/ui/map.ts +++ b/src/ui/map.ts @@ -2,8 +2,8 @@ import {extend, bindAll, warnOnce, uniqueId, isImageBitmap} from '../util/util'; import browser from '../util/browser'; import DOM from '../util/dom'; import packageJSON from '../../package.json' assert {type: 'json'}; -import {getImage, GetImageCallback, getJSON, ResourceType} from '../util/ajax'; -import {RequestManager} from '../util/request_manager'; +import {getImage, GetImageCallback, getJSON} from '../util/ajax'; +import {RequestManager, MapLibreResourceType} from '../util/request_manager'; import Style, {StyleSwapOptions} from '../style/style'; import EvaluationParameters from '../style/evaluation_parameters'; import Painter from '../render/painter'; @@ -1519,7 +1519,7 @@ class Map extends Camera { _diffStyle(style: StyleSpecification | string, options?: StyleSwapOptions & StyleOptions) { if (typeof style === 'string') { const url = style; - const request = this._requestManager.transformRequest(url, ResourceType.Style); + const request = this._requestManager.transformRequest(url, MapLibreResourceType.Style); getJSON(request, (error?: Error | null, json?: any | null) => { if (error) { this.fire(new ErrorEvent(error)); @@ -1949,7 +1949,7 @@ class Map extends Camera { * @see [Add an icon to the map](https://maplibre.org/maplibre-gl-js-docs/example/add-image/) */ loadImage(url: string, callback: GetImageCallback) { - getImage(this._requestManager.transformRequest(url, ResourceType.Image), callback); + getImage(this._requestManager.transformRequest(url, MapLibreResourceType.Image), callback); } /** diff --git a/src/util/ajax.ts b/src/util/ajax.ts index d38deb8ee3..b98a7663ce 100644 --- a/src/util/ajax.ts +++ b/src/util/ajax.ts @@ -5,39 +5,6 @@ import webpSupported from './webp_supported'; import type {Callback} from '../types/callback'; import type {Cancelable} from '../types/cancelable'; -export interface IResourceType { - Unknown: keyof this; - Style: keyof this; - Source: keyof this; - Tile: keyof this; - Glyphs: keyof this; - SpriteImage: keyof this; - SpriteJSON: keyof this; - Image: keyof this; -} - -/** - * The type of a resource. - * @private - * @readonly - * @enum {string} - */ -const ResourceType = { - Unknown: 'Unknown', - Style: 'Style', - Source: 'Source', - Tile: 'Tile', - Glyphs: 'Glyphs', - SpriteImage: 'SpriteImage', - SpriteJSON: 'SpriteJSON', - Image: 'Image' -} as IResourceType; -export {ResourceType}; - -if (typeof Object.freeze == 'function') { - Object.freeze(ResourceType); -} - /** * A `RequestParameters` object to be returned from Map.options.transformRequest callbacks. * @typedef {Object} RequestParameters diff --git a/src/util/request_manager.ts b/src/util/request_manager.ts index 987054eb64..d53e776d48 100644 --- a/src/util/request_manager.ts +++ b/src/util/request_manager.ts @@ -1,9 +1,20 @@ -import {IResourceType} from './ajax'; - import type {RequestParameters} from './ajax'; -type ResourceTypeEnum = keyof IResourceType; -export type RequestTransformFunction = (url: string, resourceType?: ResourceTypeEnum) => RequestParameters; +/** + * A type of MapLibre resource. + */ +export enum MapLibreResourceType { + Glyphs, + Image, + Source, + SpriteImage, + SpriteJSON, + Style, + Tile, + Unknown, +} + +export type RequestTransformFunction = (url: string, resourceType?: MapLibreResourceType) => RequestParameters; type UrlObject = { protocol: string; @@ -19,7 +30,7 @@ export class RequestManager { this._transformRequestFn = transformRequestFn; } - transformRequest(url: string, type: ResourceTypeEnum) { + transformRequest(url: string, type: MapLibreResourceType) { if (this._transformRequestFn) { return this._transformRequestFn(url, type) || {url}; } From 3d09fa64e1f4b52e4515761ee3ac5b957829f592 Mon Sep 17 00:00:00 2001 From: Matthew Mamonov Date: Fri, 27 Jan 2023 15:44:46 +0700 Subject: [PATCH 2/6] Re-run render tests From 501d7c2769a4a7eaefaf549e56693969a88fe3b5 Mon Sep 17 00:00:00 2001 From: Matthew Mamonov Date: Sat, 28 Jan 2023 15:10:16 +0700 Subject: [PATCH 3/6] Assign strting values to the enum --- src/util/request_manager.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/util/request_manager.ts b/src/util/request_manager.ts index d53e776d48..69821a47b8 100644 --- a/src/util/request_manager.ts +++ b/src/util/request_manager.ts @@ -4,14 +4,14 @@ import type {RequestParameters} from './ajax'; * A type of MapLibre resource. */ export enum MapLibreResourceType { - Glyphs, - Image, - Source, - SpriteImage, - SpriteJSON, - Style, - Tile, - Unknown, + Glyphs = 'Glyphs', + Image = 'Image', + Source = 'Source', + SpriteImage = 'SpriteImage', + SpriteJSON = 'Sprite', + Style = 'Style', + Tile = 'Tile', + Unknown = 'Unknown', } export type RequestTransformFunction = (url: string, resourceType?: MapLibreResourceType) => RequestParameters; From 718394fa73749eaabec7378b228a586083dc4941 Mon Sep 17 00:00:00 2001 From: Matthew Mamonov Date: Sat, 28 Jan 2023 15:11:56 +0700 Subject: [PATCH 4/6] Revert changes to tests --- src/source/image_source.test.ts | 8 ++++---- src/source/raster_dem_tile_source.test.ts | 6 +++--- src/source/raster_tile_source.test.ts | 6 +++--- src/source/vector_tile_source.test.ts | 6 +++--- src/style/load_glyph_range.test.ts | 4 ++-- src/style/load_sprite.test.ts | 18 +++++++++--------- src/style/style.test.ts | 8 ++++---- 7 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/source/image_source.test.ts b/src/source/image_source.test.ts index af4b543ac8..712b4358fc 100644 --- a/src/source/image_source.test.ts +++ b/src/source/image_source.test.ts @@ -3,7 +3,7 @@ import {Evented} from '../util/evented'; import Transform from '../geo/transform'; import {extend} from '../util/util'; import {fakeXhr} from 'nise'; -import {MapLibreResourceType, RequestManager} from '../util/request_manager'; +import {RequestManager} from '../util/request_manager'; import Dispatcher from '../util/dispatcher'; import {stubAjaxGetImage} from '../util/test/util'; @@ -72,7 +72,7 @@ describe('ImageSource', () => { respond(); expect(spy).toHaveBeenCalledTimes(1); expect(spy.mock.calls[0][0]).toBe('/image.png'); - expect(spy.mock.calls[0][1]).toBe(MapLibreResourceType.Image); + expect(spy.mock.calls[0][1]).toBe('Image'); }); test('updates url from updateImage', () => { @@ -83,12 +83,12 @@ describe('ImageSource', () => { respond(); expect(spy).toHaveBeenCalledTimes(1); expect(spy.mock.calls[0][0]).toBe('/image.png'); - expect(spy.mock.calls[0][1]).toBe(MapLibreResourceType.Image); + expect(spy.mock.calls[0][1]).toBe('Image'); source.updateImage({url: '/image2.png'}); respond(); expect(spy).toHaveBeenCalledTimes(2); expect(spy.mock.calls[1][0]).toBe('/image2.png'); - expect(spy.mock.calls[1][1]).toBe(MapLibreResourceType.Image); + expect(spy.mock.calls[1][1]).toBe('Image'); }); test('sets coordinates', () => { diff --git a/src/source/raster_dem_tile_source.test.ts b/src/source/raster_dem_tile_source.test.ts index 5fbd3be478..69f1f83277 100644 --- a/src/source/raster_dem_tile_source.test.ts +++ b/src/source/raster_dem_tile_source.test.ts @@ -1,7 +1,7 @@ import {fakeServer, FakeServer} from 'nise'; import RasterDEMTileSource from './raster_dem_tile_source'; import {OverscaledTileID} from './tile_id'; -import {MapLibreResourceType, RequestManager} from '../util/request_manager'; +import {RequestManager} from '../util/request_manager'; import Dispatcher from '../util/dispatcher'; import Tile from './tile'; @@ -48,7 +48,7 @@ describe('RasterTileSource', () => { server.respond(); expect(transformSpy.mock.calls[0][0]).toBe('/source.json'); - expect(transformSpy.mock.calls[0][1]).toBe(MapLibreResourceType.Source); + expect(transformSpy.mock.calls[0][1]).toBe('Source'); done(); }); @@ -74,7 +74,7 @@ describe('RasterTileSource', () => { expect(transformSpy).toHaveBeenCalledTimes(1); expect(transformSpy.mock.calls[0][0]).toBe('http://example.com/10/5/5.png'); - expect(transformSpy.mock.calls[0][1]).toBe(MapLibreResourceType.Tile); + expect(transformSpy.mock.calls[0][1]).toBe('Tile'); done(); } diff --git a/src/source/raster_tile_source.test.ts b/src/source/raster_tile_source.test.ts index 20babd772d..6a4cb7a577 100644 --- a/src/source/raster_tile_source.test.ts +++ b/src/source/raster_tile_source.test.ts @@ -1,6 +1,6 @@ import RasterTileSource from './raster_tile_source'; import {OverscaledTileID} from './tile_id'; -import {MapLibreResourceType, RequestManager} from '../util/request_manager'; +import {RequestManager} from '../util/request_manager'; import Dispatcher from '../util/dispatcher'; import {fakeServer, FakeServer} from 'nise'; import Tile from './tile'; @@ -48,7 +48,7 @@ describe('RasterTileSource', () => { server.respond(); expect(transformSpy.mock.calls[0][0]).toBe('/source.json'); - expect(transformSpy.mock.calls[0][1]).toBe(MapLibreResourceType.Source); + expect(transformSpy.mock.calls[0][1]).toBe('Source'); }); test('respects TileJSON.bounds', done => { @@ -126,7 +126,7 @@ describe('RasterTileSource', () => { source.loadTile(tile, () => {}); expect(transformSpy).toHaveBeenCalledTimes(1); expect(transformSpy.mock.calls[0][0]).toBe('http://example.com/10/5/5.png'); - expect(transformSpy.mock.calls[0][1]).toBe(MapLibreResourceType.Tile); + expect(transformSpy.mock.calls[0][1]).toBe('Tile'); done(); } }); diff --git a/src/source/vector_tile_source.test.ts b/src/source/vector_tile_source.test.ts index f5df91fb21..6ce270f4e2 100644 --- a/src/source/vector_tile_source.test.ts +++ b/src/source/vector_tile_source.test.ts @@ -4,7 +4,7 @@ import VectorTileSource from './vector_tile_source'; import Tile from './tile'; import {OverscaledTileID} from './tile_id'; import {Evented} from '../util/evented'; -import {MapLibreResourceType, RequestManager} from '../util/request_manager'; +import {RequestManager} from '../util/request_manager'; import fixturesSource from '../../test/unit/assets/source.json' assert {type: 'json'}; import {getMockDispatcher, getWrapDispatcher} from '../util/test/util'; import Map from '../ui/map'; @@ -82,7 +82,7 @@ describe('VectorTileSource', () => { createSource({url: '/source.json'}, transformSpy); server.respond(); - expect(transformSpy).toHaveBeenCalledWith('/source.json', MapLibreResourceType.Source); + expect(transformSpy).toHaveBeenCalledWith('/source.json', 'Source'); }); test('fires event with metadata property', done => { @@ -181,7 +181,7 @@ describe('VectorTileSource', () => { } as any as Tile; source.loadTile(tile, () => {}); expect(transformSpy).toHaveBeenCalledTimes(1); - expect(transformSpy).toHaveBeenCalledWith('http://example.com/10/5/5.png', MapLibreResourceType.Tile); + expect(transformSpy).toHaveBeenCalledWith('http://example.com/10/5/5.png', 'Tile'); done(); } }); diff --git a/src/style/load_glyph_range.test.ts b/src/style/load_glyph_range.test.ts index b9a602766c..7753cf414d 100644 --- a/src/style/load_glyph_range.test.ts +++ b/src/style/load_glyph_range.test.ts @@ -1,6 +1,6 @@ import fs from 'fs'; import path from 'path'; -import {MapLibreResourceType, RequestManager} from '../util/request_manager'; +import {RequestManager} from '../util/request_manager'; import loadGlyphRange from './load_glyph_range'; import {fakeXhr} from 'nise'; @@ -19,7 +19,7 @@ test('loadGlyphRange', done => { loadGlyphRange('Arial Unicode MS', 0, 'https://localhost/fonts/v1/{fontstack}/{range}.pbf', manager, (err, result) => { expect(err).toBeFalsy(); expect(transform).toHaveBeenCalledTimes(1); - expect(transform).toHaveBeenCalledWith('https://localhost/fonts/v1/Arial Unicode MS/0-255.pbf', MapLibreResourceType.Glyphs); + expect(transform).toHaveBeenCalledWith('https://localhost/fonts/v1/Arial Unicode MS/0-255.pbf', 'Glyphs'); expect(Object.keys(result)).toHaveLength(223); for (const key in result) { diff --git a/src/style/load_sprite.test.ts b/src/style/load_sprite.test.ts index d0bafc0793..8ff0569efd 100644 --- a/src/style/load_sprite.test.ts +++ b/src/style/load_sprite.test.ts @@ -1,6 +1,6 @@ import fs from 'fs'; import path from 'path'; -import {MapLibreResourceType, RequestManager} from '../util/request_manager'; +import {RequestManager} from '../util/request_manager'; import loadSprite from './load_sprite'; import {fakeXhr} from 'nise'; import {RGBAImage} from '../util/image'; @@ -30,8 +30,8 @@ describe('loadSprite', () => { expect(err).toBeFalsy(); expect(transform).toHaveBeenCalledTimes(2); - expect(transform).toHaveBeenNthCalledWith(1, 'http://localhost:9966/test/unit/assets/sprite1.json', MapLibreResourceType.SpriteJSON); - expect(transform).toHaveBeenNthCalledWith(2, 'http://localhost:9966/test/unit/assets/sprite1.png', MapLibreResourceType.SpriteImage); + expect(transform).toHaveBeenNthCalledWith(1, 'http://localhost:9966/test/unit/assets/sprite1.json', 'SpriteJSON'); + expect(transform).toHaveBeenNthCalledWith(2, 'http://localhost:9966/test/unit/assets/sprite1.png', 'SpriteImage'); expect(Object.keys(result)).toHaveLength(1); expect(Object.keys(result)[0]).toBe('default'); @@ -68,10 +68,10 @@ describe('loadSprite', () => { expect(err).toBeFalsy(); expect(transform).toHaveBeenCalledTimes(4); - expect(transform).toHaveBeenNthCalledWith(1, 'http://localhost:9966/test/unit/assets/sprite1.json', MapLibreResourceType.SpriteJSON); - expect(transform).toHaveBeenNthCalledWith(2, 'http://localhost:9966/test/unit/assets/sprite1.png', MapLibreResourceType.SpriteImage); - expect(transform).toHaveBeenNthCalledWith(3, 'http://localhost:9966/test/unit/assets/sprite2.json', MapLibreResourceType.SpriteJSON); - expect(transform).toHaveBeenNthCalledWith(4, 'http://localhost:9966/test/unit/assets/sprite2.png', MapLibreResourceType.SpriteImage); + expect(transform).toHaveBeenNthCalledWith(1, 'http://localhost:9966/test/unit/assets/sprite1.json', 'SpriteJSON'); + expect(transform).toHaveBeenNthCalledWith(2, 'http://localhost:9966/test/unit/assets/sprite1.png', 'SpriteImage'); + expect(transform).toHaveBeenNthCalledWith(3, 'http://localhost:9966/test/unit/assets/sprite2.json', 'SpriteJSON'); + expect(transform).toHaveBeenNthCalledWith(4, 'http://localhost:9966/test/unit/assets/sprite2.png', 'SpriteImage'); expect(Object.keys(result)).toHaveLength(2); expect(Object.keys(result)[0]).toBe('sprite1'); @@ -180,8 +180,8 @@ describe('loadSprite', () => { expect(err).toBeFalsy(); expect(transform).toHaveBeenCalledTimes(2); - expect(transform).toHaveBeenNthCalledWith(1, 'http://localhost:9966/test/unit/assets/sprite1@2x.json', MapLibreResourceType.SpriteJSON); - expect(transform).toHaveBeenNthCalledWith(2, 'http://localhost:9966/test/unit/assets/sprite1@2x.png', MapLibreResourceType.SpriteImage); + expect(transform).toHaveBeenNthCalledWith(1, 'http://localhost:9966/test/unit/assets/sprite1@2x.json', 'SpriteJSON'); + expect(transform).toHaveBeenNthCalledWith(2, 'http://localhost:9966/test/unit/assets/sprite1@2x.png', 'SpriteImage'); expect(Object.keys(result)).toHaveLength(1); expect(Object.keys(result)[0]).toBe('default'); diff --git a/src/style/style.test.ts b/src/style/style.test.ts index 4d8de46a91..0dae461cc4 100644 --- a/src/style/style.test.ts +++ b/src/style/style.test.ts @@ -3,7 +3,7 @@ import SourceCache from '../source/source_cache'; import StyleLayer from './style_layer'; import Transform from '../geo/transform'; import {extend} from '../util/util'; -import {MapLibreResourceType, RequestManager} from '../util/request_manager'; +import {RequestManager} from '../util/request_manager'; import {Event, Evented} from '../util/evented'; import { setRTLTextPlugin, @@ -155,7 +155,7 @@ describe('Style#loadURL', () => { expect(spy).toHaveBeenCalledTimes(1); expect(spy.mock.calls[0][0]).toBe('style.json'); - expect(spy.mock.calls[0][1]).toBe(MapLibreResourceType.Style); + expect(spy.mock.calls[0][1]).toBe('Style'); }); test('validates the style', done => { @@ -316,9 +316,9 @@ describe('Style#loadJSON', () => { style.on('style.load', () => { expect(transformSpy).toHaveBeenCalledTimes(2); expect(transformSpy.mock.calls[0][0]).toBe('http://example.com/sprites/bright-v8.json'); - expect(transformSpy.mock.calls[0][1]).toBe(MapLibreResourceType.SpriteJSON); + expect(transformSpy.mock.calls[0][1]).toBe('SpriteJSON'); expect(transformSpy.mock.calls[1][0]).toBe('http://example.com/sprites/bright-v8.png'); - expect(transformSpy.mock.calls[1][1]).toBe(MapLibreResourceType.SpriteImage); + expect(transformSpy.mock.calls[1][1]).toBe('SpriteImage'); done(); }); From 40a721f9a3c7679aa6f12b39fd41f816d1e34cbb Mon Sep 17 00:00:00 2001 From: Matthew Mamonov Date: Sat, 28 Jan 2023 15:19:28 +0700 Subject: [PATCH 5/6] Fix typo --- src/util/request_manager.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/request_manager.ts b/src/util/request_manager.ts index 69821a47b8..244257e1e9 100644 --- a/src/util/request_manager.ts +++ b/src/util/request_manager.ts @@ -8,7 +8,7 @@ export enum MapLibreResourceType { Image = 'Image', Source = 'Source', SpriteImage = 'SpriteImage', - SpriteJSON = 'Sprite', + SpriteJSON = 'SpriteJSON', Style = 'Style', Tile = 'Tile', Unknown = 'Unknown', From 6aaea169a53841499b791d7a7ae29362cc6865d3 Mon Sep 17 00:00:00 2001 From: Matthew Mamonov Date: Mon, 30 Jan 2023 16:19:19 +0700 Subject: [PATCH 6/6] Don't use MapLibre prefix; use const enum --- src/source/geojson_source.ts | 4 ++-- src/source/image_source.ts | 4 ++-- src/source/load_tilejson.ts | 4 ++-- src/source/raster_dem_tile_source.ts | 4 ++-- src/source/raster_tile_source.ts | 4 ++-- src/source/vector_tile_source.ts | 4 ++-- src/source/video_source.ts | 4 ++-- src/style/load_glyph_range.ts | 4 ++-- src/style/load_sprite.ts | 6 +++--- src/style/style.ts | 4 ++-- src/ui/map.ts | 6 +++--- src/util/request_manager.ts | 6 +++--- 12 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/source/geojson_source.ts b/src/source/geojson_source.ts index d4997dbb9b..7c7979303d 100644 --- a/src/source/geojson_source.ts +++ b/src/source/geojson_source.ts @@ -2,7 +2,7 @@ import {Event, ErrorEvent, Evented} from '../util/evented'; import {extend} from '../util/util'; import EXTENT from '../data/extent'; -import {MapLibreResourceType} from '../util/request_manager'; +import {ResourceType} from '../util/request_manager'; import browser from '../util/browser'; import type {Source} from './source'; @@ -323,7 +323,7 @@ class GeoJSONSource extends Evented implements Source { if (diff) { options.dataDiff = diff; } else if (typeof this._data === 'string') { - options.request = this.map._requestManager.transformRequest(browser.resolveURL(this._data as string), MapLibreResourceType.Source); + options.request = this.map._requestManager.transformRequest(browser.resolveURL(this._data as string), ResourceType.Source); options.request.collectResourceTiming = this._collectResourceTiming; } else { options.data = JSON.stringify(this._data); diff --git a/src/source/image_source.ts b/src/source/image_source.ts index 69123744f9..3b67d67b13 100644 --- a/src/source/image_source.ts +++ b/src/source/image_source.ts @@ -1,7 +1,7 @@ import {CanonicalTileID} from './tile_id'; import {Event, ErrorEvent, Evented} from '../util/evented'; import {getImage} from '../util/ajax'; -import {MapLibreResourceType} from '../util/request_manager'; +import {ResourceType} from '../util/request_manager'; import EXTENT from '../data/extent'; import {RasterBoundsArray} from '../data/array_types.g'; import rasterBoundsAttributes from '../data/raster_bounds_attributes'; @@ -112,7 +112,7 @@ class ImageSource extends Evented implements Source { this.url = this.options.url; - this._request = getImage(this.map._requestManager.transformRequest(this.url, MapLibreResourceType.Image), (err, image) => { + this._request = getImage(this.map._requestManager.transformRequest(this.url, ResourceType.Image), (err, image) => { this._request = null; this._loaded = true; diff --git a/src/source/load_tilejson.ts b/src/source/load_tilejson.ts index 3cd59dc86d..79799a01e2 100644 --- a/src/source/load_tilejson.ts +++ b/src/source/load_tilejson.ts @@ -1,7 +1,7 @@ import {pick, extend} from '../util/util'; import {getJSON} from '../util/ajax'; -import {MapLibreResourceType} from '../util/request_manager'; +import {ResourceType} from '../util/request_manager'; import browser from '../util/browser'; import type {RequestManager} from '../util/request_manager'; @@ -35,7 +35,7 @@ export default function loadTileJson( }; if (options.url) { - return getJSON(requestManager.transformRequest(options.url, MapLibreResourceType.Source), loaded); + return getJSON(requestManager.transformRequest(options.url, ResourceType.Source), loaded); } else { return browser.frame(() => loaded(null, options)); } diff --git a/src/source/raster_dem_tile_source.ts b/src/source/raster_dem_tile_source.ts index f86867ad6d..92286dd692 100644 --- a/src/source/raster_dem_tile_source.ts +++ b/src/source/raster_dem_tile_source.ts @@ -1,5 +1,5 @@ import {getImage} from '../util/ajax'; -import {MapLibreResourceType} from '../util/request_manager'; +import {ResourceType} from '../util/request_manager'; import {extend, isImageBitmap} from '../util/util'; import {Evented} from '../util/evented'; import browser from '../util/browser'; @@ -39,7 +39,7 @@ class RasterDEMTileSource extends RasterTileSource implements Source { loadTile(tile: Tile, callback: Callback) { const url = tile.tileID.canonical.url(this.tiles, this.map.getPixelRatio(), this.scheme); - tile.request = getImage(this.map._requestManager.transformRequest(url, MapLibreResourceType.Tile), imageLoaded.bind(this)); + tile.request = getImage(this.map._requestManager.transformRequest(url, ResourceType.Tile), imageLoaded.bind(this)); tile.neighboringTiles = this._getNeighboringTiles(tile.tileID); function imageLoaded(err, img) { diff --git a/src/source/raster_tile_source.ts b/src/source/raster_tile_source.ts index 017d941162..f53c83ddd4 100644 --- a/src/source/raster_tile_source.ts +++ b/src/source/raster_tile_source.ts @@ -1,7 +1,7 @@ import {extend, pick} from '../util/util'; import {getImage} from '../util/ajax'; -import {MapLibreResourceType} from '../util/request_manager'; +import {ResourceType} from '../util/request_manager'; import {Event, ErrorEvent, Evented} from '../util/evented'; import loadTileJSON from './load_tilejson'; import TileBounds from './tile_bounds'; @@ -104,7 +104,7 @@ class RasterTileSource extends Evented implements Source { loadTile(tile: Tile, callback: Callback) { const url = tile.tileID.canonical.url(this.tiles, this.map.getPixelRatio(), this.scheme); - tile.request = getImage(this.map._requestManager.transformRequest(url, MapLibreResourceType.Tile), (err, img, expiry) => { + tile.request = getImage(this.map._requestManager.transformRequest(url, ResourceType.Tile), (err, img, expiry) => { delete tile.request; if (tile.aborted) { diff --git a/src/source/vector_tile_source.ts b/src/source/vector_tile_source.ts index f24789e7ad..9bf5c570f0 100644 --- a/src/source/vector_tile_source.ts +++ b/src/source/vector_tile_source.ts @@ -3,7 +3,7 @@ import {Event, ErrorEvent, Evented} from '../util/evented'; import {extend, pick} from '../util/util'; import loadTileJSON from './load_tilejson'; import TileBounds from './tile_bounds'; -import {MapLibreResourceType} from '../util/request_manager'; +import {ResourceType} from '../util/request_manager'; import type {Source} from './source'; import type {OverscaledTileID} from './tile_id'; @@ -178,7 +178,7 @@ class VectorTileSource extends Evented implements Source { loadTile(tile: Tile, callback: Callback) { const url = tile.tileID.canonical.url(this.tiles, this.map.getPixelRatio(), this.scheme); const params = { - request: this.map._requestManager.transformRequest(url, MapLibreResourceType.Tile), + request: this.map._requestManager.transformRequest(url, ResourceType.Tile), uid: tile.uid, tileID: tile.tileID, zoom: tile.tileID.overscaledZ, diff --git a/src/source/video_source.ts b/src/source/video_source.ts index cda772d369..8bb9a13dd7 100644 --- a/src/source/video_source.ts +++ b/src/source/video_source.ts @@ -1,5 +1,5 @@ import {getVideo} from '../util/ajax'; -import {MapLibreResourceType} from '../util/request_manager'; +import {ResourceType} from '../util/request_manager'; import ImageSource from './image_source'; import rasterBoundsAttributes from '../data/raster_bounds_attributes'; @@ -67,7 +67,7 @@ class VideoSource extends ImageSource { this.urls = []; for (const url of options.urls) { - this.urls.push(this.map._requestManager.transformRequest(url, MapLibreResourceType.Source).url); + this.urls.push(this.map._requestManager.transformRequest(url, ResourceType.Source).url); } getVideo(this.urls, (err, video) => { diff --git a/src/style/load_glyph_range.ts b/src/style/load_glyph_range.ts index 2905838f8d..066f8b284d 100644 --- a/src/style/load_glyph_range.ts +++ b/src/style/load_glyph_range.ts @@ -1,5 +1,5 @@ import {getArrayBuffer} from '../util/ajax'; -import {MapLibreResourceType} from '../util/request_manager'; +import {ResourceType} from '../util/request_manager'; import parseGlyphPBF from './parse_glyph_pbf'; @@ -19,7 +19,7 @@ export default function loadGlyphRange(fontstack: string, const request = requestManager.transformRequest( urlTemplate.replace('{fontstack}', fontstack).replace('{range}', `${begin}-${end}`), - MapLibreResourceType.Glyphs + ResourceType.Glyphs ); getArrayBuffer(request, (err?: Error | null, data?: ArrayBuffer | null) => { diff --git a/src/style/load_sprite.ts b/src/style/load_sprite.ts index a04faf4742..62f0b32a0b 100644 --- a/src/style/load_sprite.ts +++ b/src/style/load_sprite.ts @@ -1,5 +1,5 @@ import {getJSON, getImage} from '../util/ajax'; -import {MapLibreResourceType} from '../util/request_manager'; +import {ResourceType} from '../util/request_manager'; import browser from '../util/browser'; import {RGBAImage} from '../util/image'; @@ -29,7 +29,7 @@ export default function loadSprite( for (const {id, url} of sprite) { // eslint-disable-next-line no-loop-func - const newJsonRequestsLength = jsonRequests.push(getJSON(requestManager.transformRequest(requestManager.normalizeSpriteURL(url, format, '.json'), MapLibreResourceType.SpriteJSON), (err?: Error | null, data?: any | null) => { + const newJsonRequestsLength = jsonRequests.push(getJSON(requestManager.transformRequest(requestManager.normalizeSpriteURL(url, format, '.json'), ResourceType.SpriteJSON), (err?: Error | null, data?: any | null) => { jsonRequests.splice(newJsonRequestsLength, 1); if (!error) { error = err; @@ -39,7 +39,7 @@ export default function loadSprite( })); // eslint-disable-next-line no-loop-func - const newImageRequestsLength = imageRequests.push(getImage(requestManager.transformRequest(requestManager.normalizeSpriteURL(url, format, '.png'), MapLibreResourceType.SpriteImage), (err, img) => { + const newImageRequestsLength = imageRequests.push(getImage(requestManager.transformRequest(requestManager.normalizeSpriteURL(url, format, '.png'), ResourceType.SpriteImage), (err, img) => { imageRequests.splice(newImageRequestsLength, 1); if (!error) { error = err; diff --git a/src/style/style.ts b/src/style/style.ts index ede918d234..8d05499586 100644 --- a/src/style/style.ts +++ b/src/style/style.ts @@ -9,7 +9,7 @@ import LineAtlas from '../render/line_atlas'; import {pick, clone, extend, deepEqual, filterObject, mapObject} from '../util/util'; import {coerceSpriteToArray} from '../util/style'; import {getJSON, getReferrer, makeRequest} from '../util/ajax'; -import {MapLibreResourceType} from '../util/request_manager'; +import {ResourceType} from '../util/request_manager'; import browser from '../util/browser'; import Dispatcher from '../util/dispatcher'; import {validateStyle, emitValidationErrors as _emitValidationErrors} from './validate_style'; @@ -270,7 +270,7 @@ class Style extends Evented { options.validate = typeof options.validate === 'boolean' ? options.validate : true; - const request = this.map._requestManager.transformRequest(url, MapLibreResourceType.Style); + const request = this.map._requestManager.transformRequest(url, ResourceType.Style); this._request = getJSON(request, (error?: Error | null, json?: any | null) => { this._request = null; if (error) { diff --git a/src/ui/map.ts b/src/ui/map.ts index 2e03c06392..d73a56dac0 100644 --- a/src/ui/map.ts +++ b/src/ui/map.ts @@ -3,7 +3,7 @@ import browser from '../util/browser'; import DOM from '../util/dom'; import packageJSON from '../../package.json' assert {type: 'json'}; import {getImage, GetImageCallback, getJSON} from '../util/ajax'; -import {RequestManager, MapLibreResourceType} from '../util/request_manager'; +import {RequestManager, ResourceType} from '../util/request_manager'; import Style, {StyleSwapOptions} from '../style/style'; import EvaluationParameters from '../style/evaluation_parameters'; import Painter from '../render/painter'; @@ -1519,7 +1519,7 @@ class Map extends Camera { _diffStyle(style: StyleSpecification | string, options?: StyleSwapOptions & StyleOptions) { if (typeof style === 'string') { const url = style; - const request = this._requestManager.transformRequest(url, MapLibreResourceType.Style); + const request = this._requestManager.transformRequest(url, ResourceType.Style); getJSON(request, (error?: Error | null, json?: any | null) => { if (error) { this.fire(new ErrorEvent(error)); @@ -1949,7 +1949,7 @@ class Map extends Camera { * @see [Add an icon to the map](https://maplibre.org/maplibre-gl-js-docs/example/add-image/) */ loadImage(url: string, callback: GetImageCallback) { - getImage(this._requestManager.transformRequest(url, MapLibreResourceType.Image), callback); + getImage(this._requestManager.transformRequest(url, ResourceType.Image), callback); } /** diff --git a/src/util/request_manager.ts b/src/util/request_manager.ts index 244257e1e9..7f3aef8411 100644 --- a/src/util/request_manager.ts +++ b/src/util/request_manager.ts @@ -3,7 +3,7 @@ import type {RequestParameters} from './ajax'; /** * A type of MapLibre resource. */ -export enum MapLibreResourceType { +export const enum ResourceType { Glyphs = 'Glyphs', Image = 'Image', Source = 'Source', @@ -14,7 +14,7 @@ export enum MapLibreResourceType { Unknown = 'Unknown', } -export type RequestTransformFunction = (url: string, resourceType?: MapLibreResourceType) => RequestParameters; +export type RequestTransformFunction = (url: string, resourceType?: ResourceType) => RequestParameters; type UrlObject = { protocol: string; @@ -30,7 +30,7 @@ export class RequestManager { this._transformRequestFn = transformRequestFn; } - transformRequest(url: string, type: MapLibreResourceType) { + transformRequest(url: string, type: ResourceType) { if (this._transformRequestFn) { return this._transformRequestFn(url, type) || {url}; }