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

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.
@@ -433,7 +433,6 @@ __export(src_exports, {
433
433
  WidgetSource: () => WidgetSource,
434
434
  WidgetTableSource: () => WidgetTableSource,
435
435
  WidgetTilesetSource: () => WidgetTilesetSource,
436
- WidgetTilesetWorkerSource: () => WidgetTilesetWorkerSource,
437
436
  _buildFeatureFilter: () => _buildFeatureFilter,
438
437
  _getHexagonResolution: () => _getHexagonResolution,
439
438
  addFilter: () => addFilter,
@@ -2105,6 +2104,15 @@ var _WidgetSource = class _WidgetSource {
2105
2104
  __publicField(this, "props");
2106
2105
  this.props = { ..._WidgetSource.defaultProps, ...props };
2107
2106
  }
2107
+ /**
2108
+ * Destroys the widget source and releases allocated resources.
2109
+ *
2110
+ * For remote sources (tables, queries) this has no effect, but for local
2111
+ * sources (tilesets, rasters) these resources will affect performance
2112
+ * and stability if many (10+) sources are created and not released.
2113
+ */
2114
+ destroy() {
2115
+ }
2108
2116
  _getSpatialFiltersResolution(source, spatialFilter, referenceViewState) {
2109
2117
  if (!spatialFilter || source.spatialDataType === "geo") {
2110
2118
  return;
@@ -2180,7 +2188,7 @@ async function makeCall({
2180
2188
  method: opts?.method,
2181
2189
  body: opts?.body
2182
2190
  },
2183
- signal: opts?.abortController?.signal,
2191
+ signal: opts?.signal,
2184
2192
  ...opts?.otherOptions
2185
2193
  });
2186
2194
  data = await response.json();
@@ -2315,12 +2323,12 @@ var WidgetRemoteSource = class extends WidgetSource {
2315
2323
  }
2316
2324
  async getCategories(options) {
2317
2325
  const {
2326
+ signal,
2318
2327
  filters = this.props.filters,
2319
2328
  filterOwner,
2320
2329
  spatialFilter,
2321
2330
  spatialFiltersMode,
2322
2331
  spatialIndexReferenceViewState,
2323
- abortController,
2324
2332
  ...params
2325
2333
  } = options;
2326
2334
  const { column, operation, operationColumn } = params;
@@ -2343,17 +2351,17 @@ var WidgetRemoteSource = class extends WidgetSource {
2343
2351
  operation,
2344
2352
  operationColumn: operationColumn || column
2345
2353
  },
2346
- opts: { abortController, headers: this._headers }
2354
+ opts: { signal, headers: this._headers }
2347
2355
  }).then((res) => normalizeObjectKeys(res.rows));
2348
2356
  }
2349
2357
  async getFeatures(options) {
2350
2358
  const {
2359
+ signal,
2351
2360
  filters = this.props.filters,
2352
2361
  filterOwner,
2353
2362
  spatialFilter,
2354
2363
  spatialFiltersMode,
2355
2364
  spatialIndexReferenceViewState,
2356
- abortController,
2357
2365
  ...params
2358
2366
  } = options;
2359
2367
  const { columns, dataType, featureIds, z, limit, tileResolution } = params;
@@ -2379,18 +2387,18 @@ var WidgetRemoteSource = class extends WidgetSource {
2379
2387
  limit: limit || 1e3,
2380
2388
  tileResolution: tileResolution || DEFAULT_TILE_RESOLUTION
2381
2389
  },
2382
- opts: { abortController, headers: this._headers }
2390
+ opts: { signal, headers: this._headers }
2383
2391
  // Avoid `normalizeObjectKeys()`, which changes column names.
2384
2392
  }).then(({ rows }) => ({ rows }));
2385
2393
  }
2386
2394
  async getFormula(options) {
2387
2395
  const {
2396
+ signal,
2388
2397
  filters = this.props.filters,
2389
2398
  filterOwner,
2390
2399
  spatialFilter,
2391
2400
  spatialFiltersMode,
2392
2401
  spatialIndexReferenceViewState,
2393
- abortController,
2394
2402
  operationExp,
2395
2403
  ...params
2396
2404
  } = options;
@@ -2414,17 +2422,17 @@ var WidgetRemoteSource = class extends WidgetSource {
2414
2422
  operation: operation ?? "count",
2415
2423
  operationExp
2416
2424
  },
2417
- opts: { abortController, headers: this._headers }
2425
+ opts: { signal, headers: this._headers }
2418
2426
  }).then((res) => normalizeObjectKeys(res.rows[0]));
