Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine ResourceType interface and const into a single enum #2103

Merged
merged 6 commits into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/source/geojson_source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 3 additions & 2 deletions src/source/image_source.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;

Expand Down
5 changes: 3 additions & 2 deletions src/source/load_tilejson.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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));
}
Expand Down
5 changes: 3 additions & 2 deletions src/source/raster_dem_tile_source.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -38,7 +39,7 @@ class RasterDEMTileSource extends RasterTileSource implements Source {

loadTile(tile: Tile, callback: Callback<void>) {
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) {
Expand Down
5 changes: 3 additions & 2 deletions src/source/raster_tile_source.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -103,7 +104,7 @@ class RasterTileSource extends Evented implements Source {

loadTile(tile: Tile, callback: Callback<void>) {
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) {
Expand Down
4 changes: 2 additions & 2 deletions src/source/vector_tile_source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -178,7 +178,7 @@ class VectorTileSource extends Evented implements Source {
loadTile(tile: Tile, callback: Callback<void>) {
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,
Expand Down
5 changes: 3 additions & 2 deletions src/source/video_source.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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) => {
Expand Down
5 changes: 3 additions & 2 deletions src/style/load_glyph_range.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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) => {
Expand Down
7 changes: 4 additions & 3 deletions src/style/load_sprite.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/style/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions src/ui/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
}

/**
Expand Down
33 changes: 0 additions & 33 deletions src/util/ajax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 16 additions & 5 deletions src/util/request_manager.ts
Original file line number Diff line number Diff line change
@@ -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 {
smellyshovel marked this conversation as resolved.
Show resolved Hide resolved
Glyphs = 'Glyphs',
Image = 'Image',
Source = 'Source',
SpriteImage = 'SpriteImage',
SpriteJSON = 'SpriteJSON',
Style = 'Style',
Tile = 'Tile',
Unknown = 'Unknown',
}

export type RequestTransformFunction = (url: string, resourceType?: MapLibreResourceType) => RequestParameters;

type UrlObject = {
protocol: string;
Expand All @@ -19,7 +30,7 @@ export class RequestManager {
this._transformRequestFn = transformRequestFn;
}

transformRequest(url: string, type: ResourceTypeEnum) {
transformRequest(url: string, type: MapLibreResourceType) {
smellyshovel marked this conversation as resolved.
Show resolved Hide resolved
if (this._transformRequestFn) {
return this._transformRequestFn(url, type) || {url};
}
Expand Down