@@ -26,6 +26,8 @@ import WorkerPool from './util/worker_pool';
2626import { prewarm , clearPrewarmedResources } from './util/global_worker_pool' ;
2727import { clearTileCache } from './util/tile_request_cache' ;
2828import { PerformanceUtils } from './util/performance' ;
29+ import type { RequestParameters , ResponseCallback } from './util/ajax' ;
30+ import type { Cancelable } from './types/cancelable' ;
2931
3032const exported = {
3133 version ,
@@ -170,7 +172,52 @@ const exported = {
170172 clearTileCache ( callback ) ;
171173 } ,
172174
173- workerUrl : ''
175+ workerUrl : '' ,
176+
177+ /**
178+ * Sets a custom load tile function that will be called when using a source that starts with a custom url schema.
179+ * The example below will be triggered for custom:// urls defined in the sources list in the style definitions.
180+ * The function passed will receive the request parameters and should call the callback with the resulting request,
181+ * for example a pbf vector tile, non-compressed, represented as ArrayBuffer.
182+ * @param {string } customProtocol - the protocol to hook, for example 'custom'
183+ * @param {Function } loadFn - the function to use when trying to fetch a tile specified by the customProtocol
184+ * @example
185+ * // this will fetch a file using the fetch API (this is obviously a non iteresting example...)
186+ * maplibre.addProtocol('custom', (params, callback) => {
187+ fetch(`https://${params.url.split("://")[1]}`)
188+ .then(t => {
189+ if (t.status == 200) {
190+ t.arrayBuffer().then(arr => {
191+ callback(null, arr, null, null);
192+ });
193+ } else {
194+ callback(new Error(`Tile fetch error: ${t.statusText}`));
195+ }
196+ })
197+ .catch(e => {
198+ callback(new Error(e));
199+ });
200+ return { cancel: () => { } };
201+ });
202+ * // the following is an example of a way to return an error when trying to load a tile
203+ * maplibre.addProtocol('custom2', (params, callback) => {
204+ * callback(new Error('someErrorMessage'));
205+ * return { cancel: () => { } };
206+ * });
207+ */
208+ addProtocol ( customProtocol : string , loadFn : ( requestParameters : RequestParameters , callback : ResponseCallback < any > ) => Cancelable ) {
209+ config . REGISTERED_PROTOCOLS [ customProtocol ] = loadFn ;
210+ } ,
211+
212+ /**
213+ * Removes a previusly added protocol
214+ * @param {string } customProtocol - the custom protocol to remove registration for
215+ * @example
216+ * maplibregl.removeProtocol('custom');
217+ */
218+ removeProtocol ( customProtocol : string ) {
219+ delete config . REGISTERED_PROTOCOLS [ customProtocol ] ;
220+ }
174221} ;
175222
176223//This gets automatically stripped out in production builds.
0 commit comments