@overmap-ai/core 1.0.58-asset-description.6 → 1.0.58-asset-description.7

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.
@@ -5378,6 +5378,16 @@ class CategoryService extends BaseApiService {
5378
5378
  store.dispatch(setCategories(result));
5379
5379
  }
5380
5380
  }
5381
+ function chunkArray(arr, chunkSize) {
5382
+ const chunks = [];
5383
+ let index2 = 0;
5384
+ const arrLength = arr.length;
5385
+ while (index2 < arrLength) {
5386
+ chunks.push(arr.slice(index2, index2 += chunkSize));
5387
+ }
5388
+ return chunks;
5389
+ }
5390
+ const BULK_ADD_ASSET_BATCH_SIZE = 1e3;
5381
5391
  class AssetService extends BaseApiService {
5382
5392
  // Basic CRUD functions
5383
5393
  add(asset, workspaceId) {
@@ -5454,36 +5464,49 @@ class AssetService extends BaseApiService {
5454
5464
  throw err;
5455
5465
  });
5456
5466
  }
5457
- async addBatch(assetsToCreate, workspaceId, assetTypeId) {
5467
+ addBulk(assetsToCreate, workspaceId, assetTypeId) {
5468
+ const { store } = this.client;
5458
5469
  const fullAssets = assetsToCreate.map((asset) => {
5459
5470
  return { ...offline(asset), submitted_at: (/* @__PURE__ */ new Date()).toISOString() };
5460
5471
  });
5461
- const { store } = this.client;
5462
- store.dispatch(addAssetsInBatches(fullAssets));
5463
- const promise = this.client.enqueueRequest({
5464
- description: "Batch create assets",
5465
- method: HttpMethod.POST,
5466
- url: `/assets/types/${assetTypeId}/add-assets/`,
5467
- queryParams: {
5468
- workspace_id: workspaceId.toString()
5469
- },
5470
- payload: {
5471
- assets: fullAssets
5472
- },
5473
- blockers: [assetTypeId],
5474
- blocks: fullAssets.map((c) => c.offline_id)
5475
- });
5476
- void promise.then((result) => {
5477
- for (const assets of Object.values(result)) {
5478
- store.dispatch(updateAsset(assets));
5479
- }
5480
- }).catch((e) => {
5481
- for (const asset of fullAssets) {
5482
- store.dispatch(removeAsset(asset.offline_id));
5483
- }
5484
- throw e;
5472
+ const assetBatches = chunkArray(fullAssets, BULK_ADD_ASSET_BATCH_SIZE).map((assetBatch) => {
5473
+ return {
5474
+ batchId: v4(),
5475
+ payload: {
5476
+ assets: assetBatch
5477
+ }
5478
+ };
5485
5479
  });
5486
- return promise;
5480
+ store.dispatch(addAssetsInBatches(fullAssets));
5481
+ let prevBatchId = null;
5482
+ const batchPromises = Promise.all(
5483
+ assetBatches.map((assetBatch) => {
5484
+ const { batchId, payload } = assetBatch;
5485
+ const promise = this.client.enqueueRequest({
5486
+ description: "Batch create assets",
5487
+ method: HttpMethod.POST,
5488
+ url: `/assets/types/${assetTypeId}/add-assets/`,
5489
+ queryParams: {
5490
+ workspace_id: workspaceId.toString()
5491
+ },
5492
+ payload,
5493
+ blockers: prevBatchId ? [assetTypeId, prevBatchId] : [assetTypeId],
5494
+ blocks: assetBatch.payload.assets.map((c) => c.offline_id).concat([batchId])
5495
+ });
5496
+ promise.then((result) => {
5497
+ for (const assets of Object.values(result)) {
5498
+ store.dispatch(updateAsset(assets));
5499
+ }
5500
+ }).catch(() => {
5501
+ for (const asset of assetBatch.payload.assets) {
5502
+ store.dispatch(removeAsset(asset.offline_id));
5503
+ }
5504
+ });
5505
+ prevBatchId = assetBatch.batchId;
5506
+ return promise;
5507
+ })
5508
+ );
5509
+ return [fullAssets, batchPromises.then((result) => result)];
5487
5510
  }
5488
5511
  async refreshStore() {
5489
5512
  const { store } = this.client;
@@ -7249,15 +7272,6 @@ class UserFormService extends BaseApiService {
7249
7272
  store.dispatch(setFormRevisionAttachments(Object.values(result.attachments)));
7250
7273
  }
7251
7274
  }
7252
- function chunkArray(arr, chunkSize) {
7253
- const chunks = [];
7254
- let index2 = 0;
7255
- const arrLength = arr.length;
7256
- while (index2 < arrLength) {
7257
- chunks.push(arr.slice(index2, index2 += chunkSize));
7258
- }
7259
- return chunks;
7260
- }
7261
7275
  const isArrayOfFiles = (value) => {
7262
7276
  return Array.isArray(value) && value[0] instanceof File;
7263
7277
  };