@epam/uui-db 5.7.1 → 5.8.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
@@ -3,7 +3,7 @@
3
3
  var shallowEqual = require('shallowequal');
4
4
  var zip = require('lodash.zip');
5
5
  var transform = require('lodash.transform');
6
- var isEqual = require('lodash.isequal');
6
+ var isEqual = require('react-fast-compare');
7
7
  var I = require('immutable');
8
8
  var uuiCore = require('@epam/uui-core');
9
9
  var groupBy = require('lodash.groupby');
@@ -913,6 +913,65 @@ class ListLoadingTracker {
913
913
  }
914
914
  }
915
915
 
916
+ const delay = (ms = 0) => {
917
+ if (ms <= 0) {
918
+ return Promise.resolve();
919
+ }
920
+ else {
921
+ return new Promise((resolve) => {
922
+ setTimeout(resolve, ms);
923
+ });
924
+ }
925
+ };
926
+ /** Creates an async function, which can collect several calls to it, and execute them in a single batch via provided callback. */
927
+ function batch(runBatch, options) {
928
+ let delayPromise = Promise.resolve();
929
+ let batchPromise = null;
930
+ let queue = [];
931
+ let nextCallPossibleAtMs = new Date().getTime();
932
+ function batchedFn(arg, callOptions) {
933
+ return __awaiter(this, void 0, void 0, function* () {
934
+ options = Object.assign(Object.assign({}, options), callOptions);
935
+ options.throttleMs = options.throttleMs || 0;
936
+ // Enqueue request
937
+ queue.push(arg);
938
+ const resultIndex = queue.length - 1; // an index of current item in queue. Result should be on the same index in batch.
939
+ batchedFn.isBusy = true;
940
+ // Wait current batch to complete
941
+ try {
942
+ yield batchPromise;
943
+ }
944
+ catch (_a) {
945
+ // ignore errors here. They are thrown to previous batch
946
+ }
947
+ // Wait for throttling delay to pass
948
+ yield delayPromise;
949
+ try {
950
+ // Start a batch if there are items.
951
+ // All calls that was awaiting will run this code.
952
+ // Only the first call will run batch, immediately clear the queue, and set batchPromise value
953
+ // Other calls will just await results of this call
954
+ if (queue.length > 0) {
955
+ nextCallPossibleAtMs = new Date().getTime() + options.throttleMs;
956
+ batchPromise = runBatch(queue);
957
+ queue = [];
958
+ }
959
+ const results = yield batchPromise;
960
+ return results === null || results === void 0 ? void 0 : results[resultIndex];
961
+ }
962
+ finally {
963
+ const nowMs = new Date().getTime();
964
+ const delayMs = Math.max(0, nextCallPossibleAtMs - nowMs);
965
+ // It's safe to replace currentPromise here. All other requests are already awaiting
966
+ delayPromise = delay(delayMs);
967
+ batchedFn.isBusy = queue.length > 0;
968
+ }
969
+ });
970
+ }
971
+ batchedFn.isBusy = false;
972
+ return batchedFn;
973
+ }
974
+
916
975
  class DbRef {
917
976
  constructor(blank) {
918
977
  this.blank = blank;
@@ -927,7 +986,7 @@ class DbRef {
927
986
  /* Error Subscriptions */
928
987
  this.saveErrorHandlers = [];
929
988
  /* Save scheduling */
930
- this.enqueueSave = uuiCore.batch(() => __awaiter(this, void 0, void 0, function* () {
989
+ this.enqueueSave = batch(() => __awaiter(this, void 0, void 0, function* () {
931
990
  const cumulativePatch = this.makeCumulativePatch();
932
991
  const lastLogEntry = this.log.length;
933
992
  if (isEmpty__default.default(cumulativePatch)) {
@@ -1100,7 +1159,7 @@ class DbRef {
1100
1159
  }
1101
1160
  }
1102
1161
 
1103
- const DbContext = react.createContext(null);
1162
+ const DbContext = /* @__PURE__ */react.createContext(null);
1104
1163
  function useDbRef() {
1105
1164
  return react.useContext(DbContext);
1106
1165
  }