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

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.
@@ -3294,7 +3294,9 @@ var WidgetTilesetSource = class extends WidgetSource {
3294
3294
  __publicField(this, "_workerEnabled");
3295
3295
  __publicField(this, "_workerNextRequestId", 1);
3296
3296
  this._workerEnabled = (props.widgetSourceWorker ?? true) && false;
3297
- this._localImpl = this._workerEnabled ? null : new WidgetTilesetSourceImpl(this.props);
3297
+ if (!this._workerEnabled) {
3298
+ this._localImpl = new WidgetTilesetSourceImpl(this.props);
3299
+ }
3298
3300
  }
3299
3301
  destroy() {
3300
3302
  this._localImpl?.destroy();
@@ -3310,31 +3312,28 @@ var WidgetTilesetSource = class extends WidgetSource {
3310
3312
  * source instance.
3311
3313
  */
3312
3314
  _getWorker() {
3313
- if (this._workerImpl || this._localImpl) {
3315
+ if (this._workerImpl) {
3314
3316
  return this._workerImpl;
3315
3317
  }
3316
- try {
3317
- this._workerImpl = new Worker(new URL("worker.js", importMetaUrl), {
3318
+ this._workerImpl = new Worker(
3319
+ new URL("@carto/api-client/worker", importMetaUrl),
3320
+ {
3318
3321
  type: "module",
3319
3322
  name: "cartowidgettileset"
3320
- });
3321
- this._workerImpl.postMessage({
3322
- method: "init" /* INIT */,
3323
- params: [this.props]
3324
- });
3325
- return this._workerImpl;
3326
- } catch {
3327
- this._workerEnabled = false;
3328
- this._localImpl = new WidgetTilesetSourceImpl(this.props);
3329
- return null;
3330
- }
3323
+ }
3324
+ );
3325
+ this._workerImpl.postMessage({
3326
+ method: "init" /* INIT */,
3327
+ params: [this.props]
3328
+ });
3329
+ return this._workerImpl;
3331
3330
  }
3332
3331
  /** Executes a given method on the worker. */
3333
3332
  _executeWorkerMethod(method, params, signal) {
3334
- const worker = this._getWorker();
3335
- if (!worker) {
3333
+ if (!this._workerEnabled) {
3336
3334
  return this._localImpl[method](...params);
3337
3335
  }
3336
+ const worker = this._getWorker();
3338
3337
  const requestId = this._workerNextRequestId++;
3339
3338
  const options = params[0];
3340
3339
  if (options?.spatialIndexReferenceViewState) {
@@ -3382,10 +3381,10 @@ var WidgetTilesetSource = class extends WidgetSource {
3382
3381
  * before computing statistics on the tiles.
3383
3382
  */
3384
3383
  loadTiles(tiles2) {
3385
- const worker = this._getWorker();
3386
- if (!worker) {
3384
+ if (!this._workerEnabled) {
3387
3385
  return this._localImpl.loadTiles(tiles2);
3388
3386
  }
3387
+ const worker = this._getWorker();
3389
3388
  tiles2 = tiles2.map(({ id, bbox, data }) => ({
3390
3389
  id,
3391
3390
  bbox,
@@ -3398,10 +3397,10 @@ var WidgetTilesetSource = class extends WidgetSource {
3398
3397
  }
3399
3398
  /** Configures options used to extract features from tiles. */
3400
3399
  setTileFeatureExtractOptions(options) {
3401
- const worker = this._getWorker();
3402
- if (!worker) {
3400
+ if (!this._workerEnabled) {
3403
3401
  return this._localImpl?.setTileFeatureExtractOptions(options);
3404
3402
  }
3403
+ const worker = this._getWorker();
3405
3404
  worker.postMessage({
3406
3405
  type: "setTileFeatureExtractOptions" /* SET_TILE_FEATURE_EXTRACT_OPTIONS */,
3407
3406
  params: [options]
@@ -3416,10 +3415,10 @@ var WidgetTilesetSource = class extends WidgetSource {
3416
3415
  geojson,
3417
3416
  spatialFilter
3418
3417
  }) {
3419
- const worker = this._getWorker();
3420
- if (!worker) {
3418
+ if (!this._workerEnabled) {
3421
3419
  return this._localImpl.loadGeoJSON({ geojson, spatialFilter });
3422
3420
  }
3421
+ const worker = this._getWorker();
3423
3422
  worker.postMessage({
3424
3423
  method: "loadGeoJSON" /* LOAD_GEOJSON */,
3425
3424
  params: [{ geojson, spatialFilter }]
@@ -1170,7 +1170,7 @@ declare class WidgetTilesetSource extends WidgetSource<WidgetTilesetSourceProps>
1170
1170
  * Returns an initialized Worker, to be reused for the lifecycle of this
1171
1171
  * source instance.
1172
1172
  */
1173
- protected _getWorker(): Worker | null;
1173
+ protected _getWorker(): Worker;
1174
1174
  /** Executes a given method on the worker. */
1175
1175
  protected _executeWorkerMethod<T>(method: Method, params: unknown[], signal?: AbortSignal): Promise<T>;
1176
1176
  /**
@@ -1170,7 +1170,7 @@ declare class WidgetTilesetSource extends WidgetSource<WidgetTilesetSourceProps>
1170
1170
  * Returns an initialized Worker, to be reused for the lifecycle of this
1171
1171
  * source instance.
1172
1172
  */
1173
- protected _getWorker(): Worker | null;
1173
+ protected _getWorker(): Worker;
1174
1174
  /** Executes a given method on the worker. */
1175
1175
  protected _executeWorkerMethod<T>(method: Method, params: unknown[], signal?: AbortSignal): Promise<T>;
1176
1176
  /**
@@ -3099,8 +3099,10 @@ var WidgetTilesetSource = class extends WidgetSource {
3099
3099
  __publicField(this, "_workerImpl", null);
3100
3100
  __publicField(this, "_workerEnabled");
3101
3101
  __publicField(this, "_workerNextRequestId", 1);
3102
- this._workerEnabled = (props.widgetSourceWorker ?? true) && true;
3103
- this._localImpl = this._workerEnabled ? null : new WidgetTilesetSourceImpl(this.props);
3102
+ this._workerEnabled = (props.widgetSourceWorker ?? true) && true && typeof Worker !== "undefined";
3103
+ if (!this._workerEnabled) {
3104
+ this._localImpl = new WidgetTilesetSourceImpl(this.props);
3105
+ }
3104
3106
  }
3105
3107
  destroy() {
3106
3108
  this._localImpl?.destroy();
@@ -3116,31 +3118,28 @@ var WidgetTilesetSource = class extends WidgetSource {
3116
3118
  * source instance.
3117
3119
  */
3118
3120
  _getWorker() {
3119
- if (this._workerImpl || this._localImpl) {
3121
+ if (this._workerImpl) {
3120
3122
  return this._workerImpl;
3121
3123
  }
3122
- try {
3123
- this._workerImpl = new Worker(new URL("worker.js", import.meta.url), {
3124
+ this._workerImpl = new Worker(
3125
+ new URL("@carto/api-client/worker", import.meta.url),
3126
+ {
3124
3127
  type: "module",
3125
3128
  name: "cartowidgettileset"
3126
- });
3127
- this._workerImpl.postMessage({
3128
- method: "init" /* INIT */,
3129
- params: [this.props]
3130
- });
3131
- return this._workerImpl;
3132
- } catch {
3133
- this._workerEnabled = false;
3134
- this._localImpl = new WidgetTilesetSourceImpl(this.props);
3135
- return null;
3136
- }
3129
+ }
3130
+ );
3131
+ this._workerImpl.postMessage({
3132
+ method: "init" /* INIT */,
3133
+ params: [this.props]
3134
+ });
3135
+ return this._workerImpl;
3137
3136
  }
3138
3137
  /** Executes a given method on the worker. */
3139
3138
  _executeWorkerMethod(method, params, signal) {
3140
- const worker = this._getWorker();
3141
- if (!worker) {
3139
+ if (!this._workerEnabled) {
3142
3140
  return this._localImpl[method](...params);
3143
3141
  }
3142
+ const worker = this._getWorker();
3144
3143
  const requestId = this._workerNextRequestId++;
3145
3144
  const options = params[0];
3146
3145
  if (options?.spatialIndexReferenceViewState) {
@@ -3188,10 +3187,10 @@ var WidgetTilesetSource = class extends WidgetSource {
3188
3187
  * before computing statistics on the tiles.
3189
3188
  */
3190
3189
  loadTiles(tiles2) {
3191
- const worker = this._getWorker();
3192
- if (!worker) {
3190
+ if (!this._workerEnabled) {
3193
3191
  return this._localImpl.loadTiles(tiles2);
3194
3192
  }
3193
+ const worker = this._getWorker();
3195
3194
  tiles2 = tiles2.map(({ id, bbox, data }) => ({
3196
3195
  id,
3197
3196
  bbox,
@@ -3204,10 +3203,10 @@ var WidgetTilesetSource = class extends WidgetSource {
3204
3203
  }
3205
3204
  /** Configures options used to extract features from tiles. */
3206
3205
  setTileFeatureExtractOptions(options) {
3207
- const worker = this._getWorker();
3208
- if (!worker) {
3206
+ if (!this._workerEnabled) {
3209
3207
  return this._localImpl?.setTileFeatureExtractOptions(options);
3210
3208
  }
3209
+ const worker = this._getWorker();
3211
3210
  worker.postMessage({
3212
3211
  type: "setTileFeatureExtractOptions" /* SET_TILE_FEATURE_EXTRACT_OPTIONS */,
3213
3212
  params: [options]
@@ -3222,10 +3221,10 @@ var WidgetTilesetSource = class extends WidgetSource {
3222
3221
  geojson,
3223
3222
  spatialFilter
3224
3223
  }) {
3225
- const worker = this._getWorker();
3226
- if (!worker) {
3224
+ if (!this._workerEnabled) {
3227
3225
  return this._localImpl.loadGeoJSON({ geojson, spatialFilter });
3228
3226
  }
3227
+ const worker = this._getWorker();
3229
3228
  worker.postMessage({
3230
3229
  method: "loadGeoJSON" /* LOAD_GEOJSON */,
3231
3230
  params: [{ geojson, spatialFilter }]
package/build/worker.js CHANGED
@@ -394,12 +394,6 @@ var require_thenBy_module = __commonJS({
394
394
  }
395
395
  });
396
396
 
397
- // src/client.ts
398
- var client = "deck-gl-carto";
399
- function getClient() {
400
- return client;
401
- }
402
-
403
397
  // src/constants.ts
404
398
  var FilterType = /* @__PURE__ */ ((FilterType2) => {
405
399
  FilterType2["IN"] = "in";
@@ -411,113 +405,6 @@ var FilterType = /* @__PURE__ */ ((FilterType2) => {
411
405
  })(FilterType || {});
412
406
  var DEFAULT_API_BASE_URL = "https://gcp-us-east1.api.carto.com";
413
407
 
414
- // src/constants-internal.ts
415
- var DEFAULT_GEO_COLUMN = "geom";
416
- var DEFAULT_AGGREGATION_RES_LEVEL_H3 = 4;
417
- var DEFAULT_AGGREGATION_RES_LEVEL_QUADBIN = 6;
418
-
419
- // src/spatial-index.ts
420
- var DEFAULT_TILE_SIZE = 512;
421
- var QUADBIN_ZOOM_MAX_OFFSET = 4;
422
- function getSpatialFiltersResolution(source2, viewState) {
423
- const dataResolution = source2.dataResolution ?? Number.MAX_VALUE;
424
- const aggregationResLevel = source2.aggregationResLevel ?? (source2.spatialDataType === "h3" ? DEFAULT_AGGREGATION_RES_LEVEL_H3 : DEFAULT_AGGREGATION_RES_LEVEL_QUADBIN);
425
- const aggregationResLevelOffset = Math.max(
426
- 0,
427
- Math.floor(aggregationResLevel)
428
- );
429
- const currentZoomInt = Math.ceil(viewState.zoom);
430
- if (source2.spatialDataType === "h3") {
431
- const tileSize = DEFAULT_TILE_SIZE;
432
- const maxResolutionForZoom = maxH3SpatialFiltersResolutions.find(
433
- ([zoom]) => zoom === currentZoomInt
434
- )?.[1] ?? Math.max(0, currentZoomInt - 3);
435
- const maxSpatialFiltersResolution = maxResolutionForZoom ? Math.min(dataResolution, maxResolutionForZoom) : dataResolution;
436
- const hexagonResolution = _getHexagonResolution(viewState, tileSize) + aggregationResLevelOffset;
437
- return Math.min(hexagonResolution, maxSpatialFiltersResolution);
438
- }
439
- if (source2.spatialDataType === "quadbin") {
440
- const maxResolutionForZoom = currentZoomInt + QUADBIN_ZOOM_MAX_OFFSET;
441
- const maxSpatialFiltersResolution = Math.min(
442
- dataResolution,
443
- maxResolutionForZoom
444
- );
445
- const quadsResolution = Math.floor(viewState.zoom) + aggregationResLevelOffset;
446
- return Math.min(quadsResolution, maxSpatialFiltersResolution);
447
- }
448
- return void 0;
449
- }
450
- var maxH3SpatialFiltersResolutions = [
451
- [20, 14],
452
- [19, 13],
453
- [18, 12],
454
- [17, 11],
455
- [16, 10],
456
- [15, 9],
457
- [14, 8],
458
- [13, 7],
459
- [12, 7],
460
- [11, 7],
461
- [10, 6],
462
- [9, 6],
463
- [8, 5],
464
- [7, 4],
465
- [6, 4],
466
- [5, 3],
467
- [4, 2],
468
- [3, 1],
469
- [2, 1],
470
- [1, 0]
471
- ];
472
- var BIAS = 2;
473
- function _getHexagonResolution(viewport, tileSize) {
474
- const zoomOffset = Math.log2(tileSize / DEFAULT_TILE_SIZE);
475
- const hexagonScaleFactor = 2 / 3 * (viewport.zoom - zoomOffset);
476
- const latitudeScaleFactor = Math.log(
477
- 1 / Math.cos(Math.PI * viewport.latitude / 180)
478
- );
479
- return Math.max(
480
- 0,
481
- Math.floor(hexagonScaleFactor + latitudeScaleFactor - BIAS)
482
- );
483
- }
484
-
485
- // src/widget-sources/widget-source.ts
486
- var _WidgetSource = class _WidgetSource {
487
- constructor(props) {
488
- __publicField(this, "props");
489
- this.props = { ..._WidgetSource.defaultProps, ...props };
490
- }
491
- /**
492
- * Destroys the widget source and releases allocated resources.
493
- *
494
- * For remote sources (tables, queries) this has no effect, but for local
495
- * sources (tilesets, rasters) these resources will affect performance
496
- * and stability if many (10+) sources are created and not released.
497
- */
498
- destroy() {
499
- }
500
- _getSpatialFiltersResolution(source2, spatialFilter, referenceViewState) {
501
- if (!spatialFilter || source2.spatialDataType === "geo") {
502
- return;
503
- }
504
- if (!referenceViewState) {
505
- throw new Error(
506
- 'Missing required option, "spatialIndexReferenceViewState".'
507
- );
508
- }
509
- return getSpatialFiltersResolution(source2, referenceViewState);
510
- }
511
- };
512
- __publicField(_WidgetSource, "defaultProps", {
513
- apiVersion: "v3" /* V3 */,
514
- apiBaseUrl: DEFAULT_API_BASE_URL,
515
- clientId: getClient(),
516
- filters: {},
517
- filtersLogicalOperator: "and"
518
- });
519
- var WidgetSource = _WidgetSource;
520
-
521
408
  // src/utils.ts
522
409
  var FILTER_TYPES = new Set(Object.values(FilterType));
523
410
  var isFilterType = (type) => FILTER_TYPES.has(type);
@@ -1271,6 +1158,11 @@ function getSpatialIndex(spatialDataType) {
1271
1158
  }
1272
1159
  }
1273
1160
 
1161
+ // src/constants-internal.ts
1162
+ var DEFAULT_GEO_COLUMN = "geom";
1163
+ var DEFAULT_AGGREGATION_RES_LEVEL_H3 = 4;
1164
+ var DEFAULT_AGGREGATION_RES_LEVEL_QUADBIN = 6;
1165
+
1274
1166
  // src/filters/tileFeatures.ts
1275
1167
  function tileFeatures({
1276
1168
  tiles: tiles2,
@@ -1637,6 +1529,114 @@ function scatterPlot({
1637
1529
  );
1638
1530
  }
1639
1531
 
1532
+ // src/client.ts
1533
+ var client = "deck-gl-carto";
1534
+ function getClient() {
1535
+ return client;
1536
+ }
1537
+
1538
+ // src/spatial-index.ts
1539
+ var DEFAULT_TILE_SIZE = 512;
1540
+ var QUADBIN_ZOOM_MAX_OFFSET = 4;
1541
+ function getSpatialFiltersResolution(source2, viewState) {
1542
+ const dataResolution = source2.dataResolution ?? Number.MAX_VALUE;
1543
+ const aggregationResLevel = source2.aggregationResLevel ?? (source2.spatialDataType === "h3" ? DEFAULT_AGGREGATION_RES_LEVEL_H3 : DEFAULT_AGGREGATION_RES_LEVEL_QUADBIN);
1544
+ const aggregationResLevelOffset = Math.max(
1545
+ 0,
1546
+ Math.floor(aggregationResLevel)
1547
+ );
1548
+ const currentZoomInt = Math.ceil(viewState.zoom);
1549
+ if (source2.spatialDataType === "h3") {
1550
+ const tileSize = DEFAULT_TILE_SIZE;
1551
+ const maxResolutionForZoom = maxH3SpatialFiltersResolutions.find(
1552
+ ([zoom]) => zoom === currentZoomInt
1553
+ )?.[1] ?? Math.max(0, currentZoomInt - 3);
1554
+ const maxSpatialFiltersResolution = maxResolutionForZoom ? Math.min(dataResolution, maxResolutionForZoom) : dataResolution;
1555
+ const hexagonResolution = _getHexagonResolution(viewState, tileSize) + aggregationResLevelOffset;
1556
+ return Math.min(hexagonResolution, maxSpatialFiltersResolution);
1557
+ }
1558
+ if (source2.spatialDataType === "quadbin") {
1559
+ const maxResolutionForZoom = currentZoomInt + QUADBIN_ZOOM_MAX_OFFSET;
1560
+ const maxSpatialFiltersResolution = Math.min(
1561
+ dataResolution,
1562
+ maxResolutionForZoom
1563
+ );
1564
+ const quadsResolution = Math.floor(viewState.zoom) + aggregationResLevelOffset;
1565
+ return Math.min(quadsResolution, maxSpatialFiltersResolution);
1566
+ }
1567
+ return void 0;
1568
+ }
1569
+ var maxH3SpatialFiltersResolutions = [
1570
+ [20, 14],
1571
+ [19, 13],
1572
+ [18, 12],
1573
+ [17, 11],
1574
+ [16, 10],
1575
+ [15, 9],
1576
+ [14, 8],
1577
+ [13, 7],
1578
+ [12, 7],
1579
+ [11, 7],
1580
+ [10, 6],
1581
+ [9, 6],
1582
+ [8, 5],
1583
+ [7, 4],
1584
+ [6, 4],
1585
+ [5, 3],
1586
+ [4, 2],
1587
+ [3, 1],
1588
+ [2, 1],
1589
+ [1, 0]
1590
+ ];
1591
+ var BIAS = 2;
1592
+ function _getHexagonResolution(viewport, tileSize) {
1593
+ const zoomOffset = Math.log2(tileSize / DEFAULT_TILE_SIZE);
1594
+ const hexagonScaleFactor = 2 / 3 * (viewport.zoom - zoomOffset);
1595
+ const latitudeScaleFactor = Math.log(
1596
+ 1 / Math.cos(Math.PI * viewport.latitude / 180)
1597
+ );
1598
+ return Math.max(
1599
+ 0,
1600
+ Math.floor(hexagonScaleFactor + latitudeScaleFactor - BIAS)
1601
+ );
1602
+ }
1603
+
1604
+ // src/widget-sources/widget-source.ts
1605
+ var _WidgetSource = class _WidgetSource {
1606
+ constructor(props) {
1607
+ __publicField(this, "props");
1608
+ this.props = { ..._WidgetSource.defaultProps, ...props };
1609
+ }
1610
+ /**
1611
+ * Destroys the widget source and releases allocated resources.
1612
+ *
1613
+ * For remote sources (tables, queries) this has no effect, but for local
1614
+ * sources (tilesets, rasters) these resources will affect performance
1615
+ * and stability if many (10+) sources are created and not released.
1616
+ */
1617
+ destroy() {
1618
+ }
1619
+ _getSpatialFiltersResolution(source2, spatialFilter, referenceViewState) {
1620
+ if (!spatialFilter || source2.spatialDataType === "geo") {
1621
+ return;
1622
+ }
1623
+ if (!referenceViewState) {
1624
+ throw new Error(
1625
+ 'Missing required option, "spatialIndexReferenceViewState".'
1626
+ );
1627
+ }
1628
+ return getSpatialFiltersResolution(source2, referenceViewState);
1629
+ }
1630
+ };
1631
+ __publicField(_WidgetSource, "defaultProps", {
1632
+ apiVersion: "v3" /* V3 */,
1633
+ apiBaseUrl: DEFAULT_API_BASE_URL,
1634
+ clientId: getClient(),
1635
+ filters: {},
1636
+ filtersLogicalOperator: "and"
1637
+ });
1638
+ var WidgetSource = _WidgetSource;
1639
+
1640
1640
  // src/widget-sources/widget-tileset-source-impl.ts
1641
1641
  import { booleanEqual } from "@turf/boolean-equal";
1642
1642
  var WidgetTilesetSourceImpl = class extends WidgetSource {
@@ -1924,202 +1924,12 @@ function normalizeColumns(columns) {
1924
1924
  return Array.isArray(columns) ? columns : typeof columns === "string" ? [columns] : [];
1925
1925
  }
1926
1926
 
1927
- // src/widget-sources/widget-tileset-source.ts
1928
- var WidgetTilesetSource = class extends WidgetSource {
1929
- constructor(props) {
1930
- super(props);
1931
- __publicField(this, "_localImpl", null);
1932
- __publicField(this, "_workerImpl", null);
1933
- __publicField(this, "_workerEnabled");
1934
- __publicField(this, "_workerNextRequestId", 1);
1935
- this._workerEnabled = (props.widgetSourceWorker ?? true) && true;
1936
- this._localImpl = this._workerEnabled ? null : new WidgetTilesetSourceImpl(this.props);
1937
- }
1938
- destroy() {
1939
- this._localImpl?.destroy();
1940
- this._localImpl = null;
1941
- this._workerImpl?.terminate();
1942
- this._workerImpl = null;
1943
- super.destroy();
1944
- }
1945
- /////////////////////////////////////////////////////////////////////////////
1946
- // WEB WORKER MANAGEMENT
1947
- /**
1948
- * Returns an initialized Worker, to be reused for the lifecycle of this
1949
- * source instance.
1950
- */
1951
- _getWorker() {
1952
- if (this._workerImpl || this._localImpl) {
1953
- return this._workerImpl;
1954
- }
1955
- try {
1956
- this._workerImpl = new Worker(new URL("worker.js", import.meta.url), {
1957
- type: "module",
1958
- name: "cartowidgettileset"
1959
- });
1960
- this._workerImpl.postMessage({
1961
- method: "init" /* INIT */,
1962
- params: [this.props]
1963
- });
1964
- return this._workerImpl;
1965
- } catch {
1966
- this._workerEnabled = false;
1967
- this._localImpl = new WidgetTilesetSourceImpl(this.props);
1968
- return null;
1969
- }
1970
- }
1971
- /** Executes a given method on the worker. */
1972
- _executeWorkerMethod(method, params, signal) {
1973
- const worker = this._getWorker();
1974
- if (!worker) {
1975
- return this._localImpl[method](...params);
1976
- }
1977
- const requestId = this._workerNextRequestId++;
1978
- const options = params[0];
1979
- if (options?.spatialIndexReferenceViewState) {
1980
- const { zoom, latitude, longitude } = options.spatialIndexReferenceViewState;
1981
- options.spatialIndexReferenceViewState = { zoom, latitude, longitude };
1982
- }
1983
- let resolve = null;
1984
- let reject = null;
1985
- function onMessage(e) {
1986
- const response = e.data;
1987
- if (response.requestId !== requestId) return;
1988
- if (signal?.aborted) {
1989
- reject(new Error(signal.reason));
1990
- } else if (response.ok) {
1991
- resolve(response.result);
1992
- } else {
1993
- reject(new Error(response.error));
1994
- }
1995
- }
1996
- function onAbort() {
1997
- reject(new Error(signal.reason));
1998
- }
1999
- worker.addEventListener("message", onMessage);
2000
- signal?.addEventListener("abort", onAbort);
2001
- const promise = new Promise((_resolve, _reject) => {
2002
- resolve = _resolve;
2003
- reject = _reject;
2004
- worker.postMessage({
2005
- requestId,
2006
- method,
2007
- params
2008
- });
2009
- });
2010
- void promise.finally(() => {
2011
- worker.removeEventListener("message", onMessage);
2012
- signal?.removeEventListener("abort", onAbort);
2013
- });
2014
- return promise;
2015
- }
2016
- /////////////////////////////////////////////////////////////////////////////
2017
- // DATA LOADING
2018
- /**
2019
- * Loads features as a list of tiles (typically provided by deck.gl).
2020
- * After tiles are loaded, {@link extractTileFeatures} must be called
2021
- * before computing statistics on the tiles.
2022
- */
2023
- loadTiles(tiles2) {
2024
- const worker = this._getWorker();
2025
- if (!worker) {
2026
- return this._localImpl.loadTiles(tiles2);
2027
- }
2028
- tiles2 = tiles2.map(({ id, bbox, data }) => ({
2029
- id,
2030
- bbox,
2031
- data
2032
- }));
2033
- worker.postMessage({
2034
- method: "loadTiles" /* LOAD_TILES */,
2035
- params: [tiles2]
2036
- });
2037
- }
2038
- /** Configures options used to extract features from tiles. */
2039
- setTileFeatureExtractOptions(options) {
2040
- const worker = this._getWorker();
2041
- if (!worker) {
2042
- return this._localImpl?.setTileFeatureExtractOptions(options);
2043
- }
2044
- worker.postMessage({
2045
- type: "setTileFeatureExtractOptions" /* SET_TILE_FEATURE_EXTRACT_OPTIONS */,
2046
- params: [options]
2047
- });
2048
- }
2049
- /**
2050
- * Loads features as GeoJSON (used for testing).
2051
- * @experimental
2052
- * @internal Not for public use. Spatial filters in other method calls will be ignored.
2053
- */
2054
- loadGeoJSON({
2055
- geojson,
2056
- spatialFilter
2057
- }) {
2058
- const worker = this._getWorker();
2059
- if (!worker) {
2060
- return this._localImpl.loadGeoJSON({ geojson, spatialFilter });
2061
- }
2062
- worker.postMessage({
2063
- method: "loadGeoJSON" /* LOAD_GEOJSON */,
2064
- params: [{ geojson, spatialFilter }]
2065
- });
2066
- }
2067
- /////////////////////////////////////////////////////////////////////////////
2068
- // WIDGETS API
2069
- // eslint-disable-next-line @typescript-eslint/require-await
2070
- async getFeatures() {
2071
- throw new Error("getFeatures not supported for tilesets");
2072
- }
2073
- async getFormula({
2074
- signal,
2075
- ...options
2076
- }) {
2077
- return this._executeWorkerMethod("getFormula" /* GET_FORMULA */, [options], signal);
2078
- }
2079
- async getHistogram({
2080
- signal,
2081
- ...options
2082
- }) {
2083
- return this._executeWorkerMethod("getHistogram" /* GET_HISTOGRAM */, [options], signal);
2084
- }
2085
- async getCategories({
2086
- signal,
2087
- ...options
2088
- }) {
2089
- return this._executeWorkerMethod("getCategories" /* GET_CATEGORIES */, [options], signal);
2090
- }
2091
- async getScatter({
2092
- signal,
2093
- ...options
2094
- }) {
2095
- return this._executeWorkerMethod("getScatter" /* GET_SCATTER */, [options], signal);
2096
- }
2097
- async getTable({
2098
- signal,
2099
- ...options
2100
- }) {
2101
- return this._executeWorkerMethod("getTable" /* GET_TABLE */, [options], signal);
2102
- }
2103
- async getTimeSeries({
2104
- signal,
2105
- ...options
2106
- }) {
2107
- return this._executeWorkerMethod("getTimeSeries" /* GET_TIME_SERIES */, [options], signal);
2108
- }
2109
- async getRange({
2110
- signal,
2111
- ...options
2112
- }) {
2113
- return this._executeWorkerMethod("getRange" /* GET_RANGE */, [options], signal);
2114
- }
2115
- };
2116
-
2117
1927
  // src/workers/widget-tileset-worker.ts
2118
1928
  var source;
2119
1929
  addEventListener("message", (e) => {
2120
1930
  const { method, params, requestId } = e.data;
2121
1931
  if (method === "init" /* INIT */) {
2122
- source = new WidgetTilesetSource({
1932
+ source = new WidgetTilesetSourceImpl({
2123
1933
  ...params[0],
2124
1934
  widgetSourceWorker: false
2125
1935
  });
package/package.json CHANGED
@@ -8,7 +8,7 @@
8
8
  "homepage": "https://github.com/CartoDB/carto-api-client#readme",
9
9
  "author": "Don McCurdy <donmccurdy@carto.com>",
10
10
  "packageManager": "yarn@4.3.1",
11
- "version": "0.5.0-alpha.8",
11
+ "version": "0.5.0-alpha.9",
12
12
  "license": "MIT",
13
13
  "type": "module",
14
14
  "sideEffects": false,
@@ -66,10 +66,13 @@ export class WidgetTilesetSource extends WidgetSource<WidgetTilesetSourceProps>
66
66
  super(props);
67
67
 
68
68
  this._workerEnabled =
69
- (props.widgetSourceWorker ?? true) && TSUP_FORMAT !== 'cjs';
70
- this._localImpl = this._workerEnabled
71
- ? null
72
- : new WidgetTilesetSourceImpl(this.props);
69
+ (props.widgetSourceWorker ?? true) &&
70
+ TSUP_FORMAT !== 'cjs' &&
71
+ typeof Worker !== 'undefined';
72
+
73
+ if (!this._workerEnabled) {
74
+ this._localImpl = new WidgetTilesetSourceImpl(this.props);
75
+ }
73
76
  }
74
77
 
75
78
  destroy() {
@@ -89,28 +92,25 @@ export class WidgetTilesetSource extends WidgetSource<WidgetTilesetSourceProps>
89
92
  * Returns an initialized Worker, to be reused for the lifecycle of this
90
93
  * source instance.
91
94
  */
92
- protected _getWorker(): Worker | null {
93
- if (this._workerImpl || this._localImpl) {
95
+ protected _getWorker(): Worker {
96
+ if (this._workerImpl) {
94
97
  return this._workerImpl;
95
98
  }
96
99
 
97
- try {
98
- this._workerImpl = new Worker(new URL('worker.js', import.meta.url), {
100
+ this._workerImpl = new Worker(
101
+ new URL('@carto/api-client/worker', import.meta.url),
102
+ {
99
103
  type: 'module',
100
104
  name: 'cartowidgettileset',
101
- });
105
+ }
106
+ );
102
107
 
103
- this._workerImpl.postMessage({
104
- method: Method.INIT,
105
- params: [this.props],
106
- } as WorkerRequest);
108
+ this._workerImpl.postMessage({
109
+ method: Method.INIT,
110
+ params: [this.props],
111
+ } as WorkerRequest);
107
112
 
108
- return this._workerImpl;
109
- } catch {
110
- this._workerEnabled = false;
111
- this._localImpl = new WidgetTilesetSourceImpl(this.props);
112
- return null;
113
- }
113
+ return this._workerImpl;
114
114
  }
115
115
 
116
116
  /** Executes a given method on the worker. */
@@ -119,12 +119,12 @@ export class WidgetTilesetSource extends WidgetSource<WidgetTilesetSourceProps>
119
119
  params: unknown[],
120
120
  signal?: AbortSignal
121
121
  ): Promise<T> {
122
- const worker = this._getWorker();
123
- if (!worker) {
122
+ if (!this._workerEnabled) {
124
123
  // @ts-expect-error No type-checking dynamic method name.
125
124
  return this._localImpl[method](...params);
126
125
  }
127
126
 
127
+ const worker = this._getWorker();
128
128
  const requestId = this._workerNextRequestId++;
129
129
 
130
130
  // TODO: ViewState may contain non-serializable data, which we do not need.
@@ -193,11 +193,12 @@ export class WidgetTilesetSource extends WidgetSource<WidgetTilesetSourceProps>
193
193
  * before computing statistics on the tiles.
194
194
  */
195
195
  loadTiles(tiles: unknown[]) {
196
- const worker = this._getWorker();
197
- if (!worker) {
196
+ if (!this._workerEnabled) {
198
197
  return this._localImpl!.loadTiles(tiles);
199
198
  }
200
199
 
200
+ const worker = this._getWorker();
201
+
201
202
  tiles = (tiles as Tile[]).map(({id, bbox, data}) => ({
202
203
  id,
203
204
  bbox,
@@ -212,11 +213,12 @@ export class WidgetTilesetSource extends WidgetSource<WidgetTilesetSourceProps>
212
213
 
213
214
  /** Configures options used to extract features from tiles. */
214
215
  setTileFeatureExtractOptions(options: TileFeatureExtractOptions) {
215
- const worker = this._getWorker();
216
- if (!worker) {
216
+ if (!this._workerEnabled) {
217
217
  return this._localImpl?.setTileFeatureExtractOptions(options);
218
218
  }
219
219
 
220
+ const worker = this._getWorker();
221
+
220
222
  worker.postMessage({
221
223
  type: Method.SET_TILE_FEATURE_EXTRACT_OPTIONS,
222
224
  params: [options],
@@ -235,11 +237,12 @@ export class WidgetTilesetSource extends WidgetSource<WidgetTilesetSourceProps>
235
237
  geojson: FeatureCollection;
236
238
  spatialFilter: SpatialFilter;
237
239
  }) {
238
- const worker = this._getWorker();
239
- if (!worker) {
240
+ if (!this._workerEnabled) {
240
241
  return this._localImpl!.loadGeoJSON({geojson, spatialFilter});
241
242
  }
242
243
 
244
+ const worker = this._getWorker();
245
+
243
246
  worker.postMessage({
244
247
  method: Method.LOAD_GEOJSON,
245
248
  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,13 +10,13 @@ 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
21
  widgetSourceWorker: false,
24
22
  });