Skip to content

Commit e7aa478

Browse files
stepankuzmingithub-actions[bot]
authored andcommitted
Refactor getWorkerSource interface
GitOrigin-RevId: 2e96d1ddf677f0db069280fa021786713f7c7d16
1 parent 2fcb758 commit e7aa478

5 files changed

Lines changed: 23 additions & 22 deletions

File tree

src/source/worker.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,10 @@ import type {TaskMetadata} from '../util/scheduler';
2020
import type {RtlTextPlugin} from './rtl_text_plugin';
2121
import type {RasterizedImageMap} from '../render/image_manager';
2222
import type {ActorMessage, ActorMessages} from '../util/actor_messages';
23-
import type {WorkerSource, WorkerSourceConstructor} from './worker_source';
23+
import type {WorkerSourceType, WorkerSource, WorkerSourceConstructor, WorkerSourceRequest} from './worker_source';
2424
import type {StyleModelMap} from '../style/style_mode';
2525
import type {Callback} from '../types/callback';
2626

27-
/**
28-
* Source types that can instantiate a {@link WorkerSource} in {@link MapWorker}.
29-
*/
30-
type WorkerSourceType =
31-
| 'vector'
32-
| 'geojson'
33-
| 'raster-dem'
34-
| 'raster-array'
35-
| 'batched-model';
36-
3727
/**
3828
* Generic type for grouping items by mapId and style scope.
3929
*/
@@ -232,27 +222,27 @@ export default class MapWorker {
232222
loadTile(mapId: number, params: ActorMessages['loadTile']['params'], callback: ActorMessages['loadTile']['callback']) {
233223
assert(params.type);
234224
params.projection = this.projections[mapId] || this.defaultProjection;
235-
this.getWorkerSource(mapId, params.type, params.source, params.scope).loadTile(params, callback);
225+
this.getWorkerSource(mapId, params).loadTile(params, callback);
236226
}
237227

238228
decodeRasterArray(mapId: number, params: ActorMessages['decodeRasterArray']['params'], callback: ActorMessages['decodeRasterArray']['callback']) {
239-
(this.getWorkerSource(mapId, params.type, params.source, params.scope) as RasterArrayTileWorkerSource).decodeRasterArray(params, callback);
229+
(this.getWorkerSource(mapId, params) as RasterArrayTileWorkerSource).decodeRasterArray(params, callback);
240230
}
241231

242232
reloadTile(mapId: number, params: ActorMessages['reloadTile']['params'], callback: ActorMessages['reloadTile']['callback']) {
243233
assert(params.type);
244234
params.projection = this.projections[mapId] || this.defaultProjection;
245-
this.getWorkerSource(mapId, params.type, params.source, params.scope).reloadTile(params, callback);
235+
this.getWorkerSource(mapId, params).reloadTile(params, callback);
246236
}
247237

248238
abortTile(mapId: number, params: ActorMessages['abortTile']['params'], callback: ActorMessages['abortTile']['callback']) {
249239
assert(params.type);
250-
this.getWorkerSource(mapId, params.type, params.source, params.scope).abortTile(params, callback);
240+
this.getWorkerSource(mapId, params).abortTile(params, callback);
251241
}
252242

253243
removeTile(mapId: number, params: ActorMessages['removeTile']['params'], callback: ActorMessages['removeTile']['callback']) {
254244
assert(params.type);
255-
this.getWorkerSource(mapId, params.type, params.source, params.scope).removeTile(params, callback);
245+
this.getWorkerSource(mapId, params).removeTile(params, callback);
256246
}
257247

258248
removeSource(mapId: number, params: ActorMessages['removeSource']['params'], callback: ActorMessages['removeSource']['callback']) {
@@ -374,7 +364,8 @@ export default class MapWorker {
374364
return layerIndex;
375365
}
376366

377-
getWorkerSource(mapId: number, type: string, source: string, scope: string): WorkerSource {
367+
getWorkerSource(mapId: number, params: WorkerSourceRequest): WorkerSource {
368+
const {type, source, scope} = params;
378369
const workerSources = this.workerSources;
379370

380371
if (!workerSources[mapId])

src/source/worker_source.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ import type {StyleModelMap} from '../style/style_mode';
2727
import type {IndoorTileOptions} from '../style/indoor_data.js';
2828
import type {Cancelable} from '../types/cancelable';
2929

30+
/**
31+
* Source types that can instantiate a {@link WorkerSource} in {@link MapWorker}.
32+
*/
33+
export type WorkerSourceType =
34+
| 'vector'
35+
| 'geojson'
36+
| 'raster-dem'
37+
| 'raster-array'
38+
| 'batched-model';
39+
3040
/**
3141
* The parameters passed to the {@link MapWorker#getWorkerSource}.
3242
*/

src/types/worker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type MapWorker from '../source/worker';
22
import type {RtlTextPlugin} from '../source/rtl_text_plugin';
3-
import type {WorkerSourceConstructor, WorkerSource} from '../source/worker_source';
3+
import type {WorkerSourceConstructor, WorkerSource, WorkerSourceRequest} from '../source/worker_source';
44

55
// Extends Worker interface in a browser environment
66
declare global {
77
interface Worker {
88
worker: MapWorker;
99
registerWorkerSource?: (name: string, WorkerSource: WorkerSourceConstructor) => void;
10-
getWorkerSource?: (mapId: number, type: string, source: string, scope: string) => WorkerSource;
10+
getWorkerSource?: (mapId: number, params: WorkerSourceRequest) => WorkerSource;
1111
registerRTLTextPlugin?: (rtlTextPlugin?: RtlTextPlugin) => void;
1212

1313
importScripts: (...urls: string[]) => void;

src/util/actor.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ class Actor {
186186
// task.type == sourcetype.method
187187
const keys = task.type.split('.');
188188
const {source, scope} = params as {source: string; scope: string};
189-
const workerSource = this.parent.getWorkerSource(task.sourceMapId, keys[0], source, scope);
189+
const workerSource = this.parent.getWorkerSource(task.sourceMapId, {type: keys[0], source, scope, uid: 0});
190190
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
191191
workerSource[keys[1]](params, done);
192192
} else {

test/unit/source/worker.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ test('worker sources should be scoped', () => {
6767

6868
_self.registerWorkerSource('sourceType', function () {} as unknown as WorkerSourceConstructor);
6969

70-
const a = worker.getWorkerSource(999, 'sourceType', 'sourceId', 'scope1');
71-
const b = worker.getWorkerSource(999, 'sourceType', 'sourceId', 'scope2');
70+
const a = worker.getWorkerSource(999, {type: 'sourceType', source: 'sourceId', scope: 'scope1', uid: 0});
71+
const b = worker.getWorkerSource(999, {type: 'sourceType', source: 'sourceId', scope: 'scope2', uid: 0});
7272

7373
expect(a).not.toBe(b);
7474
});

0 commit comments

Comments
 (0)