@mapwhit/tilerenderer 0.47.1 → 0.47.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/build/min/package.json +1 -1
  2. package/package.json +1 -2
  3. package/src/data/array_types.js +1 -1
  4. package/src/data/bucket/circle_bucket.js +1 -1
  5. package/src/data/bucket/fill_bucket.js +1 -1
  6. package/src/data/bucket/fill_extrusion_bucket.js +1 -1
  7. package/src/data/bucket/heatmap_bucket.js +1 -1
  8. package/src/data/bucket/line_bucket.js +1 -1
  9. package/src/data/bucket/symbol_bucket.js +1 -1
  10. package/src/data/dem_data.js +1 -1
  11. package/src/data/feature_index.js +43 -82
  12. package/src/data/program_configuration.js +1 -1
  13. package/src/data/segment.js +2 -2
  14. package/src/gl/color_mode.js +6 -6
  15. package/src/index.js +2 -0
  16. package/src/render/glyph_atlas.js +1 -1
  17. package/src/render/glyph_manager.js +43 -48
  18. package/src/render/image_atlas.js +1 -1
  19. package/src/render/image_manager.js +9 -37
  20. package/src/source/geojson_source.js +49 -93
  21. package/src/source/geojson_worker_source.js +33 -134
  22. package/src/source/image_source.js +9 -14
  23. package/src/source/load_tilejson.js +27 -34
  24. package/src/source/raster_dem_tile_source.js +27 -40
  25. package/src/source/raster_tile_source.js +53 -62
  26. package/src/source/rtl_text_plugin.js +2 -1
  27. package/src/source/source_cache.js +22 -20
  28. package/src/source/source_state.js +17 -26
  29. package/src/source/tile_id.js +1 -1
  30. package/src/source/vector_tile_source.js +56 -73
  31. package/src/source/vector_tile_worker_source.js +20 -85
  32. package/src/source/worker.js +38 -95
  33. package/src/source/worker_tile.js +39 -84
  34. package/src/style/load_sprite.js +14 -17
  35. package/src/style/properties.js +1 -1
  36. package/src/style/style.js +22 -22
  37. package/src/style/style_layer_index.js +17 -23
  38. package/src/style-spec/reference/v8.json +2 -2
  39. package/src/symbol/anchor.js +1 -1
  40. package/src/symbol/collision_index.js +23 -16
  41. package/src/symbol/grid_index.js +176 -182
  42. package/src/symbol/mergelines.js +48 -48
  43. package/src/symbol/opacity_state.js +1 -1
  44. package/src/ui/camera.js +82 -85
  45. package/src/ui/map.js +5 -32
  46. package/src/util/actor.js +46 -42
  47. package/src/util/browser.js +6 -0
  48. package/src/util/dictionary_coder.js +13 -21
  49. package/src/util/dispatcher.js +14 -17
  50. package/src/util/image.js +1 -1
  51. package/src/util/loader/image.js +11 -11
  52. package/src/util/polyfill.js +16 -0
  53. package/src/util/task_queue.js +39 -43
  54. package/src/util/transfer_registry.js +167 -0
  55. package/src/util/web_worker_transfer.js +5 -190
  56. package/src/source/raster_dem_tile_worker_source.js +0 -26
@@ -24,22 +24,21 @@ class RasterTileSource extends Evented {
24
24
  Object.assign(this, pick(options, ['url', 'scheme', 'tileSize']));
25
25
  }
26
26
 
