@carto/api-client 0.5.0-alpha.8 → 0.5.0

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 (43) hide show
  1. package/CHANGELOG.md +30 -1
  2. package/build/api-client.cjs +15248 -3366
  3. package/build/api-client.cjs.map +1 -0
  4. package/build/api-client.d.cts +529 -182
  5. package/build/api-client.d.ts +529 -182
  6. package/build/api-client.js +15000 -3227
  7. package/build/api-client.js.map +1 -0
  8. package/build/worker.js +5118 -810
  9. package/build/worker.js.map +1 -0
  10. package/package.json +47 -31
  11. package/src/api/query.ts +2 -1
  12. package/src/constants-internal.ts +10 -0
  13. package/src/constants.ts +5 -1
  14. package/src/deck/get-data-filter-extension-props.ts +27 -9
  15. package/src/fetch-map/basemap-styles.ts +159 -0
  16. package/src/fetch-map/basemap.ts +120 -0
  17. package/src/fetch-map/fetch-map.ts +331 -0
  18. package/src/fetch-map/index.ts +13 -0
  19. package/src/fetch-map/layer-map.ts +461 -0
  20. package/src/fetch-map/parse-map.ts +425 -0
  21. package/src/fetch-map/source.ts +233 -0
  22. package/src/fetch-map/types.ts +268 -0
  23. package/src/fetch-map/utils.ts +69 -0
  24. package/src/filters/tileFeatures.ts +27 -10
  25. package/src/filters/tileFeaturesRaster.ts +122 -0
  26. package/src/index.ts +1 -0
  27. package/src/models/model.ts +0 -7
  28. package/src/sources/base-source.ts +4 -2
  29. package/src/sources/h3-tileset-source.ts +1 -1
  30. package/src/sources/quadbin-tileset-source.ts +1 -1
  31. package/src/sources/raster-source.ts +18 -5
  32. package/src/sources/types.ts +15 -8
  33. package/src/sources/vector-tileset-source.ts +1 -1
  34. package/src/spatial-index.ts +3 -84
  35. package/src/types.ts +16 -2
  36. package/src/widget-sources/index.ts +1 -0
  37. package/src/widget-sources/types.ts +0 -2
  38. package/src/widget-sources/widget-raster-source.ts +14 -0
  39. package/src/widget-sources/widget-remote-source.ts +16 -91
  40. package/src/widget-sources/widget-source.ts +9 -25
  41. package/src/widget-sources/widget-tileset-source-impl.ts +13 -16
  42. package/src/widget-sources/widget-tileset-source.ts +53 -47
  43. package/src/workers/widget-tileset-worker.ts +5 -7
@@ -55,21 +55,26 @@ export type WidgetTilesetSourceResult = {widgetSource: WidgetTilesetSource};
55
55
  * const { widgetSource } = await data;
