@epam/uui-db 4.6.2 → 4.8.0-alpha.0

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
@@ -91,7 +91,7 @@ module.exports =
91
91
  /*!******************************************!*\
92
92
  !*** ../node_modules/tslib/tslib.es6.js ***!
93
93
  \******************************************/
94
- /*! exports provided: __extends, __assign, __rest, __decorate, __param, __metadata, __awaiter, __generator, __createBinding, __exportStar, __values, __read, __spread, __spreadArrays, __spreadArray, __await, __asyncGenerator, __asyncDelegator, __asyncValues, __makeTemplateObject, __importStar, __importDefault, __classPrivateFieldGet, __classPrivateFieldSet */
94
+ /*! exports provided: __extends, __assign, __rest, __decorate, __param, __metadata, __awaiter, __generator, __createBinding, __exportStar, __values, __read, __spread, __spreadArrays, __spreadArray, __await, __asyncGenerator, __asyncDelegator, __asyncValues, __makeTemplateObject, __importStar, __importDefault, __classPrivateFieldGet, __classPrivateFieldSet, __classPrivateFieldIn */
95
95
  /***/ (function(module, __webpack_exports__, __webpack_require__) {
96
96
 
97
97
  "use strict";
@@ -120,7 +120,8 @@ __webpack_require__.r(__webpack_exports__);
120
120
  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "__importDefault", function() { return __importDefault; });
121
121
  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "__classPrivateFieldGet", function() { return __classPrivateFieldGet; });
122
122
  /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "__classPrivateFieldSet", function() { return __classPrivateFieldSet; });
123
- /*! *****************************************************************************
123
+ /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "__classPrivateFieldIn", function() { return __classPrivateFieldIn; });
124
+ /******************************************************************************
124
125
  Copyright (c) Microsoft Corporation.
125
126
 
126
127
  Permission to use, copy, modify, and/or distribute this software for any
@@ -229,7 +230,11 @@ function __generator(thisArg, body) {
229
230
 
230
231
  var __createBinding = Object.create ? (function(o, m, k, k2) {
231
232
  if (k2 === undefined) k2 = k;
232
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
233
+ var desc = Object.getOwnPropertyDescriptor(m, k);
234
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
235
+ desc = { enumerable: true, get: function() { return m[k]; } };
236
+ }
237
+ Object.defineProperty(o, k2, desc);
233
238
  }) : (function(o, m, k, k2) {
234
239
  if (k2 === undefined) k2 = k;
235
240
  o[k2] = m[k];
@@ -359,6 +364,11 @@ function __classPrivateFieldSet(receiver, state, value, kind, f) {
359
364
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
360
365
  return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
361
366
  }
367
+
368
+ function __classPrivateFieldIn(state, receiver) {
369
+ if (receiver === null || (typeof receiver !== "object" && typeof receiver !== "function")) throw new TypeError("Cannot use 'in' operator on non-object");
370
+ return typeof state === "function" ? receiver === state : state.has(receiver);
371
+ }
362
372
 
363
373
 
364
374
  /***/ }),
@@ -483,6 +493,7 @@ var tempIds_1 = __webpack_require__(/*! ./tempIds */ "./src/tempIds.tsx");
483
493
  var utils_1 = __webpack_require__(/*! ./utils */ "./src/utils.ts");
484
494
  var lodash_isempty_1 = (0, tslib_1.__importDefault)(__webpack_require__(/*! lodash.isempty */ "lodash.isempty"));
485
495
  var Loader_1 = __webpack_require__(/*! ./Loader */ "./src/Loader.ts");
496
+ var uui_core_1 = __webpack_require__(/*! @epam/uui-core */ "@epam/uui-core");
486
497
  var SimpleLoadingTracker_1 = __webpack_require__(/*! ./SimpleLoadingTracker */ "./src/SimpleLoadingTracker.ts");
487
498
  var ListLoadingTracker_1 = __webpack_require__(/*! ./ListLoadingTracker */ "./src/ListLoadingTracker.ts");
