@sumaris-net/ngx-components 1.16.2 → 1.16.3

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.
@@ -20308,53 +20308,59 @@
20308
20308
  .then(function () {
20309
20309
  if (opts === null || opts === void 0 ? void 0 : opts.maxProgression)
20310
20310
  progression.next(opts.maxProgression);
20311
- if (opts === null || opts === void 0 ? void 0 : opts.progression)
20312
- opts.progression.complete();
20311
+ progression.complete();
20313
20312
  })
20314
20313
  .catch(function (err) { return progression.error(err); });
20315
20314
  return progression;
20316
20315
  };
20317
20316
  JobUtils.fetchAllPages = function (loadPageFn, opts) {
20318
- var _a;
20319
20317
  return __awaiter(this, void 0, void 0, function () {
20320
- var maxProgression, total, pageCount, progressionStep, currentPageDataCount, fetchSize, offset, data, res, actualProgression, lastProgressionStep;
20321
- return __generator(this, function (_b) {
20322
- switch (_b.label) {
20318
+ var maxProgression, progression, total, pageCount, progressionStep, currentPageDataCount, fetchSize, offset, data, res;
20319
+ return __generator(this, function (_a) {
20320
+ switch (_a.label) {
20323
20321
  case 0:
20324
- maxProgression = opts && opts.maxProgression || undefined;
20325
- fetchSize = opts && opts.fetchSize || 1000;
20322
+ maxProgression = toNumber(opts === null || opts === void 0 ? void 0 : opts.maxProgression, 100);
20323
+ progression = opts === null || opts === void 0 ? void 0 : opts.progression;
20324
+ fetchSize = toNumber(opts === null || opts === void 0 ? void 0 : opts.fetchSize, 1000);
20325
+ if (fetchSize <= 0)
20326
+ throw new Error('Unable to fetch pages: invalid fetchSize: ' + fetchSize);
20326
20327
  offset = 0;
20327
20328
  data = [];
20328
- _b.label = 1;
20329
+ _a.label = 1;
20329
20330
  case 1:
20330
20331
  if (opts && opts.logPrefix)
20331
20332
  console.debug(opts.logPrefix + " Fetching page #" + offset / fetchSize + "...");
20332
20333
  return [4 /*yield*/, loadPageFn(offset, fetchSize)];
20333
20334
  case 2:
20334
- res = _b.sent();
20335
+ res = _a.sent();
20336
+ // Concat to previous result
20335
20337
  data = data.concat(res.data);
20338
+ // Compute variables, for next loop
20336
20339
  currentPageDataCount = res.data && res.data.length || 0;
20337
20340
  offset += currentPageDataCount;
20338
- // Set total count (only if not already set)
20341
+ // Set total count (only once)
20339
20342
  if (isNil(total) && isNotNil(res.total)) {
20340
20343
  total = res.total;
20341
20344
  // Compute the number of page
20342
20345
  pageCount = Math.round(res.total / fetchSize + 0.5); // Round to HALF_UP integer
20343
- actualProgression = Math.max(0, toNumber(opts && ((_a = opts.progression) === null || _a === void 0 ? void 0 : _a.value), 0));
20344
- progressionStep = maxProgression
20345
- ? (maxProgression - actualProgression) / pageCount
20346
- : undefined /*if 0 reset to undefined*/;
20346
+ // Compute step, by dividing maxProgression by number of page
20347
+ if (progression) {
20348
+ progressionStep = maxProgression / pageCount;
20349
+ // DEBUG
20350
+ if (opts && opts.logPrefix)
20351
+ console.debug(opts.logPrefix + " {pageCount: " + pageCount + ", progression: " + progression.value + ", pageProgressionStep: " + progressionStep + "}");
20352
+ }
20347
20353
  }
20348
20354
  // Increment progression on each iteration (if possible)
20349
- if (opts && opts.progression && isNotNil(progressionStep)) {
20350
- opts.progression.next(opts.progression.value + progressionStep);
20355
+ if (isNotNil(progressionStep)) {
20356
+ progression.next(progression.value + progressionStep);
20351
20357
  }
20352
- if (opts && opts.onPageLoaded)
20358
+ if (opts === null || opts === void 0 ? void 0 : opts.onPageLoaded)
20353
20359
  opts.onPageLoaded(res);
20354
- _b.label = 3;
20360
+ _a.label = 3;
20355
20361
  case 3:
20356
20362
  if (currentPageDataCount > 0 && ((isNil(total) && currentPageDataCount === fetchSize) || (isNotNil(total) && (offset < total)))) return [3 /*break*/, 1];
20357
- _b.label = 4;
20363
+ _a.label = 4;
20358
20364
  case 4:
20359
20365
  // Complete progression to reach progressionStep value (because of round)
20360
20366
  if (isNotNil(progressionStep)) {
@@ -20364,14 +20370,19 @@
20364
20370
  total = data.length;
20365
20371
  pageCount = Math.round(data.length / fetchSize + 0.5);
20366
20372
  }
20367
- lastProgressionStep = opts.maxProgression - (progressionStep * pageCount);
20368
- if (opts && opts.progression && lastProgressionStep > 0) {
20369
- opts.progression.next(opts.progression.value + lastProgressionStep);
20370
- }
20373
+ // Compute final increment, using the real page count
20374
+ progressionStep = Math.max(0, maxProgression - (progressionStep * pageCount));
20375
+ }
20376
+ // Or increment once (e.g. when total is not known, in received LoadResult)
20377
+ else if (progression) {
20378
+ progressionStep = maxProgression;
20371
20379
  }
20372
- // Or increment once
20373
- else if (opts && opts.progression && opts.maxProgression) {
20374
- opts.progression.next(opts.maxProgression);
20380
+ // DEBUG
20381
+ if (opts && opts.logPrefix)
20382
+ console.debug(opts.logPrefix + " All pages fetched - {progression: " + progression.value + ", lastProgressionStep: " + progressionStep);
20383
+ // Final increment
20384
+ if (progression && progressionStep > 0) {
20385
+ progression.next(progression.value + progressionStep);
20375
20386
  }
20376
20387
  return [2 /*return*/, {
20377
20388
  data: data,