2419
2427
  }
2420
2428
  async getHistogram(options) {
2421
2429
  const {
2430
+ signal,
2422
2431
  filters = this.props.filters,
2423
2432
  filterOwner,
2424
2433
  spatialFilter,
2425
2434
  spatialFiltersMode,
2426
2435
  spatialIndexReferenceViewState,
2427
- abortController,
2428
2436
  ...params
2429
2437
  } = options;
2430
2438
  const { column, operation, ticks } = params;
@@ -2443,7 +2451,7 @@ var WidgetRemoteSource = class extends WidgetSource {
2443
2451
  spatialFilter
2444
2452
  },
2445
2453
  params: { column, operation, ticks },
2446
- opts: { abortController, headers: this._headers }
2454
+ opts: { signal, headers: this._headers }
2447
2455
  }).then((res) => normalizeObjectKeys(res.rows));
2448
2456
  if (data.length) {
2449
2457
  const result = Array(ticks.length + 1).fill(0);
@@ -2456,12 +2464,12 @@ var WidgetRemoteSource = class extends WidgetSource {
2456
2464
  }
2457
2465
  async getRange(options) {
2458
2466
  const {
2467
+ signal,
2459
2468
  filters = this.props.filters,
2460
2469
  filterOwner,
2461
2470
  spatialFilter,
2462
2471
  spatialFiltersMode,
2463
2472
  spatialIndexReferenceViewState,
2464
- abortController,
2465
2473
  ...params
2466
2474
  } = options;
2467
2475
  const { column } = params;
@@ -2480,17 +2488,17 @@ var WidgetRemoteSource = class extends WidgetSource {
2480
2488
  spatialFilter
2481
2489
  },
2482
2490
  params: { column },
2483
- opts: { abortController, headers: this._headers }
2491
+ opts: { signal, headers: this._headers }
2484
2492
  }).then((res) => normalizeObjectKeys(res.rows[0]));
2485
2493
  }
2486
2494
  async getScatter(options) {
2487
2495
  const {
2496
+ signal,
2488
2497
  filters = this.props.filters,
2489
2498
  filterOwner,
2490
2499
  spatialFilter,
2491
2500
  spatialFiltersMode,
2492
2501
  spatialIndexReferenceViewState,
2493
- abortController,
2494
2502
  ...params
2495
2503
  } = options;
2496
2504
  const { xAxisColumn, xAxisJoinOperation, yAxisColumn, yAxisJoinOperation } = params;
@@ -2516,17 +2524,17 @@ var WidgetRemoteSource = class extends WidgetSource {
2516
2524
  yAxisJoinOperation,
2517
2525
  limit: HARD_LIMIT
2518
2526
  },
2519
- opts: { abortController, headers: this._headers }
2527
+ opts: { signal, headers: this._headers }
2520
2528
  }).then((res) => normalizeObjectKeys(res.rows)).then((res) => res.map(({ x, y }) => [x, y]));
2521
2529
  }