488
499
  var DbRef = /** @class */ (function () {
@@ -492,7 +503,6 @@ var DbRef = /** @class */ (function () {
492
503
  this.log = [];
493
504
  this.autoSave = true;
494
505
  this.savedPoint = 0;
495
- this.errors = [];
496
506
  this.throttleSaveMs = 1000;
497
507
  /* Update subscriptions (aka live views) */
498
508
  this.lastSubscriptionId = 1;
@@ -501,10 +511,36 @@ var DbRef = /** @class */ (function () {
501
511
  /* Error Subscriptions */
502
512
  this.saveErrorHandlers = [];
503
513
  /* Save scheduling */
504
- this.isSaveInProgress = false;
505
- this.isSaveThrottled = false;
514
+ this.enqueueSave = (0, uui_core_1.batch)(function () { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () {
515
+ var cumulativePatch, lastLogEntry, response, error_1;
516
+ return (0, tslib_1.__generator)(this, function (_a) {
517
+ switch (_a.label) {
518
+ case 0:
519
+ cumulativePatch = this.makeCumulativePatch();
520
+ lastLogEntry = this.log.length;
521
+ if ((0, lodash_isempty_1.default)(cumulativePatch)) {
522
+ return [2 /*return*/];
523
+ }
524
+ _a.label = 1;
525
+ case 1:
526
+ _a.trys.push([1, 3, , 4]);
527
+ return [4 /*yield*/, this.savePatch(cumulativePatch)];
528
+ case 2:
529
+ response = _a.sent();
530
+ this.applyResponse(response, lastLogEntry);
531
+ this.update();
532
+ return [3 /*break*/, 4];
533
+ case 3:
534
+ error_1 = _a.sent();
535
+ this.saveError(cumulativePatch, error_1);
536
+ this.update();
537
+ throw error_1;
538
+ case 4: return [2 /*return*/];
539
+ }
540
+ });
541
+ }); });
506
542
  this.handleBeforeUnload = function (e) {
507
- if (_this.isSaveInProgress || _this.isSaveThrottled) {
543
+ if (_this.enqueueSave.isBusy) {
508
544
  e.returnValue = false;
509
545
  return false;
510
546
  }
@@ -518,8 +554,7 @@ var DbRef = /** @class */ (function () {
518
554
  }
519
555
  /** Saves all committed patches. Useful only if autoSave is off. */
520
556
  DbRef.prototype.save = function () {
521
- this.enqueueSave();
522
- this.update();
557
+ return this.enqueueSave();
523
558
  };
524
559
  DbRef.prototype.getAutoSave = function () {
525
560
  return this.autoSave;
@@ -527,13 +562,12 @@ var DbRef = /** @class */ (function () {
527
562
  DbRef.prototype.setAutoSave = function (val) {
528
563
  if (this.autoSave != val) {
529
564
  this.autoSave = val;
530
- this.enqueueSave();
565
+ this.autoSave && this.enqueueSave();
531
566
  this.update();
532
567
  }
533
568
  };
534
569
  DbRef.prototype.revert = function () {
535
570
  this.db = this.base;
536
- this.errors = [];
537
571
  this.log = this.log.slice(0, this.savedPoint);
538
572
  this.update();
539
573
  };
@@ -543,18 +577,13 @@ var DbRef = /** @class */ (function () {
543
577
  };
544
578
  /* Db Update logic */
545
579
  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
- }
580
+ var dbBeforeUpdate = this.db.with(patch);
581
+ patch = this.beforeUpdate(patch, dbBeforeUpdate.tables, this.db.tables);
582
+ patch = (0, patchHelpers_1.mergeEntityPatches)(this.db.tables, patch);
583
+ this.log.push({ patch: patch });
584
+ this.db = this.db.with(patch);
585
+ this.autoSave && this.enqueueSave(null, { throttleMs: this.throttleSaveMs });
586
+ this.update();
558
587
  };
559
588
  DbRef.prototype.beforeUpdate = function (patch, tables, prevTables) {
560
589
  var _this = this;
@@ -600,24 +629,12 @@ var DbRef = /** @class */ (function () {
600
629
  return cumulativePatch;
601
630
  };
602
631
  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
- }
632
+ this.tempIdMap.appendServerMapping(response);
633
+ var serverPatch = (0, patchHelpers_1.flattenResponse)(response.submit, this.db.tables);
634
+ serverPatch = (0, patchHelpers_1.mergeEntityPatches)(this.db.tables, serverPatch);
635
+ this.savedPoint = newSavedPoint;
636
+ this.base = this.db;
637
+ this.commitFetch(serverPatch);
621
638
  };
622
639
  DbRef.prototype.subscribe = function (view, params, onUpdate) {
623
640
  var _this = this;
@@ -658,36 +675,6 @@ var DbRef = /** @class */ (function () {
658
675
  DbRef.prototype.saveError = function (request, error) {
659
676
  this.saveErrorHandlers.forEach(function (handler) { return handler(request, error); });
660
677
  };
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
678
  DbRef.prototype.makeLoader = function (options) {
692
679
  var loader = new Loader_1.Loader(this, function () { return new SimpleLoadingTracker_1.SimpleLoadingTracker(); }, options);
693
680
  this.loaders.push(loader);
@@ -770,14 +757,22 @@ var DbTable = /** @class */ (function () {
770
757
  var id = _this.getId(entityPatch);
771
758
  var immId = _this.keyToImmutable(id);
772
759
  var existing = _this.state.pk.get(immId);
760
+ var isDeleted = false;
773
761
  var updated = entityPatch;
774
762
  if (existing) {
775
763
  updated = (0, tslib_1.__assign)((0, tslib_1.__assign)({}, existing), entityPatch);
764
+ if (_this.schema.deleteFlag) {
765
+ isDeleted = updated[_this.schema.deleteFlag] === true;
766
+ }
776
767
  }
777
- return { id: id, immId: immId, patch: entityPatch, existing: existing, updated: updated, updatedFieldsisNew: !existing };
768
+ return { id: id, immId: immId, patch: entityPatch, existing: existing, updated: updated, isNew: !existing, isDeleted: isDeleted };
778
769
  });
779
770
  var idVal = immutable_1.Seq.Keyed(updates.map(function (entity) { return [entity.immId, entity.updated]; }));
780
771
  var newPk = this.state.pk.merge(idVal);
772
+ // TBD: replace with deleteAll after migrating to immutable 4
773
+ updates.filter(function (u) { return u.isDeleted; }).forEach(function (u) {
774
+ newPk = newPk.delete(u.immId);
775
+ });
781
776
  var newIndexes = this.state.indexes.map(function (index) {
782
777
  var newMap = index.map;
783
778
  var _loop_1 = function (n) {
@@ -785,11 +780,13 @@ var DbTable = /** @class */ (function () {
785
780
  var newKey = update.updated[index.field];
786
781
  if (update.existing) {
787
782
  var oldKey = update.existing[index.field];
788
- if (oldKey !== newKey) {
783
+ if (oldKey !== newKey || update.isDeleted) {
789
784
  newMap = newMap.update(oldKey, function (set) { return set.remove(update.immId); });
790
785
  }
791
786
  }
792
- newMap = newMap.update(newKey, I.Set(), function (set) { return set.add(update.immId); });
787
+ if (!update.isDeleted) {
788
+ newMap = newMap.update(newKey, I.Set(), function (set) { return set.add(update.immId); });
789
+ }
793
790
  };
794
791
  for (var n = 0; n < updates.length; n++) {
795
792
  _loop_1(n);