@epam/uui-db 4.6.1-alpha.0 → 4.6.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.
package/index.js CHANGED
@@ -483,6 +483,7 @@ var tempIds_1 = __webpack_require__(/*! ./tempIds */ "./src/tempIds.tsx");
483
483
  var utils_1 = __webpack_require__(/*! ./utils */ "./src/utils.ts");
484
484
  var lodash_isempty_1 = (0, tslib_1.__importDefault)(__webpack_require__(/*! lodash.isempty */ "lodash.isempty"));
485
485
  var Loader_1 = __webpack_require__(/*! ./Loader */ "./src/Loader.ts");
486
+ var uui_core_1 = __webpack_require__(/*! @epam/uui-core */ "@epam/uui-core");
486
487
  var SimpleLoadingTracker_1 = __webpack_require__(/*! ./SimpleLoadingTracker */ "./src/SimpleLoadingTracker.ts");
487
488
  var ListLoadingTracker_1 = __webpack_require__(/*! ./ListLoadingTracker */ "./src/ListLoadingTracker.ts");
488
489
  var DbRef = /** @class */ (function () {
@@ -492,7 +493,6 @@ var DbRef = /** @class */ (function () {
492
493
  this.log = [];
493
494
  this.autoSave = true;
494
495
  this.savedPoint = 0;
495
- this.errors = [];
496
496
  this.throttleSaveMs = 1000;
497
497
  /* Update subscriptions (aka live views) */
498
498
  this.lastSubscriptionId = 1;
@@ -501,10 +501,36 @@ var DbRef = /** @class */ (function () {
501
501
  /* Error Subscriptions */
502
502
  this.saveErrorHandlers = [];
503
503
  /* Save scheduling */
504
- this.isSaveInProgress = false;
505
- this.isSaveThrottled = false;
504
+ this.enqueueSave = (0, uui_core_1.batch)(function () { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () {
505
+ var cumulativePatch, lastLogEntry, response, error_1;
506
+ return (0, tslib_1.__generator)(this, function (_a) {
507
+ switch (_a.label) {
508
+ case 0:
509
+ cumulativePatch = this.makeCumulativePatch();
510
+ lastLogEntry = this.log.length;
511
+ if ((0, lodash_isempty_1.default)(cumulativePatch)) {
512
+ return [2 /*return*/];
513
+ }
514
+ _a.label = 1;
515
+ case 1:
516
+ _a.trys.push([1, 3, , 4]);
517
+ return [4 /*yield*/, this.savePatch(cumulativePatch)];
518
+ case 2:
519
+ response = _a.sent();
520
+ this.applyResponse(response, lastLogEntry);
521
+ this.update();
522
+ return [3 /*break*/, 4];
523
+ case 3:
524
+ error_1 = _a.sent();
525
+ this.saveError(cumulativePatch, error_1);
526
+ this.update();
527
+ throw error_1;
528
+ case 4: return [2 /*return*/];
529
+ }
530
+ });
531
+ }); });
506
532
  this.handleBeforeUnload = function (e) {
507
- if (_this.isSaveInProgress || _this.isSaveThrottled) {
533
+ if (_this.enqueueSave.isBusy) {
508
534
  e.returnValue = false;
509
535
  return false;
510
536
  }
@@ -518,8 +544,7 @@ var DbRef = /** @class */ (function () {
518
544
  }
519
545
  /** Saves all committed patches. Useful only if autoSave is off. */
520
546
  DbRef.prototype.save = function () {
521
- this.enqueueSave();
522
- this.update();
547
+ return this.enqueueSave();
523
548
  };
524
549
  DbRef.prototype.getAutoSave = function () {
525
550
  return this.autoSave;
@@ -527,13 +552,12 @@ var DbRef = /** @class */ (function () {
527
552
  DbRef.prototype.setAutoSave = function (val) {
528
553
  if (this.autoSave != val) {
529
554
  this.autoSave = val;
530
- this.enqueueSave();
555
+ this.autoSave && this.enqueueSave();
531
556
  this.update();
532
557
  }
533
558
  };
534
559
  DbRef.prototype.revert = function () {
535
560
  this.db = this.base;
536
- this.errors = [];
537
561
  this.log = this.log.slice(0, this.savedPoint);
538
562
  this.update();
539
563
  };
@@ -543,18 +567,13 @@ var DbRef = /** @class */ (function () {
543
567
  };
544
568
  /* Db Update logic */
545
569
  DbRef.prototype.commit = function (patch) {
546
- try {
547
- var dbBeforeUpdate = this.db.with(patch);
548
- patch = this.beforeUpdate(patch, dbBeforeUpdate.tables, this.db.tables);
549
- patch = (0, patchHelpers_1.mergeEntityPatches)(this.db.tables, patch);
550
- this.log.push({ patch: patch });
551
- this.db = this.db.with(patch);
552
- this.autoSave && this.enqueueSave();
553
- this.update();
554
- }
555
- catch (e) {
556
- this.saveError(patch, e);
557
- }
570
+ var dbBeforeUpdate = this.db.with(patch);
571
+ patch = this.beforeUpdate(patch, dbBeforeUpdate.tables, this.db.tables);
572
+ patch = (0, patchHelpers_1.mergeEntityPatches)(this.db.tables, patch);
573
+ this.log.push({ patch: patch });
574
+ this.db = this.db.with(patch);
575
+ this.autoSave && this.enqueueSave(null, { throttleMs: this.throttleSaveMs });
576
+ this.update();
558
577
  };
559
578
  DbRef.prototype.beforeUpdate = function (patch, tables, prevTables) {
560
579
  var _this = this;
@@ -600,24 +619,12 @@ var DbRef = /** @class */ (function () {
600
619
  return cumulativePatch;
601
620
  };
602
621
  DbRef.prototype.applyResponse = function (response, newSavedPoint) {
603
- var _this = this;
604
- if (this.errors.length == 0) {
605
- this.tempIdMap.appendServerMapping(response);
606
- var serverPatch = (0, patchHelpers_1.flattenResponse)(response.submit, this.db.tables);
607
- serverPatch = (0, patchHelpers_1.mergeEntityPatches)(this.db.tables, serverPatch);
608
- this.savedPoint = newSavedPoint;
609
- this.base = this.db;
610
- this.commitFetch(serverPatch);
611
- if (this.autoSave) {
612
- this.isSaveThrottled = true;
613
- setTimeout(function () {
614
- _this.isSaveThrottled = false;
615
- if (_this.savedPoint < _this.log.length) {
616
- _this.enqueueSave();
617
- }
618
- }, this.throttleSaveMs);
619
- }
620
- }
622
+ this.tempIdMap.appendServerMapping(response);
623
+ var serverPatch = (0, patchHelpers_1.flattenResponse)(response.submit, this.db.tables);
624
+ serverPatch = (0, patchHelpers_1.mergeEntityPatches)(this.db.tables, serverPatch);
625
+ this.savedPoint = newSavedPoint;
626
+ this.base = this.db;
627
+ this.commitFetch(serverPatch);
621
628
  };
622
629
  DbRef.prototype.subscribe = function (view, params, onUpdate) {
623
630
  var _this = this;
@@ -658,36 +665,6 @@ var DbRef = /** @class */ (function () {
658
665
  DbRef.prototype.saveError = function (request, error) {
659
666
  this.saveErrorHandlers.forEach(function (handler) { return handler(request, error); });
660
667
  };
661
- DbRef.prototype.enqueueSave = function () {
662
- var _this = this;
663
- if (!this.isSaveInProgress && !this.isSaveThrottled) {
664
- try {
665
- this.isSaveInProgress = true;
666
- var lastLogEntry_1 = this.log.length;
667
- var cumulativePatch_1 = this.makeCumulativePatch();
668
- if ((0, lodash_isempty_1.default)(cumulativePatch_1)) {
669
- this.isSaveInProgress = false;
670
- return;
671
- }
672
- this.savePatch(cumulativePatch_1)
673
- .then(function (response) {
674
- _this.applyResponse(response, lastLogEntry_1);
675
- _this.isSaveInProgress = false;
676
- _this.update();
677
- })
678
- .catch(function (reason) {
679
- _this.saveError(cumulativePatch_1, reason);
680
- _this.isSaveInProgress = false;
681
- _this.update();
682
- });
683
- }
684
- catch (e) {
685
- this.isSaveInProgress = false;
686
- this.isSaveThrottled = false;
687
- throw e;
688
- }
689
- }
690
- };
691
668
  DbRef.prototype.makeLoader = function (options) {
692
669
  var loader = new Loader_1.Loader(this, function () { return new SimpleLoadingTracker_1.SimpleLoadingTracker(); }, options);
693
670
  this.loaders.push(loader);
@@ -770,14 +747,22 @@ var DbTable = /** @class */ (function () {
770
747
  var id = _this.getId(entityPatch);
771
748
  var immId = _this.keyToImmutable(id);
772
749
  var existing = _this.state.pk.get(immId);
750
+ var isDeleted = false;
773
751
  var updated = entityPatch;
774
752
  if (existing) {
775
753
  updated = (0, tslib_1.__assign)((0, tslib_1.__assign)({}, existing), entityPatch);
754
+ if (_this.schema.deleteFlag) {
755
+ isDeleted = updated[_this.schema.deleteFlag] === true;
756
+ }
776
757
  }
777
- return { id: id, immId: immId, patch: entityPatch, existing: existing, updated: updated, updatedFieldsisNew: !existing };
758
+ return { id: id, immId: immId, patch: entityPatch, existing: existing, updated: updated, isNew: !existing, isDeleted: isDeleted };
778
759
  });
779
760
  var idVal = immutable_1.Seq.Keyed(updates.map(function (entity) { return [entity.immId, entity.updated]; }));
780
761
  var newPk = this.state.pk.merge(idVal);
762
+ // TBD: replace with deleteAll after migrating to immutable 4
763
+ updates.filter(function (u) { return u.isDeleted; }).forEach(function (u) {
764
+ newPk = newPk.delete(u.immId);
765
+ });
781
766
  var newIndexes = this.state.indexes.map(function (index) {
782
767
  var newMap = index.map;
783
768
  var _loop_1 = function (n) {
@@ -785,11 +770,13 @@ var DbTable = /** @class */ (function () {
785
770
  var newKey = update.updated[index.field];
786
771
  if (update.existing) {
787
772
  var oldKey = update.existing[index.field];
788
- if (oldKey !== newKey) {
773
+ if (oldKey !== newKey || update.isDeleted) {
789
774
  newMap = newMap.update(oldKey, function (set) { return set.remove(update.immId); });
790
775
  }
791
776
  }
792
- newMap = newMap.update(newKey, I.Set(), function (set) { return set.add(update.immId); });
777
+ if (!update.isDeleted) {
778
+ newMap = newMap.update(newKey, I.Set(), function (set) { return set.add(update.immId); });
779
+ }
793
780
  };
794
781
  for (var n = 0; n < updates.length; n++) {
795
782
  _loop_1(n);