2522
2530
  async getTable(options) {
2523
2531
  const {
2532
+ signal,
2524
2533
  filters = this.props.filters,
2525
2534
  filterOwner,
2526
2535
  spatialFilter,
2527
2536
  spatialFiltersMode,
2528
2537
  spatialIndexReferenceViewState,
2529
- abortController,
2530
2538
  ...params
2531
2539
  } = options;
2532
2540
  const { columns, sortBy, sortDirection, offset = 0, limit = 10 } = params;
@@ -2551,7 +2559,7 @@ var WidgetRemoteSource = class extends WidgetSource {
2551
2559
  limit,
2552
2560
  offset
2553
2561
  },
2554
- opts: { abortController, headers: this._headers }
2562
+ opts: { signal, headers: this._headers }
2555
2563
  }).then((res) => ({
2556
2564
  // Avoid `normalizeObjectKeys()`, which changes column names.
2557
2565
  rows: res.rows ?? res.ROWS,
@@ -2560,9 +2568,9 @@ var WidgetRemoteSource = class extends WidgetSource {
2560
2568
  }
2561
2569
  async getTimeSeries(options) {
2562
2570
  const {
2571
+ signal,
2563
2572
  filters = this.props.filters,
2564
2573
  filterOwner,
2565
- abortController,
2566
2574
  spatialFilter,
2567
2575
  spatialFiltersMode,
2568
2576
  spatialIndexReferenceViewState,
@@ -2604,7 +2612,7 @@ var WidgetRemoteSource = class extends WidgetSource {
2604
2612
  splitByCategoryLimit,
2605
2613
  splitByCategoryValues
2606
2614
  },
2607
- opts: { abortController, headers: this._headers }
2615
+ opts: { signal, headers: this._headers }
2608
2616
  }).then((res) => ({
2609
2617
  rows: normalizeObjectKeys(res.rows),
2610
2618
  categories: res.metadata?.categories
@@ -2639,6 +2647,9 @@ var WidgetTableSource = class extends WidgetRemoteSource {
2639
2647
  // src/widget-sources/widget-tileset-source.ts
2640
2648
  init_cjs_shims();
2641
2649
 
2650
+ // src/widget-sources/widget-tileset-source-impl.ts
2651
+ init_cjs_shims();
2652
+
2642
2653
  // src/operations/aggregation.ts
2643
2654
  init_cjs_shims();
2644
2655
  var aggregationFunctions = {
@@ -2987,9 +2998,9 @@ function scatterPlot({
2987
2998
  );
2988
2999
  }
2989
3000
 
2990
- // src/widget-sources/widget-tileset-source.ts
3001
+ // src/widget-sources/widget-tileset-source-impl.ts
2991
3002
  var import_boolean_equal = require("@turf/boolean-equal");
2992
- var WidgetTilesetSource = class extends WidgetSource {
3003
+ var WidgetTilesetSourceImpl = class extends WidgetSource {
2993
3004
  constructor() {
2994
3005
  super(...arguments);
2995
3006
  __publicField(this, "_tiles", []);
@@ -3274,40 +3285,56 @@ function normalizeColumns(columns) {
3274
3285
  return Array.isArray(columns) ? columns : typeof columns === "string" ? [columns] : [];
3275
3286
  }
3276
3287
 
3277
- // src/widget-sources/widget-tileset-worker-source.ts
3278
- init_cjs_shims();
3279
- var WidgetTilesetWorkerSource = class extends WidgetSource {
3288
+ // src/widget-sources/widget-tileset-source.ts
3289
+ var WidgetTilesetSource = class extends WidgetSource {
3280
3290
  constructor(props) {
3281
3291
  super(props);
3282
- /////////////////////////////////////////////////////////////////////////////
3283
- // WEB WORKER MANAGEMENT
3284
- __publicField(this, "_worker", null);
3292
+ __publicField(this, "_localImpl", null);
3293
+ __publicField(this, "_workerImpl", null);
3294
+ __publicField(this, "_workerEnabled");
3285
3295
  __publicField(this, "_workerNextRequestId", 1);
3296
+ this._workerEnabled = (props.widgetSourceWorker ?? true) && false;
3297
+ this._localImpl = this._workerEnabled ? null : new WidgetTilesetSourceImpl(this.props);
3298
+ }
3299
+ destroy() {
3300
+ this._localImpl?.destroy();
3301
+ this._localImpl = null;
3302
+ this._workerImpl?.terminate();
3303
+ this._workerImpl = null;
3304
+ super.destroy();
3286
3305
  }
3306
+ /////////////////////////////////////////////////////////////////////////////
3307
+ // WEB WORKER MANAGEMENT
3287
3308
  /**
3288
3309
  * Returns an initialized Worker, to be reused for the lifecycle of this
3289
3310
  * source instance.
3290
3311
  */
3291
3312
  _getWorker() {
3292
- if (this._worker) {
3293
- return this._worker;
3313
+ if (this._workerImpl || this._localImpl) {
3314
+ return this._workerImpl;
3294
3315
  }
3295
- this._worker = new Worker(
3296
- new URL("@carto/api-client/worker", importMetaUrl),
3297
- {
3316
+ try {
3317
+ this._workerImpl = new Worker(new URL("worker.js", importMetaUrl), {
3298
3318
  type: "module",
3299
3319
  name: "cartowidgettileset"
3300
- }
3301
- );
3302
- this._worker.postMessage({
3303
- method: "init" /* INIT */,
3304
- params: [this.props]
3305
- });
3306
- return this._worker;
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
+ }
3307
3331
  }
3308
3332
  /** Executes a given method on the worker. */
3309
3333
  _executeWorkerMethod(method, params, signal) {
3310
3334
  const worker = this._getWorker();
3335
+ if (!worker) {
3336
+ return this._localImpl[method](...params);
3337
+ }
3311
3338
  const requestId = this._workerNextRequestId++;
3312
3339
  const options = params[0];
3313
3340
  if (options?.spatialIndexReferenceViewState) {
@@ -3355,19 +3382,27 @@ var WidgetTilesetWorkerSource = class extends WidgetSource {
3355
3382
  * before computing statistics on the tiles.
3356
3383
  */
3357
3384
  loadTiles(tiles2) {
3385
+ const worker = this._getWorker();
3386
+ if (!worker) {
3387
+ return this._localImpl.loadTiles(tiles2);
3388
+ }
3358
3389
  tiles2 = tiles2.map(({ id, bbox, data }) => ({
3359
3390
  id,
3360
3391
  bbox,
3361
3392
  data
3362
3393
  }));
3363
- this._getWorker().postMessage({
3394
+ worker.postMessage({
3364
3395
  method: "loadTiles" /* LOAD_TILES */,
3365
3396
  params: [tiles2]
3366
3397
  });
3367
3398
  }
3368
3399
  /** Configures options used to extract features from tiles. */
3369
3400
  setTileFeatureExtractOptions(options) {
3370
- this._getWorker().postMessage({
3401
+ const worker = this._getWorker();
3402
+ if (!worker) {
3403
+ return this._localImpl?.setTileFeatureExtractOptions(options);
3404
+ }
3405
+ worker.postMessage({
3371
3406
  type: "setTileFeatureExtractOptions" /* SET_TILE_FEATURE_EXTRACT_OPTIONS */,
3372
3407
  params: [options]
3373
3408
  });
@@ -3381,7 +3416,11 @@ var WidgetTilesetWorkerSource = class extends WidgetSource {
3381
3416
  geojson,
3382
3417
  spatialFilter
3383
3418
  }) {
3384
- this._getWorker().postMessage({
3419
+ const worker = this._getWorker();
3420
+ if (!worker) {
3421
+ return this._localImpl.loadGeoJSON({ geojson, spatialFilter });
3422
+ }
3423
+ worker.postMessage({
3385
3424
  method: "loadGeoJSON" /* LOAD_GEOJSON */,
3386
3425
  params: [{ geojson, spatialFilter }]
3387
3426
  });
@@ -3393,74 +3432,46 @@ var WidgetTilesetWorkerSource = class extends WidgetSource {
3393
3432
  throw new Error("getFeatures not supported for tilesets");
3394
3433
  }
3395
3434
  async getFormula({
3396
- abortController,
3435
+ signal,
3397
3436
  ...options
3398
3437
  }) {
3399
- return this._executeWorkerMethod(
3400
- "getFormula" /* GET_FORMULA */,
3401
- [options],
3402
- abortController?.signal
3403
- );
3438
+ return this._executeWorkerMethod("getFormula" /* GET_FORMULA */, [options], signal);
3404
3439
  }
3405
3440
  async getHistogram({
3406
- abortController,
3441
+ signal,
3407
3442
  ...options
3408
3443
  }) {
3409
- return this._executeWorkerMethod(
3410
- "getHistogram" /* GET_HISTOGRAM */,
3411
- [options],
3412
- abortController?.signal
3413
- );
3444
+ return this._executeWorkerMethod("getHistogram" /* GET_HISTOGRAM */, [options], signal);
3414
3445
  }
3415
3446
  async getCategories({
3416
- abortController,
3447
+ signal,
3417
3448
  ...options
3418
3449
  }) {
3419
- return this._executeWorkerMethod(
3420
- "getCategories" /* GET_CATEGORIES */,
3421
- [options],
3422
- abortController?.signal
3423
- );
3450
+ return this._executeWorkerMethod("getCategories" /* GET_CATEGORIES */, [options], signal);
3424
3451
  }
3425
3452
  async getScatter({
3426
- abortController,
3453
+ signal,
3427
3454
  ...options
3428
3455
  }) {
3429
- return this._executeWorkerMethod(
3430
- "getScatter" /* GET_SCATTER */,
3431
- [options],
3432
- abortController?.signal
3433
- );
3456
+ return this._executeWorkerMethod("getScatter" /* GET_SCATTER */, [options], signal);
3434
3457
  }
3435
3458
  async getTable({
3436
- abortController,
3459
+ signal,
3437
3460
  ...options
3438
3461
  }) {
3439
- return this._executeWorkerMethod(
3440
- "getTable" /* GET_TABLE */,
3441
- [options],
3442
- abortController?.signal
3443
- );
3462
+ return this._executeWorkerMethod("getTable" /* GET_TABLE */, [options], signal);
3444
3463
  }
3445
3464
  async getTimeSeries({
3446
- abortController,
3465
+ signal,
3447
3466
  ...options
3448
3467
  }) {
3449
- return this._executeWorkerMethod(
3450
- "getTimeSeries" /* GET_TIME_SERIES */,
3451
- [options],
3452
- abortController?.signal
3453
- );
3468
+ return this._executeWorkerMethod("getTimeSeries" /* GET_TIME_SERIES */, [options], signal);
3454
3469
  }
3455
3470
  async getRange({
3456
- abortController,
3471
+ signal,
3457
3472
  ...options
3458
3473
  }) {
3459
- return this._executeWorkerMethod(
3460
- "getRange" /* GET_RANGE */,
3461
- [options],
3462
- abortController?.signal
3463
- );
3474
+ return this._executeWorkerMethod("getRange" /* GET_RANGE */, [options], signal);
3464
3475
  }
3465
3476
  };
3466
3477
 
@@ -3549,36 +3560,14 @@ function getTileFormat(tilejson) {
3549
3560
  return tileParams.get("formatTiles") === "mvt" ? "mvt" /* MVT */ : "binary" /* BINARY */;
3550
3561
  }
3551
3562
 
3552
- // src/workers/utils.ts
3553
- init_cjs_shims();
3554
- var _isModuleWorkerSupported = true ? false : null;
3555
- function isModuleWorkerSupported() {
3556
- if (_isModuleWorkerSupported !== null) {
3557
- return _isModuleWorkerSupported;
3558
- }
3559
- try {
3560
- new Worker("blob://", {
3561
- get type() {
3562
- _isModuleWorkerSupported = true;
3563
- return "module";
3564
- }
3565
- });
3566
- } catch {
3567
- } finally {
3568
- _isModuleWorkerSupported || (_isModuleWorkerSupported = false);
3569
- }
3570
- return _isModuleWorkerSupported;
3571
- }
3572
-
3573
3563
  // src/sources/h3-tileset-source.ts
3574
3564
  var h3TilesetSource = async function(options) {
3575
3565
  const { tableName, spatialDataColumn = "h3" } = options;
3576
3566
  const urlParameters = { name: tableName };
3577
- const WidgetSourceClass = options.widgetSourceWorker !== false && isModuleWorkerSupported() ? WidgetTilesetWorkerSource : WidgetTilesetSource;
3578
3567
  return baseSource("tileset", options, urlParameters).then(
3579
3568
  (result) => ({
3580
3569
  ...result,
3581
- widgetSource: new WidgetSourceClass({
3570
+ widgetSource: new WidgetTilesetSource({
3582
3571
  ...options,
3583
3572
  tileFormat: getTileFormat(result),
3584
3573
  spatialDataColumn,
@@ -3684,11 +3673,10 @@ init_cjs_shims();
3684
3673
  var quadbinTilesetSource = async function(options) {
3685
3674
  const { tableName, spatialDataColumn = "quadbin" } = options;
3686
3675
  const urlParameters = { name: tableName };
3687
- const WidgetSourceClass = options.widgetSourceWorker !== false && isModuleWorkerSupported() ? WidgetTilesetWorkerSource : WidgetTilesetSource;
3688
3676
  return baseSource("tileset", options, urlParameters).then(
3689
3677
  (result) => ({
3690
3678
  ...result,
3691
- widgetSource: new WidgetSourceClass({
3679
+ widgetSource: new WidgetTilesetSource({
3692
3680
  ...options,
3693
3681
  tileFormat: getTileFormat(result),
3694
3682
  spatialDataColumn,
@@ -3789,11 +3777,10 @@ init_cjs_shims();
3789
3777
  var vectorTilesetSource = async function(options) {
3790
3778
  const { tableName, spatialDataColumn = DEFAULT_GEO_COLUMN } = options;
3791
3779
  const urlParameters = { name: tableName };
3792
- const WidgetSourceClass = options.widgetSourceWorker !== false && isModuleWorkerSupported() ? WidgetTilesetWorkerSource : WidgetTilesetSource;
3793
3780
  return baseSource("tileset", options, urlParameters).then(
3794
3781
  (result) => ({
3795
3782
  ...result,
3796
- widgetSource: new WidgetSourceClass({
3783
+ widgetSource: new WidgetTilesetSource({
3797
3784
  ...options,
3798
3785
  tileFormat: getTileFormat(result),
3799
3786
  spatialDataColumn,
@@ -3859,7 +3846,6 @@ var query = async function(options) {
3859
3846
  WidgetSource,
3860
3847
  WidgetTableSource,
3861
3848
  WidgetTilesetSource,
3862
- WidgetTilesetWorkerSource,
3863
3849
  _buildFeatureFilter,
3864
3850
  _getHexagonResolution,
3865
3851
  addFilter,