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

Support custom tiles source #30

Merged
merged 24 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
29 changes: 28 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import WorkerPool from './util/worker_pool';
import {prewarm, clearPrewarmedResources} from './util/global_worker_pool';
import {clearTileCache} from './util/tile_request_cache';
import {PerformanceUtils} from './util/performance';
import type {RequestParameters, ResponseCallback} from './util/ajax';
import type {Cancelable} from './types/cancelable';

const exported = {
version,
Expand Down Expand Up @@ -170,7 +172,32 @@ const exported = {
clearTileCache(callback);
},

workerUrl: ''
workerUrl: '',

/**
* Sets a custom load tile function that will be called when the using a source that starts with a custom url.
* The example below will be triggered for custom:// urls.
* The function to be used will recieve the request parameters and should call the callback with the resulting request,
HarelM marked this conversation as resolved.
Show resolved Hide resolved
* For example a pbf vector tile non compressed represented as ArrayBuffer:
* ```
* addCustomTilesFunction('custom', (params, callback) => {
* const myArrayBufferOfTheTile = getTheTileByUsingTheRequest(params);
* callback(null, myArrayBufferOfTheTile, null, null);
* return { cancel: () => { } };
* });
* // the following is an example of a way to return an error when trying to load a tile
* addCustomTilesFunction('custom', (params, callback) => {
* callback(new Error(someErrorMessage));
* return { cancel: () => { } };
* });
* ```
*/
addCustomTilesFunction(customUrl: string, loadFn: (requestParameters: RequestParameters, callback: ResponseCallback<any>) => Cancelable) {
config.LOAD_TILES_FUNCTION_MAP[customUrl] = loadFn;
},
removeCustomTilesFunction(customUrl: string) {
delete config.LOAD_TILES_FUNCTION_MAP[customUrl];
}
};

//This gets automatically stripped out in production builds.
Expand Down
9 changes: 9 additions & 0 deletions src/util/ajax.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,15 @@ export const makeRequest = function(requestParameters: RequestParameters, callba
// some versions (see https://bugs.webkit.org/show_bug.cgi?id=174980#c2)
// - Requests for resources with the file:// URI scheme don't work with the Fetch API either. In
// this case we unconditionally use XHR on the current thread since referrers don't matter.
let key = Object.keys(config.LOAD_TILES_FUNCTION_MAP || {}).find(k => requestParameters.url.startsWith(k));
HarelM marked this conversation as resolved.
Show resolved Hide resolved
if (key) {
if (isWorker() && self.worker && self.worker.actor) {
return self.worker.actor.send('getResource', requestParameters, callback);
}
if (!isWorker()) {
return config.LOAD_TILES_FUNCTION_MAP[key](requestParameters, callback);
}
}
if (!isFileURL(requestParameters.url)) {
if (window.fetch && window.Request && window.AbortController && window.Request.prototype.hasOwnProperty('signal')) {
return makeFetchRequest(requestParameters, callback);
Expand Down
6 changes: 4 additions & 2 deletions src/util/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ type Config = {|
FEEDBACK_URL: string,
REQUIRE_ACCESS_TOKEN: boolean,
ACCESS_TOKEN: ?string,
MAX_PARALLEL_IMAGE_REQUESTS: number
MAX_PARALLEL_IMAGE_REQUESTS: number,
LOAD_TILES_FUNCTION_MAP: object,
|};

const config: Config = {
Expand All @@ -24,7 +25,8 @@ const config: Config = {
FEEDBACK_URL: 'https://apps.mapbox.com/feedback',
REQUIRE_ACCESS_TOKEN: true,
ACCESS_TOKEN: null,
MAX_PARALLEL_IMAGE_REQUESTS: 16
MAX_PARALLEL_IMAGE_REQUESTS: 16,
LOAD_TILES_FUNCTION_MAP: {}
};

export default config;