Skip to content

Commit 6e51fe7

Browse files
stepankuzmingithub-actions[bot]
authored andcommitted
Refactor worker source constructor
GitOrigin-RevId: 9395a79907afc6713fae980287e0d89b5f821aa0
1 parent b767373 commit 6e51fe7

12 files changed

Lines changed: 67 additions & 89 deletions

3d-style/source/tiled_3d_model_worker_source.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ import type Actor from '../../src/util/actor';
1313
import type StyleLayerIndex from '../../src/style/style_layer_index';
1414
import type {
1515
WorkerSource,
16+
WorkerSourceOptions,
1617
WorkerSourceTileRequest,
1718
WorkerSourceTiled3dModelRequest,
1819
WorkerSourceVectorTileCallback,
1920
WorkerSourceVectorTileResult
2021
} from '../../src/source/worker_source';
21-
import type {LoadVectorData} from '../../src/source/load_vector_tile';
2222
import type Projection from '../../src/geo/projection/projection';
2323
import type ModelStyleLayer from '../style/style_layer/model_style_layer';
2424
import type {ImageId} from '../../src/style-spec/expression/types/image_id';
@@ -120,7 +120,7 @@ class Tiled3dModelWorkerSource implements WorkerSource {
120120
brightness?: number;
121121
worldview: string | undefined;
122122

123-
constructor(actor: Actor, layerIndex: StyleLayerIndex, availableImages: ImageId[], availableModels: StyleModelMap, isSpriteLoaded: boolean, loadVectorData?: LoadVectorData, brightness?: number, worldview?: string) {
123+
constructor({actor, layerIndex, availableImages, availableModels, brightness, worldview}: WorkerSourceOptions) {
124124
this.actor = actor;
125125
this.layerIndex = layerIndex;
126126
this.availableImages = availableImages;

src/source/geojson_worker_source.ts

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,15 @@ import VectorTileWorkerSource from './vector_tile_worker_source';
1010
import {createExpression} from '../style-spec/expression/index';
1111

1212
import type {
13+
WorkerSourceOptions,
1314
WorkerSourceVectorTileRequest,
1415
WorkerSourceVectorTileCallback,
1516
} from '../source/worker_source';
16-
import type Actor from '../util/actor';
17-
import type StyleLayerIndex from '../style/style_layer_index';
1817
import type {Feature} from './geojson_wrapper';
1918
import type {Feature as ExpressionFeature} from '../style-spec/expression/index';
2019
import type {LoadVectorDataCallback} from './load_vector_tile';
2120
import type {RequestParameters, ResponseCallback} from '../util/ajax';
2221
import type {Callback} from '../types/callback';
23-
import type {ImageId} from '../style-spec/expression/types/image_id';
24-
import type {StyleModelMap} from '../style/style_mode';
2522

2623
export type GeoJSONWorkerOptions = {
2724
source: string;
@@ -49,8 +46,6 @@ type ResourceTiming = Record<string, PerformanceResourceTiming[]>;
4946

5047
export type LoadGeoJSONResult = FeatureCollectionOrFeature & {resourceTiming?: ResourceTiming};
5148

52-
export type LoadGeoJSON = (params: LoadGeoJSONParameters, callback: ResponseCallback<LoadGeoJSONResult>) => void;
53-
5449
export interface GeoJSONIndex {
5550
// eslint-disable-next-line @typescript-eslint/no-explicit-any
5651
getTile: (z: number, x: number, y: number) => any;
@@ -111,9 +106,7 @@ function loadGeoJSONTile(this: GeoJSONWorkerSource, params: WorkerSourceVectorTi
111106
* The {@link WorkerSource} implementation that supports {@link GeoJSONSource}.
112107
* This class is designed to be easily reused to support custom source types
113108
* for data formats that can be parsed/converted into an in-memory GeoJSON
114-
* representation. To do so, create it with
115-
* `new GeoJSONWorkerSource(actor, layerIndex, customLoadGeoJSONFunction)`.
116-
* For a full example, see [mapbox-gl-topojson](https://github.com/developmentseed/mapbox-gl-topojson).
109+
* representation.
117110
*
118111
* @private
119112
*/
@@ -122,16 +115,10 @@ class GeoJSONWorkerSource extends VectorTileWorkerSource {
122115
_dynamicIndex: GeoJSONRT;
123116

124117
/**
125-
* @param [loadGeoJSON] Optional method for custom loading/parsing of
126-
* GeoJSON based on parameters passed from the main-thread Source.
127-
* See {@link GeoJSONWorkerSource#loadGeoJSON}.
128118
* @private
129119
*/
130-
constructor(actor: Actor, layerIndex: StyleLayerIndex, availableImages: ImageId[], availableModels: StyleModelMap, isSpriteLoaded: boolean, loadGeoJSON?: LoadGeoJSON | null, brightness?: number | null) {
131-
super(actor, layerIndex, availableImages, availableModels, isSpriteLoaded, loadGeoJSONTile, brightness);
132-
if (loadGeoJSON) {
133-
this.loadGeoJSON = loadGeoJSON;
134-
}
120+
constructor({actor, layerIndex, availableImages, availableModels, isSpriteLoaded, brightness}: WorkerSourceOptions) {
121+
super({actor, layerIndex, availableImages, availableModels, isSpriteLoaded, loadTileData: loadGeoJSONTile, brightness});
135122
this._dynamicIndex = new GeoJSONRT();
136123
}
137124

@@ -241,15 +228,11 @@ class GeoJSONWorkerSource extends VectorTileWorkerSource {
241228
}
242229

243230
/**
244-
* Fetch and parse GeoJSON according to the given params. Calls `callback`
245-
* with `(err, data)`, where `data` is a parsed GeoJSON object.
231+
* Fetch and parse GeoJSON according to the given params.
246232
*
247233
* GeoJSON is loaded and parsed from `params.url` if it exists, or else
248234
* expected as a literal (string or object) `params.data`.
249235
*
250-
* @param params
251-
* @param [params.url] A URL to the remote GeoJSON data.
252-
* @param [params.data] Literal GeoJSON data. Must be provided if `params.url` is not.
253236
* @private
254237
*/
255238
loadGeoJSON(params: LoadGeoJSONParameters, callback: ResponseCallback<FeatureCollectionOrFeature>): void {
@@ -263,8 +246,7 @@ class GeoJSONWorkerSource extends VectorTileWorkerSource {
263246
// delay loading by one tick to hopefully let GC clean up the previous index (if present)
264247
setTimeout(() => {
265248
try {
266-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
267-
return callback(null, JSON.parse(params.data));
249+
return callback(null, JSON.parse(params.data) as FeatureCollectionOrFeature);
268250
} catch (e) {
269251
return callback(new Error(`Input data given to '${params.source}' is not a valid GeoJSON object.`));
270252
}

src/source/load_vector_tile.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Protobuf from 'pbf';
33
import {getArrayBuffer} from '../util/ajax';
44

55
import type {Callback} from '../types/callback';
6+
import type {Cancelable} from '../types/cancelable';
67
import type {WorkerSourceVectorTileRequest} from './worker_source';
78
import type {default as Scheduler, TaskMetadata} from '../util/scheduler';
89

@@ -20,14 +21,13 @@ export type LoadVectorTileResult = {
2021
*/
2122
export type LoadVectorDataCallback = Callback<LoadVectorTileResult | null | undefined>;
2223

23-
export type LoadVectorData = (params: WorkerSourceVectorTileRequest, callback: LoadVectorDataCallback) => AbortVectorDataRequest | undefined;
24+
export type LoadVectorData = (params: WorkerSourceVectorTileRequest, callback: LoadVectorDataCallback) => Cancelable['cancel'];
2425

25-
type VectorDataRequest = (callback: LoadVectorDataCallback) => AbortVectorDataRequest;
26-
type AbortVectorDataRequest = () => void;
26+
type VectorDataRequest = (callback: LoadVectorDataCallback) => Cancelable['cancel'];
2727

2828
type DedupedRequestEntry = {
2929
result?: [Error | null, LoadVectorTileResult];
30-
cancel?: AbortVectorDataRequest;
30+
cancel?: Cancelable['cancel'];
3131
callbacks?: LoadVectorDataCallback[];
3232
};
3333

@@ -40,7 +40,7 @@ export class DedupedRequest {
4040
this.scheduler = scheduler;
4141
}
4242

43-
request(key: string, metadata: TaskMetadata, request: VectorDataRequest, callback: LoadVectorDataCallback): AbortVectorDataRequest {
43+
request(key: string, metadata: TaskMetadata, request: VectorDataRequest, callback: LoadVectorDataCallback): Cancelable['cancel'] {
4444
const entry = this.entries[key] = this.entries[key] || {callbacks: []};
4545

4646
if (entry.result) {
@@ -92,7 +92,7 @@ export function loadVectorTile(
9292
params: WorkerSourceVectorTileRequest,
9393
callback: LoadVectorDataCallback,
9494
skipParse?: boolean,
95-
): AbortVectorDataRequest {
95+
): Cancelable['cancel'] {
9696
const key = JSON.stringify(params.request);
9797

9898
const makeRequest: VectorDataRequest = (callback: LoadVectorDataCallback) => {

src/source/raster_array_tile_worker_source.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {ActorMessages} from '../util/actor_messages';
99
import type {OverscaledTileID} from './tile_id';
1010
import type {
1111
WorkerSource,
12+
WorkerSourceOptions,
1213
WorkerSourceTileRequest,
1314
WorkerSourceRasterArrayTileRequest,
1415
WorkerSourceRasterArrayTileCallback,
@@ -79,7 +80,7 @@ class RasterArrayTileWorkerSource implements WorkerSource {
7980
loading: Record<number, RasterArrayWorkerTile>;
8081
loaded: Record<number, RasterArrayWorkerTile>;
8182

82-
constructor(actor: Actor) {
83+
constructor({actor}: WorkerSourceOptions) {
8384
this.actor = actor;
8485
this.loading = {};
8586
this.loaded = {};

src/source/raster_dem_tile_worker_source.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import DEMData from '../data/dem_data';
22

3-
import type Actor from '../util/actor';
43
import type {
54
WorkerSource,
65
WorkerSourceTileRequest,
@@ -10,7 +9,6 @@ import type {
109
} from './worker_source';
1110

1211
class RasterDEMTileWorkerSource implements WorkerSource {
13-
actor: Actor;
1412
offscreenCanvas: OffscreenCanvas;
1513
offscreenCanvasContext: OffscreenCanvasRenderingContext2D;
1614

src/source/vector_tile_worker_source.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {getExpiryDataFromHeaders} from '../util/util';
88

99
import type {
1010
WorkerSource,
11+
WorkerSourceOptions,
1112
WorkerSourceTileRequest,
1213
WorkerSourceVectorTileRequest,
1314
WorkerSourceVectorTileResult,
@@ -25,8 +26,7 @@ import type {StyleModelMap} from '../style/style_mode';
2526
* The {@link WorkerSource} implementation that supports {@link VectorTileSource}.
2627
* This class is designed to be easily reused to support custom source types
2728
* for data formats that can be parsed/converted into an in-memory VectorTile
28-
* representation. To do so, create it with
29-
* `new VectorTileWorkerSource(actor, styleLayers, customLoadVectorDataFunction)`.
29+
* representation.
3030
*
3131
* @private
3232
*/
@@ -41,22 +41,18 @@ class VectorTileWorkerSource extends Evented implements WorkerSource {
4141
deduped: DedupedRequest;
4242
isSpriteLoaded: boolean;
4343
scheduler?: Scheduler | null;
44-
brightness?: number | null;
44+
brightness?: number;
4545

4646
/**
47-
* @param [loadVectorData] Optional method for custom loading of a VectorTile
48-
* object based on parameters passed from the main-thread Source. See
49-
* {@link VectorTileWorkerSource#loadTile}. The default implementation simply
50-
* loads the pbf at `params.url`.
5147
* @private
5248
*/
53-
constructor(actor: Actor, layerIndex: StyleLayerIndex, availableImages: ImageId[], availableModels: StyleModelMap, isSpriteLoaded: boolean, loadVectorData?: LoadVectorData | null, brightness?: number | null) {
49+
constructor({actor, layerIndex, availableImages, availableModels, isSpriteLoaded, loadTileData, brightness}: WorkerSourceOptions) {
5450
super();
5551
this.actor = actor;
5652
this.layerIndex = layerIndex;
5753
this.availableImages = availableImages;
5854
this.availableModels = availableModels;
59-
this.loadVectorData = loadVectorData || loadVectorTile;
55+
this.loadVectorData = (loadTileData as LoadVectorData) || loadVectorTile;
6056
this.loading = {};
6157
this.loaded = {};
6258
this.deduped = new DedupedRequest(actor.scheduler);

src/source/worker.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export default class MapWorker {
6161
referrer: string | null | undefined;
6262
dracoUrl: string | null | undefined;
6363
meshoptUrl: string | null | undefined;
64-
brightness: number | null | undefined;
64+
brightness: number | undefined;
6565
imageRasterizer: ImageRasterizer;
6666
worldview: string | undefined;
6767
rtlPluginParsingListeners: Array<Callback<boolean>>;
@@ -397,17 +397,21 @@ export default class MapWorker {
397397
scheduler: this.actor.scheduler
398398
} as Actor;
399399

400-
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call
401-
workerSources[mapId][scope][type][source] = new this.workerSourceTypes[type](
400+
const WorkerSourceConstructor = this.workerSourceTypes[type as WorkerSourceType];
401+
if (!WorkerSourceConstructor) {
402+
throw new Error(`Unknown worker source type "${type}".`);
403+
}
404+
405+
workerSources[mapId][scope][type][source] = new WorkerSourceConstructor({
402406
actor,
403-
this.getLayerIndex(mapId, scope),
404-
this.getAvailableImages(mapId, scope),
405-
this.getAvailableModels(mapId, scope),
406-
this.isSpriteLoaded[mapId][scope],
407-
undefined,
408-
this.brightness,
409-
this.worldview
410-
);
407+
layerIndex: this.getLayerIndex(mapId, scope),
408+
availableImages: this.getAvailableImages(mapId, scope),
409+
availableModels: this.getAvailableModels(mapId, scope),
410+
isSpriteLoaded: this.isSpriteLoaded[mapId][scope],
411+
loadTileData: undefined,
412+
brightness: this.brightness,
413+
worldview: this.worldview
414+
});
411415
}
412416

413417
return workerSources[mapId][scope][type][source];

src/source/worker_source.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import type {ImageId} from '../style-spec/expression/types/image_id';
2525
import type {StringifiedImageVariant} from '../style-spec/expression/types/image_variant';
2626
import type {StyleModelMap} from '../style/style_mode';
2727
import type {IndoorTileOptions} from '../style/indoor_data.js';
28+
import type {Cancelable} from '../types/cancelable';
2829

2930
/**
3031
* The parameters passed to the {@link MapWorker#getWorkerSource}.
@@ -162,14 +163,6 @@ export type WorkerSourceImageRaserizeCallback = Callback<RasterizedImageMap>;
162163
*
163164
* @see {@link Map#addSourceType}
164165
* @private
165-
*
166-
* @class WorkerSource
167-
* @param actor
168-
* @param layerIndex
169-
* @param availableImages
170-
* @param isSpriteLoaded
171-
* @param loadData
172-
* @param brightness
173166
*/
174167
export interface WorkerSource {
175168
availableImages?: ImageId[];
@@ -202,15 +195,19 @@ export interface WorkerSource {
202195
removeSource?: (params: {source: string}, callback: Callback<void>) => void;
203196
}
204197

198+
export type WorkerSourceLoadTileData = (params: WorkerSourceTileRequest, callback: Callback<unknown>) => Cancelable['cancel'];
199+
200+
export type WorkerSourceOptions = {
201+
actor: Actor;
202+
layerIndex: StyleLayerIndex;
203+
availableImages: ImageId[];
204+
availableModels: StyleModelMap;
205+
isSpriteLoaded: boolean;
206+
loadTileData?: WorkerSourceLoadTileData;
207+
brightness?: number;
208+
worldview?: string;
209+
};
210+
205211
export interface WorkerSourceConstructor {
206-
new(
207-
actor?: Actor,
208-
layerIndex?: StyleLayerIndex,
209-
availableImages?: ImageId[],
210-
availableModels?: StyleModelMap,
211-
isSpriteLoaded?: boolean,
212-
loadData?: (params: {source: string; scope: string}, callback: Callback<unknown>) => () => void | undefined,
213-
brightness?: number,
214-
worldview?: string
215-
): WorkerSource;
212+
new(options: WorkerSourceOptions): WorkerSource;
216213
}

test/unit/source/geojson_worker_source.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe('reloadTile', () => {
1919
}
2020
];
2121
const layerIndex = new StyleLayerIndex(layers);
22-
const source = new GeoJSONWorkerSource(actor, layerIndex, [], [], true);
22+
const source = new GeoJSONWorkerSource({actor, layerIndex, availableImages: [], availableModels: [], isSpriteLoaded: true});
2323
const originalLoadVectorData = source.loadVectorData;
2424
let loadVectorCallCount = 0;
2525
source.loadVectorData = function (params, callback) {
@@ -131,7 +131,8 @@ describe('resourceTiming', () => {
131131
vi.spyOn(perf, 'getEntriesByName').mockImplementation(() => { return [exampleResourceTiming]; });
132132

133133
const layerIndex = new StyleLayerIndex(layers);
134-
const source = new GeoJSONWorkerSource(actor, layerIndex, [], [], true, (params, callback) => { return callback(null, geoJson); });
134+
const source = new GeoJSONWorkerSource({actor, layerIndex, availableImages: [], availableModels: [], isSpriteLoaded: true});
135+
source.loadGeoJSON = (params, callback) => { return callback(null, geoJson); };
135136

136137
source.loadData({source: 'testSource', request: {url: 'http://localhost/nonexistent', collectResourceTiming: true}}, (err, result) => {
137138
expect(err).toEqual(null);
@@ -141,7 +142,7 @@ describe('resourceTiming', () => {
141142

142143
test('loadData - data', () => {
143144
const layerIndex = new StyleLayerIndex(layers);
144-
const source = new GeoJSONWorkerSource(actor, layerIndex, [], [], true);
145+
const source = new GeoJSONWorkerSource({actor, layerIndex, availableImages: [], availableModels: [], isSpriteLoaded: true});
145146

146147
source.loadData({source: 'testSource', data: JSON.stringify(geoJson)}, (err, result) => {
147148
expect(err).toEqual(null);

test/unit/source/raster_dem_tile_worker_source.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
// @ts-nocheck
33
import {describe, test, expect} from '../../util/vitest';
44
import RasterDEMTileWorkerSource from '../../../src/source/raster_dem_tile_worker_source';
5-
import StyleLayerIndex from '../../../src/style/style_layer_index';
65
import DEMData from '../../../src/data/dem_data';
76

87
describe('loadTile', () => {
98
test('loads DEM tile', () => {
10-
const source = new RasterDEMTileWorkerSource(null, new StyleLayerIndex());
9+
const source = new RasterDEMTileWorkerSource();
1110

1211
source.loadTile({
1312
source: 'source',

0 commit comments

Comments
 (0)