Skip to content

Commit 7be796d

Browse files
mournergithub-actions[bot]
authored andcommitted
Expose setWorkerUrl for ESM entry point
GitOrigin-RevId: 894f6dc62c1ac1cefef6002305f95b908884fd5c
1 parent ae84112 commit 7be796d

5 files changed

Lines changed: 24 additions & 29 deletions

File tree

src/index.esm.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ export {FreeCameraOptions} from './ui/free_camera';
166166
export {setRTLTextPlugin, getRTLTextPluginStatus} from './source/rtl_text_plugin';
167167
export {addTileProvider} from './source/tile_provider';
168168
export {prewarm, clearPrewarmedResources} from './util/worker_pool_factory';
169+
export {setWorkerUrl} from './util/worker_class';
169170
export {getWorkerCount, setWorkerCount} from './util/worker_pool';
170171
export {setAccessToken, setBaseApiUrl, setMaxParallelImageRequests, getDracoUrl, setDracoUrl, getMeshoptUrl, setMeshoptUrl, getBuildingGenUrl, setBuildingGenUrl} from './util/config';
171172

src/index.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -274,14 +274,6 @@ const exported = {
274274
WorkerClass.workerClass = klass;
275275
},
276276

277-
get workerParams(): WorkerOptions {
278-
return WorkerClass.workerParams;
279-
},
280-
281-
set workerParams(params: WorkerOptions) {
282-
WorkerClass.workerParams = params;
283-
},
284-
285277
/**
286278
* Provides an interface for loading Draco decoding library (draco_decoder_gltf.wasm v1.5.6) from a self-hosted URL.
287279
* This needs to be set only once, and before any call to `new mapboxgl.Map(..)` takes place.

src/util/worker_class.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
import type {Class} from '../types/class';
22

3-
type WorkerClass = {
3+
type WorkerState = {
44
workerUrl: string;
55
workerClass: Class<Worker> | null;
6-
workerParams?: WorkerOptions;
6+
workerParams?: WorkerOptions; // Internal, test-only: extra options passed to `new Worker()`
77
};
88

9-
const WorkerClass: WorkerClass = {
9+
const WorkerClass: WorkerState = {
1010
workerUrl: '',
1111
workerClass: null,
1212
workerParams: undefined,
1313
};
1414

15+
/**
16+
* Sets the URL from which the WebWorker bundle is loaded. Must be set once,
17+
* before the first `new Map(...)`. Used by both the UMD and ESM bundles to
18+
* override the default worker location — for example to load the worker from a
19+
* same-origin trampoline when GL JS is served cross-origin.
20+
*
21+
* @param {string} url A URL hosting the GL JS WebWorker bundle.
22+
*/
23+
export function setWorkerUrl(url: string) {
24+
WorkerClass.workerUrl = url;
25+
}
26+
1527
export default WorkerClass;

test/unit/setup.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,10 @@ import WorkerClass from '../../src/util/worker_class';
55
import {prewarm} from '../../src/util/worker_pool_factory';
66
import {markTestBaseline, cleanupTestMaps} from '../util/vitest';
77

8-
if (!globalThis.defined) {
9-
WorkerClass.workerParams = {
10-
type: 'module'
11-
};
12-
8+
if (!WorkerClass.workerUrl) {
9+
// Internal, test-only: load the untranspiled worker source as an ES module.
10+
WorkerClass.workerParams = {type: 'module'};
1311
WorkerClass.workerUrl = '/src/source/worker.ts';
14-
15-
globalThis.defined = true;
1612
}
1713

1814
// Keep the shared WorkerPool alive across all tests in this iframe. Without

test/usvg/setup.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
/* global globalThis */
1+
import WorkerClass from '../../src/util/worker_class';
22

3-
import mapboxgl from '../../src/index';
4-
5-
if (!globalThis.defined) {
6-
mapboxgl.workerParams = {
7-
type: 'module'
8-
};
9-
10-
mapboxgl.workerUrl = '/src/source/worker.ts';
11-
12-
globalThis.defined = true;
3+
if (!WorkerClass.workerUrl) {
4+
// Internal, test-only: load the untranspiled worker source as an ES module.
5+
WorkerClass.workerParams = {type: 'module'};
6+
WorkerClass.workerUrl = '/src/source/worker.ts';
137
}

0 commit comments

Comments
 (0)