56
56
  * ```
57
57
  */
58
- export class WidgetTilesetSource extends WidgetSource<WidgetTilesetSourceProps> {
58
+ export class WidgetTilesetSource<
59
+ Props extends WidgetTilesetSourceProps = WidgetTilesetSourceProps,
60
+ > extends WidgetSource<Props> {
59
61
  protected _localImpl: WidgetTilesetSourceImpl | null = null;
60
62
 
61
63
  protected _workerImpl: Worker | null = null;
62
64
  protected _workerEnabled: boolean;
63
65
  protected _workerNextRequestId = 1;
64
66
 
65
- constructor(props: WidgetTilesetSourceProps) {
67
+ constructor(props: Props) {
66
68
  super(props);
67
69
 
68
70
  this._workerEnabled =
69
- (props.widgetSourceWorker ?? true) && TSUP_FORMAT !== 'cjs';
70
- this._localImpl = this._workerEnabled
71
- ? null
72
- : new WidgetTilesetSourceImpl(this.props);
71
+ (props.widgetWorker ?? true) &&
72
+ TSUP_FORMAT !== 'cjs' &&
73
+ typeof Worker !== 'undefined';
74
+
75
+ if (!this._workerEnabled) {
76
+ this._localImpl = new WidgetTilesetSourceImpl(this.props);
77
+ }
73
78
  }
74
79
 
75
80
  destroy() {
@@ -89,28 +94,25 @@ export class WidgetTilesetSource extends WidgetSource<WidgetTilesetSourceProps>
89
94
  * Returns an initialized Worker, to be reused for the lifecycle of this
90
95
  * source instance.
91
96
  */
92
- protected _getWorker(): Worker | null {
93
- if (this._workerImpl || this._localImpl) {
97
+ protected _getWorker(): Worker {
98
+ if (this._workerImpl) {
94
99
  return this._workerImpl;
95
100
  }
96
101
 
97
- try {
98
- this._workerImpl = new Worker(new URL('worker.js', import.meta.url), {
102
+ this._workerImpl = new Worker(
103
+ new URL('@carto/api-client/worker', import.meta.url),
104
+ {
99
105
  type: 'module',
100
106
  name: 'cartowidgettileset',
101
- });
107
+ }
108
+ );
102
109
 
103
- this._workerImpl.postMessage({
104
- method: Method.INIT,
105
- params: [this.props],
106
- } as WorkerRequest);
110
+ this._workerImpl.postMessage({
111
+ method: Method.INIT,
112
+ params: [this.props],
113
+ } as WorkerRequest);
107
114
 
108
- return this._workerImpl;
109
- } catch {
110
- this._workerEnabled = false;
111
- this._localImpl = new WidgetTilesetSourceImpl(this.props);
112
- return null;
113
- }
115
+ return this._workerImpl;
114
116
  }
115
117
 
116
118
  /** Executes a given method on the worker. */
@@ -119,23 +121,14 @@ export class WidgetTilesetSource extends WidgetSource<WidgetTilesetSourceProps>
119
121
  params: unknown[],
120
122
  signal?: AbortSignal
121
123
  ): Promise<T> {
122
- const worker = this._getWorker();
123
- if (!worker) {
124
+ if (!this._workerEnabled) {
124
125
  // @ts-expect-error No type-checking dynamic method name.
125
126
  return this._localImpl[method](...params);
126
127
  }
127
128
 
129
+ const worker = this._getWorker();
128
130
  const requestId = this._workerNextRequestId++;
129
131
 
130
- // TODO: ViewState may contain non-serializable data, which we do not need.
131
- // Remove this sanitization after sc-469614 is fixed.
132
- const options = params[0] as any;
133
- if (options?.spatialIndexReferenceViewState) {
134
- const {zoom, latitude, longitude} =
135
- options.spatialIndexReferenceViewState;
136
- options.spatialIndexReferenceViewState = {zoom, latitude, longitude};
137
- }
138
-
139
132
  let resolve: ((value: T) => void) | null = null;
140
133
  let reject: ((reason: any) => void) | null = null;
141
134
 
@@ -145,10 +138,9 @@ export class WidgetTilesetSource extends WidgetSource<WidgetTilesetSourceProps>
145
138
  function onMessage(e: MessageEvent) {
146
139
  const response = e.data as WorkerResponse;
147
140
  if (response.requestId !== requestId) return;
141
+ if (signal?.aborted) return; // handled by 'abort' listener
148
142
 
149
- if (signal?.aborted) {
150
- reject!(new Error(signal.reason));
151
- } else if (response.ok) {
143
+ if (response.ok) {
152
144
  resolve!(response.result as T);
153
145
  } else {
154
146
  reject!(new Error(response.error));
@@ -193,16 +185,28 @@ export class WidgetTilesetSource extends WidgetSource<WidgetTilesetSourceProps>
193
185
  * before computing statistics on the tiles.
194
186
  */
195
187
  loadTiles(tiles: unknown[]) {
196
- const worker = this._getWorker();
197
- if (!worker) {
188
+ if (!this._workerEnabled) {
198
189
  return this._localImpl!.loadTiles(tiles);
199
190
  }
200
191
 
201
- tiles = (tiles as Tile[]).map(({id, bbox, data}) => ({
202
- id,
203
- bbox,
204
- data,
205
- }));
192
+ const worker = this._getWorker();
193
+
194
+ // We cannot pass an instance of Tile2DHeader to a Web Worker, and must
195
+ // extract properties required for widget calculations. Note that the
196
+ // `tile: Tile = {...}` assignment is structured so TS will warn if any
197
+ // types required by the internal 'Tile' type are missing.
198
+ tiles = (tiles as Tile[]).map(
199
+ ({id, index, bbox, isVisible, data}: Tile) => {
200
+ const tile: Tile = {
201
+ id,
202
+ index,
203
+ isVisible,
204
+ data,
205
+ bbox,
206
+ };
207
+ return tile;
208
+ }
209
+ );
206
210
 
207
211
  worker.postMessage({
208
212
  method: Method.LOAD_TILES,
@@ -212,13 +216,14 @@ export class WidgetTilesetSource extends WidgetSource<WidgetTilesetSourceProps>
212
216
 
213
217
  /** Configures options used to extract features from tiles. */
214
218
  setTileFeatureExtractOptions(options: TileFeatureExtractOptions) {
215
- const worker = this._getWorker();
216
- if (!worker) {
219
+ if (!this._workerEnabled) {
217
220
  return this._localImpl?.setTileFeatureExtractOptions(options);
218
221
  }
219
222
 
223
+ const worker = this._getWorker();
224
+
220
225
  worker.postMessage({
221
- type: Method.SET_TILE_FEATURE_EXTRACT_OPTIONS,
226
+ method: Method.SET_TILE_FEATURE_EXTRACT_OPTIONS,
222
227
  params: [options],
223
228
  });
224
229
  }
@@ -235,11 +240,12 @@ export class WidgetTilesetSource extends WidgetSource<WidgetTilesetSourceProps>
235
240
  geojson: FeatureCollection;
236
241
  spatialFilter: SpatialFilter;
237
242
  }) {
238
- const worker = this._getWorker();
239
- if (!worker) {
243
+ if (!this._workerEnabled) {
240
244
  return this._localImpl!.loadGeoJSON({geojson, spatialFilter});
241
245
  }
242
246
 
247
+ const worker = this._getWorker();
248
+
243
249
  worker.postMessage({
244
250
  method: Method.LOAD_GEOJSON,
245
251
  params: [{geojson, spatialFilter}],
@@ -1,7 +1,5 @@
1
- import {
2
- WidgetTilesetSource,
3
- type WidgetTilesetSourceProps,
4
- } from '../widget-sources/widget-tileset-source.js';
1
+ import {WidgetTilesetSourceImpl} from '../widget-sources/widget-tileset-source-impl.js';
2
+ import {type WidgetTilesetSourceProps} from '../widget-sources/widget-tileset-source.js';
5
3
  import {Method} from './constants.js';
6
4
  import type {WorkerRequest, WorkerResponse} from './types.js';
7
5
 
@@ -12,15 +10,15 @@ import type {WorkerRequest, WorkerResponse} from './types.js';
12
10
  * representing and executing calculations on a single datasource.
13
11
  */
14
12
 
15
- let source: WidgetTilesetSource;
13
+ let source: WidgetTilesetSourceImpl;
16
14
 
17
15
  addEventListener('message', (e) => {
18
16
  const {method, params, requestId} = e.data as WorkerRequest;
19
17
 
20
18
  if (method === Method.INIT) {
21
- source = new WidgetTilesetSource({
19
+ source = new WidgetTilesetSourceImpl({
22
20
  ...(params[0] as WidgetTilesetSourceProps),
23
- widgetSourceWorker: false,
21
+ widgetWorker: false,
24
22
  });
25
23
  return;
26
24
  }