27
- load() {
27
+ async load() {
28
28
  this.fire(new Event('dataloading', { dataType: 'source' }));
29
- loadTileJSON(this._options, (err, tileJSON) => {
30
- if (err) {
31
- this.fire(new ErrorEvent(err));
32
- } else if (tileJSON) {
33
- Object.assign(this, tileJSON);
34
- if (tileJSON.bounds) this.tileBounds = new TileBounds(tileJSON.bounds, this.minzoom, this.maxzoom);
35
-
36
- // `content` is included here to prevent a race condition where `Style#_updateSources` is called
37
- // before the TileJSON arrives. this makes sure the tiles needed are loaded once TileJSON arrives
38
- // ref: https://github.com/mapbox/mapbox-gl-js/pull/4347#discussion_r104418088
39
- this.fire(new Event('data', { dataType: 'source', sourceDataType: 'metadata' }));
40
- this.fire(new Event('data', { dataType: 'source', sourceDataType: 'content' }));
41
- }
42
- });
29
+ try {
30
+ const tileJSON = await loadTileJSON(this._options);
31
+ Object.assign(this, tileJSON);
32
+ if (tileJSON.bounds) this.tileBounds = new TileBounds(tileJSON.bounds, this.minzoom, this.maxzoom);
33
+
34
+ // `content` is included here to prevent a race condition where `Style#_updateSources` is called
35
+ // before the TileJSON arrives. this makes sure the tiles needed are loaded once TileJSON arrives
36
+ // ref: https://github.com/mapbox/mapbox-gl-js/pull/4347#discussion_r104418088
37
+ this.fire(new Event('data', { dataType: 'source', sourceDataType: 'metadata' }));
38
+ this.fire(new Event('data', { dataType: 'source', sourceDataType: 'content' }));
39
+ } catch (err) {
40
+ this.fire(new ErrorEvent(err));
41
+ }
43
42
  }
44
43
 
45
44
  onAdd(map) {
@@ -55,67 +54,59 @@ class RasterTileSource extends Evented {
55
54
  return !this.tileBounds || this.tileBounds.contains(tileID.canonical);
56
55
  }
57
56
 
58
- loadTile(tile, callback) {
59
- const done = (err, img) => {
60
- delete tile.request;
57
+ async loadTile(tile) {
58
+ try {
59
+ tile.abortController = new window.AbortController();
60
+ const data = await this.tiles(tile.tileID.canonical, tile.abortController).catch(() => {});
61
+ if (!data) {
62
+ const err = new Error('Tile could not be loaded');
63
+ err.status = 404; // will try to use the parent/child tile
64
+ throw err;
65
+ }
66
+ const img = await loadImage(data);
67
+ if (!img) {
68
+ return;
69
+ }
61
70
 
62
- if (tile.aborted) {
63
- tile.state = 'unloaded';
64
- callback(null);
65
- } else if (err) {
66
- tile.state = 'errored';
67
- callback(err);
68
- } else if (img) {
69
- const context = this.map.painter.context;
70
- const gl = context.gl;
71
- tile.texture = this.map.painter.getTileTexture(img.width);
72
- if (tile.texture) {
73
- tile.texture.update(img, { useMipmap: true });
74
- } else {
75
- tile.texture = new Texture(context, img, gl.RGBA, { useMipmap: true });
76
- tile.texture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE, gl.LINEAR_MIPMAP_NEAREST);
77
-
78
- if (context.extTextureFilterAnisotropic) {
79
- gl.texParameterf(
80
- gl.TEXTURE_2D,
81
- context.extTextureFilterAnisotropic.TEXTURE_MAX_ANISOTROPY_EXT,
82
- context.extTextureFilterAnisotropicMax
83
- );
84
- }
71
+ tile.texture = this.map.painter.getTileTexture(img.width);
72
+ if (tile.texture) {
73
+ tile.texture.update(img, { useMipmap: true });
74
+ } else {
75
+ const { context } = this.map.painter;
76
+ const { gl } = context;
77
+ tile.texture = new Texture(context, img, gl.RGBA, { useMipmap: true });
78
+ tile.texture.bind(gl.LINEAR, gl.CLAMP_TO_EDGE, gl.LINEAR_MIPMAP_NEAREST);
79
+
80
+ if (context.extTextureFilterAnisotropic) {
81
+ gl.texParameterf(
82
+ gl.TEXTURE_2D,
83
+ context.extTextureFilterAnisotropic.TEXTURE_MAX_ANISOTROPY_EXT,
84
+ context.extTextureFilterAnisotropicMax
85
+ );
85
86
  }
87
+ }
86
88
 
87
- tile.state = 'loaded';
88
-
89
- callback(null);
89
+ tile.state = 'loaded';
90
+ } catch (err) {
91
+ if (tile.aborted) {
92
+ tile.state = 'unloaded';
93
+ return;
90
94
  }
91
- };
92
-
93
- tile.abortController = new window.AbortController();
94
- this.tiles(tile.tileID.canonical, tile.abortController)
95
- .catch(() => {})
96
- .then(data => {
97
- if (!data) {
98
- const err = new Error('Tile could not be loaded');
99
- err.status = 404; // will try to use the parent/child tile
100
- return done(err);
101
- }
102
- tile.request = loadImage(data, done);
103
- });
95
+ tile.state = 'errored';
96
+ throw err;
97
+ }
104
98
  }
105
99
 
106
- abortTile(tile, callback) {
100
+ abortTile(tile) {
107
101
  if (tile.abortController) {
108
102
  tile.aborted = true;
109
103
  tile.abortController.abort();
110
104
  delete tile.abortController;
111
- delete tile.request;
112
105
  }
113
- callback();
114
106
  }
115
107
 
116
- unloadTile(tile, callback) {
108
+ unloadTile(tile) {
117
109
  if (tile.texture) this.map.painter.saveTileTexture(tile.texture);
118
- callback();
119
110
  }
120
111
 
121
112
  hasTransition() {
@@ -1,4 +1,5 @@
1
1
  const { Event, Evented } = require('../util/evented');
2
+ const browser = require('../util/browser');
2
3
 
3
4
  let pluginRequested = false;
4
5
  let pluginURL = null;
@@ -27,7 +28,7 @@ function setRTLTextPlugin(url, callback) {
27
28
  throw new Error('setRTLTextPlugin cannot be called multiple times.');
28
29
  }
29
30
  pluginRequested = true;
30
- pluginURL = url;
31
+ pluginURL = browser.resolveURL(url);
31
32
  _completionCallback = error => {
32
33
  if (error) {
33
34
  // Clear loaded state to allow retries
@@ -110,16 +110,16 @@ class SourceCache extends Evented {
110
110
  if (this.transform) this.update(this.transform);
111
111
  }
112
112
 
113
- _loadTile(tile, callback) {
114
- return this._source.loadTile(tile, callback);
113
+ _loadTile(tile) {
114
+ return this._source.loadTile(tile);
115
115
  }
116
116
 
117
117
  _unloadTile(tile) {
118
- if (this._source.unloadTile) return this._source.unloadTile(tile, () => {});
118
+ return this._source.unloadTile?.(tile);
119
119
  }
120
120
 
121
121
  _abortTile(tile) {
122
- if (this._source.abortTile) return this._source.abortTile(tile, () => {});
122
+ return this._source.abortTile?.(tile);
123
123
  }
124
124
 
125
125
  serialize() {
@@ -127,9 +127,7 @@ class SourceCache extends Evented {
127
127
  }
128
128
 
129
129
  prepare(context) {
130
- if (this._source.prepare) {
131
- this._source.prepare();
132
- }
130
+ this._source.prepare?.();
133
131
 
134
132
  this._state.coalesceChanges(this._tiles, this.map ? this.map.painter : null);
135
133
  for (const i in this._tiles) {
@@ -202,21 +200,22 @@ class SourceCache extends Evented {
202
200
  if (tile.state !== 'loading') {
203
201
  tile.state = state;
204
202
  }
205
-
206
- this._loadTile(tile, this._tileLoaded.bind(this, tile, id, state));
203
+ this._loadTile(tile).then(
204
+ () => this._tileLoaded(tile),
205
+ err => this._tileLoadError(tile, err)
206
+ );
207
207
  }
208
208
 
209
- _tileLoaded(tile, id, previousState, err) {
210
- if (err) {
211
- tile.state = 'errored';
212
- // ignore do nothing strategy
213
- if (err.doNothing) return;
214
- if (err.status !== 404) this._source.fire(new ErrorEvent(err, { tile }));
215
- // continue to try loading parent/children tiles if a tile doesn't exist (404)
216
- else this.update(this.transform);
217
- return;
218
- }
209
+ _tileLoadError(tile, err) {
210
+ tile.state = 'errored';
211
+ // ignore do nothing strategy
212
+ if (err.doNothing) return;
213
+ if (err.status !== 404) this._source.fire(new ErrorEvent(err, { tile }));
214
+ // continue to try loading parent/children tiles if a tile doesn't exist (404)
215
+ else this.update(this.transform);
216
+ }
219
217
 
218
+ _tileLoaded(tile, err) {
220
219
  tile.timeAdded = browser.now();
221
220
  if (this.getSource().type === 'raster-dem' && tile.dem) this._backfillDEM(tile);
222
221
  this._state.initializeTileState(tile, this.map ? this.map.painter : null);
@@ -610,7 +609,10 @@ class SourceCache extends Evented {
610
609
  const cached = Boolean(tile);
611
610
  if (!cached) {
612
611
  tile = new Tile(tileID, this._source.tileSize * tileID.overscaleFactor());
613
- this._loadTile(tile, this._tileLoaded.bind(this, tile, tileID.key, tile.state));
612
+ this._loadTile(tile).then(
613
+ () => this._tileLoaded(tile),
614
+ err => this._tileLoadError(tile, err)
615
+ );
614
616
  }
615
617
 
616
618
  // Impossible, but silence flow.
@@ -5,50 +5,41 @@
5
5
  * @private
6
6
  */
7
7
  class SourceFeatureState {
8
- constructor() {
9
- this.state = {};
10
- this.stateChanges = {};
11
- }
8
+ #state = {};
9
+ #stateChanges = {};
12
10
 
13
11
  updateState(sourceLayer, feature, state) {
14
- feature = String(feature);
15
- this.stateChanges[sourceLayer] = this.stateChanges[sourceLayer] || {};
16
- this.stateChanges[sourceLayer][feature] = this.stateChanges[sourceLayer][feature] || {};
17
- Object.assign(this.stateChanges[sourceLayer][feature], state);
12
+ const changes = (this.#stateChanges[sourceLayer] ??= {});
13
+ const featureState = (changes[feature] ??= {});
14
+ Object.assign(featureState, state);
18
15
  }
19
16
 
20
17
  getState(sourceLayer, feature) {
21
- feature = String(feature);
22
- const base = this.state[sourceLayer] || {};
23
- const changes = this.stateChanges[sourceLayer] || {};
24
- return Object.assign({}, base[feature], changes[feature]);
18
+ const base = this.#state[sourceLayer];
19
+ const changes = this.#stateChanges[sourceLayer];
20
+ return Object.assign({}, base?.[feature], changes?.[feature]);
25
21
  }
26
22
 
27
23
  initializeTileState(tile, painter) {
28
- tile.setFeatureState(this.state, painter);
24
+ tile.setFeatureState(this.#state, painter);
29
25
  }
30
26
 
31
27
  coalesceChanges(tiles, painter) {
32
28
  const changes = {};
33
- for (const sourceLayer in this.stateChanges) {
34
- this.state[sourceLayer] = this.state[sourceLayer] || {};
29
+ for (const sourceLayer in this.#stateChanges) {
30
+ this.#state[sourceLayer] ??= {};
35
31
  const layerStates = {};
36
- for (const id in this.stateChanges[sourceLayer]) {
37
- if (!this.state[sourceLayer][id]) {
38
- this.state[sourceLayer][id] = {};
39
- }
40
- Object.assign(this.state[sourceLayer][id], this.stateChanges[sourceLayer][id]);
41
- layerStates[id] = this.state[sourceLayer][id];
32
+ for (const id in this.#stateChanges[sourceLayer]) {
33
+ this.#state[sourceLayer][id] ??= {};
34
+ Object.assign(this.#state[sourceLayer][id], this.#stateChanges[sourceLayer][id]);
35
+ layerStates[id] = this.#state[sourceLayer][id];
42
36
  }
43
37
  changes[sourceLayer] = layerStates;
44
38
  }
45
- this.stateChanges = {};
39
+ this.#stateChanges = {};
46
40
  if (Object.keys(changes).length === 0) return;
47
41
 
48
- for (const id in tiles) {
49
- const tile = tiles[id];
50
- tile.setFeatureState(changes, painter);
51
- }
42
+ Object.values(tiles).forEach(tile => tile.setFeatureState(changes, painter));
52
43
  }
53
44
  }
54
45
 
@@ -1,7 +1,7 @@
1
1
  const { getTileBBox } = require('@mapbox/whoots-js');
2
2
 
3
3
  const assert = require('assert');
4
- const { register } = require('../util/web_worker_transfer');
4
+ const { register } = require('../util/transfer_registry');
5
5
  const Coordinate = require('../geo/coordinate');
6
6
 
7
7
  class CanonicalTileID {
@@ -1,4 +1,3 @@
1
- const config = require('../util/config');
2
1
  const { Event, ErrorEvent, Evented } = require('../util/evented');
3
2
  const { pick } = require('../util/object');
4
3
  const loadTileJSON = require('./load_tilejson');
@@ -29,29 +28,25 @@ class VectorTileSource extends Evented {
29
28
  throw new Error('vector tile sources must have a tileSize of 512');
30
29
  }
31
30
 
32
- this.updateWorkerConfig(config);
33
- config.on('change', c => this.updateWorkerConfig(c));
34
-
35
31
  this.setEventedParent(eventedParent);
36
32
  }
37
33
 
38
- load() {
34
+ async load() {
39
35
  this.fire(new Event('dataloading', { dataType: 'source' }));
40
36
 
41
- loadTileJSON(this._options, (err, tileJSON) => {
42
- if (err) {
43
- this.fire(new ErrorEvent(err));
44
- } else if (tileJSON) {
45
- Object.assign(this, tileJSON);
46
- if (tileJSON.bounds) this.tileBounds = new TileBounds(tileJSON.bounds, this.minzoom, this.maxzoom);
47
-
48
- // `content` is included here to prevent a race condition where `Style#_updateSources` is called
49
- // before the TileJSON arrives. this makes sure the tiles needed are loaded once TileJSON arrives
50
- // ref: https://github.com/mapbox/mapbox-gl-js/pull/4347#discussion_r104418088
51
- this.fire(new Event('data', { dataType: 'source', sourceDataType: 'metadata' }));
52
- this.fire(new Event('data', { dataType: 'source', sourceDataType: 'content' }));
53
- }
54
- });
37
+ try {
38
+ const tileJSON = await loadTileJSON(this._options);
39
+ Object.assign(this, tileJSON);
40
+ if (tileJSON.bounds) this.tileBounds = new TileBounds(tileJSON.bounds, this.minzoom, this.maxzoom);
41
+
42
+ // `content` is included here to prevent a race condition where `Style#_updateSources` is called
43
+ // before the TileJSON arrives. this makes sure the tiles needed are loaded once TileJSON arrives
44
+ // ref: https://github.com/mapbox/mapbox-gl-js/pull/4347#discussion_r104418088
45
+ this.fire(new Event('data', { dataType: 'source', sourceDataType: 'metadata' }));
46
+ this.fire(new Event('data', { dataType: 'source', sourceDataType: 'content' }));
47
+ } catch (err) {
48
+ this.fire(new ErrorEvent(err));
49
+ }
55
50
  }
56
51
 
57
52
  hasTile(tileID) {
@@ -67,78 +62,66 @@ class VectorTileSource extends Evented {
67
62
  return Object.assign({}, this._options);
68
63
  }
69
64
 
70
- loadTile(tile, callback) {
71
- tile.abortController = new window.AbortController();
72
- this.tiles(tile.tileID.canonical, tile.abortController)
73
- .catch(() => {})
74
- .then(data => {
75
- if (!data) {
76
- const err = new Error('Tile could not be loaded');
77
- err.status = 404; // will try to use the parent/child tile
78
- return done(err);
79
- }
80
- const params = {
81
- response: { data },
82
- uid: tile.uid,
83
- tileID: tile.tileID,
84
- zoom: tile.tileID.overscaledZ,
85
- tileSize: this.tileSize * tile.tileID.overscaleFactor(),
86
- type: this.type,
87
- source: this.id,
88
- pixelRatio: browser.devicePixelRatio,
89
- showCollisionBoxes: this.map.showCollisionBoxes
90
- };
91
-
92
- if (tile.workerID === undefined || tile.state === 'expired') {
93
- tile.workerID = this.dispatcher.send('loadTile', params, done.bind(this));
94
- } else if (tile.state === 'loading') {
95
- // schedule tile reloading after it has been loaded
96
- tile.reloadCallback = callback;
97
- } else {
98
- this.dispatcher.send('reloadTile', params, done.bind(this), tile.workerID);
99
- }
100
- });
101
-
102
- function done(err, data) {
103
- if (tile.aborted) return callback(null);
104
-
105
- if (err) {
106
- return callback(err);
107
- }
108
-
109
- if (data?.resourceTiming) tile.resourceTiming = data.resourceTiming;
65
+ async loadTile(tile) {
66
+ if (tile.workerID != null && tile.state === 'loading') {
67
+ tile.reloadPromise ??= Promise.withResolvers();
68
+ return tile.reloadPromise.promise;
69
+ }
70
+ const data = await this.#loadTile(tile);
71
+ if (tile.reloadPromise) {
72
+ const { resolve, reject } = tile.reloadPromise;
73
+ tile.reloadPromise = null;
74
+ return this.loadTile(tile).then(resolve, reject);
75
+ }
76
+ return data;
77
+ }
110
78
 
79
+ async #loadTile(tile) {
80
+ try {
81
+ tile.abortController = new window.AbortController();
82
+ const rawData = await this.tiles(tile.tileID.canonical, tile.abortController).catch(() => {});
83
+ if (!rawData) {
84
+ const err = new Error('Tile could not be loaded');
85
+ err.status = 404; // will try to use the parent/child tile
86
+ throw err;
87
+ }
88
+ const params = {
89
+ response: { data: rawData.slice() },
90
+ uid: tile.uid,
91
+ tileID: tile.tileID,
92
+ zoom: tile.tileID.overscaledZ,
93
+ tileSize: this.tileSize * tile.tileID.overscaleFactor(),
94
+ type: this.type,
95
+ source: this.id,
96
+ pixelRatio: browser.devicePixelRatio,
97
+ showCollisionBoxes: this.map.showCollisionBoxes
98
+ };
99
+ tile.workerID ??= this.dispatcher.nextWorkerId();
100
+ const data = await this.dispatcher.send('loadTile', params, tile.workerID);
101
+ data.rawTileData = rawData;
111
102
  tile.loadVectorData(data, this.map.painter);
112
-
113
- callback(null);
114
-
115
- if (tile.reloadCallback) {
116
- this.loadTile(tile, tile.reloadCallback);
117
- tile.reloadCallback = null;
103
+ } catch (err) {
104
+ if (tile.aborted) {
105
+ tile.state = 'unloaded';
106
+ return;
118
107
  }
108
+ tile.state = 'errored';
109
+ throw err;
119
110
  }
120
111
  }
121
112
 
122
113
  abortTile(tile) {
123
114
  tile.aborted = true;
124
115
  tile.abortController.abort();
125
- this.dispatcher.send('abortTile', { uid: tile.uid, type: this.type, source: this.id }, undefined, tile.workerID);
126
116
  }
127
117
 
128
118
  unloadTile(tile) {
129
119
  tile.unloadVectorData();
130
- this.dispatcher.send('removeTile', { uid: tile.uid, type: this.type, source: this.id }, undefined, tile.workerID);
131
120
  }
132
121
 
133
122
  hasTransition() {
134
123
  return false;
135
124
  }
136
-
137
- updateWorkerConfig() {
138
- this.dispatcher.broadcast('vector.updateConfig', {
139
- source: this.id
140
- });
141
- }
142
125
  }
143
126
 
144
127
  module.exports = VectorTileSource;
@@ -1,19 +1,18 @@
1
- const vt = require('@mapbox/vector-tile');
1
+ const { VectorTile } = require('@mapbox/vector-tile');
2
2
  const Protobuf = require('@mapwhit/pbf');
3
3
  const WorkerTile = require('./worker_tile');
4
4
 
5
- function loadVectorTile(params, callback) {
5
+ function loadVectorTile(params) {
6
6
  if (!params.response) {
7
- return callback(new Error('no tile data'));
7
+ throw new Error('no tile data');
8
8
  }
9
9
  const { data } = params.response;
10
10
  if (!data) {
11
- return callback();
11
+ return;
12
12
  }
13
- callback(null, {
14
- vectorTile: new vt.VectorTile(new Protobuf(data)),
15
- rawData: data
16
- });
13
+ return {
14
+ vectorTile: new VectorTile(new Protobuf(data))
15
+ };
17
16
  }
18
17
 
19
18
  /**
@@ -32,11 +31,10 @@ class VectorTileWorkerSource {
32
31
  * {@link VectorTileWorkerSource#loadTile}. The default implementation simply
33
32
  * loads the pbf at `params.url`.
34
33
  */
35
- constructor(actor, layerIndex, loadVectorData) {
34
+ constructor(actor, layerIndex, loadVectorData = loadVectorTile) {
36
35
  this.actor = actor;
37
36
  this.layerIndex = layerIndex;
38
- this.loadVectorData = loadVectorData || loadVectorTile.bind(this);
39
- this.loaded = {};
37
+ this.loadVectorData = loadVectorData;
40
38
  }
41
39
 
42
40
  /**
@@ -44,83 +42,20 @@ class VectorTileWorkerSource {
44
42
  * {@link VectorTileWorkerSource#loadVectorData} (which by default expects
45
43
  * a `params.url` property) for fetching and producing a VectorTile object.
46
44
  */
47
- loadTile(params, callback) {
48
- const uid = params.uid;
49
-
50
- const workerTile = new WorkerTile(params);
51
- this.loadVectorData(params, (err, response) => {
52
- if (err || !response) {
53
- return callback(err);
54
- }
55
-
56
- const rawTileData = response.rawData;
57
- workerTile.vectorTile = response.vectorTile;
58
- workerTile.parse(response.vectorTile, this.layerIndex, this.actor, (err, result) => {
59
- if (err || !result) return callback(err);
60
-
61
- // Transferring a copy of rawTileData because the worker needs to retain its copy.
62
- callback(null, Object.assign({ rawTileData: rawTileData.slice(0) }, result));
63
- });
64
-
65
- this.loaded = this.loaded || {};
66
- this.loaded[uid] = workerTile;
67
- });
68
- }
69
-
70
- /**
71
- * Implements {@link WorkerSource#reloadTile}.
72
- */
73
- reloadTile(params, callback) {
74
- const loaded = this.loaded;
75
- const uid = params.uid;
76
- const vtSource = this;
77
- if (loaded?.[uid]) {
78
- const workerTile = loaded[uid];
79
- workerTile.showCollisionBoxes = params.showCollisionBoxes;
80
-
81
- const done = (err, data) => {
82
- const reloadCallback = workerTile.reloadCallback;
83
- if (reloadCallback) {
84
- delete workerTile.reloadCallback;
85
- workerTile.parse(workerTile.vectorTile, vtSource.layerIndex, vtSource.actor, reloadCallback);
86
- }
87
- callback(err, data);
88
- };
89
-
90
- if (workerTile.status === 'parsing') {
91
- workerTile.reloadCallback = done;
92
- } else if (workerTile.status === 'done') {
93
- workerTile.parse(workerTile.vectorTile, this.layerIndex, this.actor, done);
94
- }
45
+ async loadTile(params) {
46
+ const response = this.loadVectorData(params);
47
+ if (!response) {
48
+ return;
95
49
  }
96
- }
97
-
98
- /**
99
- * Implements {@link WorkerSource#abortTile}.
100
- *
101
- * @param params
102
- * @param params.uid The UID for this tile.
103
- */
104
- abortTile(params, callback) {
105
- callback();
106
- }
107
-
108
- /**
109
- * Implements {@link WorkerSource#removeTile}.
110
- *
111
- * @param params
112
- * @param params.uid The UID for this tile.
113
- */
114
- removeTile(params, callback) {
115
- const loaded = this.loaded;
116
- const uid = params.uid;
117
- if (loaded?.[uid]) {
118
- delete loaded[uid];
50
+ const { vectorTile, rawData } = response;
51
+ const workerTile = new WorkerTile(params);
52
+ workerTile.vectorTile = vectorTile;
53
+ const result = await workerTile.parse(vectorTile, this.layerIndex, this.actor);
54
+ if (rawData) {
55
+ result.rawTileData = rawData;
119
56
  }
120
- callback();
57
+ return result;
121
58
  }
122
-
123
- updateConfig() {}
124
59
  }
125
60
 
126
61
  module.exports = VectorTileWorkerSource;