dexie 4.0.1-alpha.11 → 4.0.1-alpha.13

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/dist/dexie.d.ts CHANGED
@@ -4,7 +4,7 @@
4
4
  *
5
5
  * By David Fahlander, david.fahlander@gmail.com
6
6
  *
7
- * Version 4.0.1-alpha.11, Wed May 24 2023
7
+ * Version 4.0.1-alpha.13, Wed May 31 2023
8
8
  *
9
9
  * https://dexie.org
10
10
  *
@@ -987,6 +987,8 @@ export interface Unsubscribable {
987
987
  export interface Observable<T = any> {
988
988
  subscribe(observerOrNext?: Observer<T> | ((value: T) => void)): Subscription;
989
989
  subscribe(next?: ((value: T) => void) | null, error?: ((error: any) => void) | null, complete?: (() => void) | null): Subscription;
990
+ getValue?(): T;
991
+ hasValue?(): boolean;
990
992
  [Symbol.observable]: () => Subscribable<T>;
991
993
  }
992
994
  export interface Subscription {
package/dist/dexie.js CHANGED
@@ -4,7 +4,7 @@
4
4
  *
5
5
  * By David Fahlander, david.fahlander@gmail.com
6
6
  *
7
- * Version 4.0.1-alpha.11, Wed May 24 2023
7
+ * Version 4.0.1-alpha.13, Wed May 31 2023
8
8
  *
9
9
  * https://dexie.org
10
10
  *
@@ -282,6 +282,12 @@
282
282
  var i;
283
283
  return x != null && (i = x[iteratorSymbol]) && i.apply(x);
284
284
  } : function () { return null; };
285
+ function delArrayItem(a, x) {
286
+ var i = a.indexOf(x);
287
+ if (i >= 0)
288
+ a.splice(i, 1);
289
+ return i >= 0;
290
+ }
285
291
  var NO_CHAR_ARRAY = {};
286
292
  function getArrayOf(arrayLike) {
287
293
  var i, a, x, it;
@@ -1254,13 +1260,17 @@
1254
1260
  return fn(resolve, reject, trans);
1255
1261
  });
1256
1262
  }).then(function (result) {
1257
- trans.idbtrans.commit();
1263
+ if (mode === 'readwrite')
1264
+ try {
1265
+ trans.idbtrans.commit();
1266
+ }
1267
+ catch (_a) { }
1258
1268
  return mode === 'readonly' ? result : trans._completion.then(function () { return result; });
1259
1269
  });
1260
1270
  }
1261
1271
  }
1262
1272
 
1263
- var DEXIE_VERSION = '4.0.1-alpha.11';
1273
+ var DEXIE_VERSION = '4.0.1-alpha.13';
1264
1274
  var maxString = String.fromCharCode(65535);
1265
1275
  var minKey = -Infinity;
1266
1276
  var INVALID_KEY_ARGUMENT = "Invalid key provided. Keys must be of type string, number, Date or Array<string | number | Date>.";
@@ -4756,7 +4766,45 @@
4756
4766
  return immutable ? Object.freeze(finalResult) : finalResult;
4757
4767
  }
4758
4768
 
4769
+ function obsSetsOverlap(os1, os2) {
4770
+ return Object.keys(os1).some(function (key) { return os2[key] && rangesOverlap(os2[key], os1[key]); });
4771
+ }
4772
+
4759
4773
  var cache = {};
4774
+ function invalidateCachedQueries(updateParts) {
4775
+ var queriesToSignal = new Set();
4776
+ for (var key in updateParts) {
4777
+ var parts = /^idb\:\/\/(.*)\/(.*)\//.exec(key);
4778
+ if (parts) {
4779
+ var dbName = parts[1], tableName = parts[2];
4780
+ var tblCache = cache["idb://".concat(dbName, "/").concat(tableName)];
4781
+ if (tblCache)
4782
+ invalidateTableQueries(tblCache, updateParts, queriesToSignal);
4783
+ }
4784
+ }
4785
+ queriesToSignal.forEach(function (requery) { return requery(); });
4786
+ }
4787
+ function invalidateTableQueries(tblCache, mutatedParts, queriesToSignal) {
4788
+ var updatedEntryLists = [];
4789
+ for (var _i = 0, _a = Object.entries(tblCache.queries.query); _i < _a.length; _i++) {
4790
+ var _b = _a[_i], indexName = _b[0], entries = _b[1];
4791
+ var filteredEntries = [];
4792
+ for (var _c = 0, entries_1 = entries; _c < entries_1.length; _c++) {
4793
+ var entry = entries_1[_c];
4794
+ if (entry.obsSet && obsSetsOverlap(mutatedParts, entry.obsSet)) {
4795
+ entry.subscribers.forEach(function (requery) { return queriesToSignal.add(requery); });
4796
+ }
4797
+ else {
4798
+ filteredEntries.push(entry);
4799
+ }
4800
+ }
4801
+ updatedEntryLists.push([indexName, filteredEntries]);
4802
+ }
4803
+ for (var _d = 0, updatedEntryLists_1 = updatedEntryLists; _d < updatedEntryLists_1.length; _d++) {
4804
+ var _e = updatedEntryLists_1[_d], indexName = _e[0], newEntries = _e[1];
4805
+ tblCache.queries.query[indexName] = newEntries;
4806
+ }
4807
+ }
4760
4808
 
4761
4809
  function areRangesEqual(r1, r2) {
4762
4810
  return (cmp(r1.lower, r2.lower) === 0 &&
@@ -4842,10 +4890,6 @@
4842
4890
  }
4843
4891
  }
4844
4892
 
4845
- function obsSetsOverlap(os1, os2) {
4846
- return Object.keys(os1).some(function (key) { return os2[key] && rangesOverlap(os2[key], os1[key]); });
4847
- }
4848
-
4849
4893
  function signalSubscribers(tblCache, mutatedParts) {
4850
4894
  extendObservabilitySet(tblCache.unsignaledParts, mutatedParts);
4851
4895
  if (!tblCache.signalTimer) {
@@ -4882,9 +4926,7 @@
4882
4926
  function enqueForDeletion(cacheEntry, container) {
4883
4927
  setTimeout(function () {
4884
4928
  if (cacheEntry.subscribers.size === 0) {
4885
- var pos = container.indexOf(cacheEntry);
4886
- if (pos > -1)
4887
- container.splice(pos, 1);
4929
+ delArrayItem(container, cacheEntry);
4888
4930
  }
4889
4931
  }, 3000);
4890
4932
  }
@@ -4895,7 +4937,7 @@
4895
4937
  name: 'Cache',
4896
4938
  create: function (core) {
4897
4939
  var dbName = core.schema.name;
4898
- return __assign(__assign({}, core), { transaction: function (stores, mode, options) {
4940
+ var coreMW = __assign(__assign({}, core), { transaction: function (stores, mode, options) {
4899
4941
  var idbtrans = core.transaction(stores, mode, options);
4900
4942
  var txs = PSD.txs;
4901
4943
  if (txs || mode === 'readwrite') {
@@ -4904,13 +4946,11 @@
4904
4946
  var ac_1 = new AbortController();
4905
4947
  var signal = ac_1.signal;
4906
4948
  var endTransaction = function (wasCommitted) { return function () {
4907
- if (txs) {
4908
- var idx = txs.indexOf(idbtrans);
4909
- if (idx > -1)
4910
- txs.splice(idx, 1);
4911
- }
4949
+ if (txs)
4950
+ delArrayItem(txs, idbtrans);
4912
4951
  ac_1.abort();
4913
4952
  if (mode === 'readwrite') {
4953
+ var affectedSubscribers_1 = new Set();
4914
4954
  for (var _i = 0, stores_1 = stores; _i < stores_1.length; _i++) {
4915
4955
  var storeName = stores_1[_i];
4916
4956
  var tblCache = cache["idb://".concat(dbName, "/").concat(storeName)];
@@ -4930,8 +4970,8 @@
4930
4970
  var freezeResults = Object.isFrozen(entry.res);
4931
4971
  var modRes = applyOptimisticOps(entry.res, entry.req, ops, table, entry, freezeResults);
4932
4972
  if (entry.dirty) {
4933
- entries.splice(entries.indexOf(entry), 1);
4934
- entry.subscribers.forEach(function (requery) { return requery(); });
4973
+ delArrayItem(entries, entry);
4974
+ entry.subscribers.forEach(function (requery) { return affectedSubscribers_1.add(requery); });
4935
4975
  }
4936
4976
  else if (modRes !== entry.res) {
4937
4977
  entry.res = modRes;
@@ -4940,9 +4980,9 @@
4940
4980
  }
4941
4981
  else {
4942
4982
  if (entry.dirty) {
4943
- entries.splice(entries.indexOf(entry), 1);
4983
+ delArrayItem(entries, entry);
4944
4984
  }
4945
- entry.subscribers.forEach(function (requery) { return requery(); });
4985
+ entry.subscribers.forEach(function (requery) { return affectedSubscribers_1.add(requery); });
4946
4986
  }
4947
4987
  }
4948
4988
  }
@@ -4950,6 +4990,7 @@
4950
4990
  }
4951
4991
  }
4952
4992
  }
4993
+ affectedSubscribers_1.forEach(function (requery) { return requery(); });
4953
4994
  }
4954
4995
  }; };
4955
4996
  idbtrans.addEventListener('abort', endTransaction(false), {
@@ -4966,7 +5007,7 @@
4966
5007
  }, table: function (tableName) {
4967
5008
  var downTable = core.table(tableName);
4968
5009
  var primKey = downTable.schema.primaryKey;
4969
- return __assign(__assign({}, downTable), { mutate: function (req) {
5010
+ var tableMW = __assign(__assign({}, downTable), { mutate: function (req) {
4970
5011
  if (primKey.outbound ||
4971
5012
  PSD.trans.db._options.cache === 'disabled'
4972
5013
  ) {
@@ -4993,7 +5034,7 @@
4993
5034
  tblCache.optimisticOps.push(req);
4994
5035
  signalSubscribers(tblCache, req.mutatedParts);
4995
5036
  promise.catch(function () {
4996
- tblCache.optimisticOps.splice(tblCache.optimisticOps.indexOf(req), 1);
5037
+ delArrayItem(tblCache.optimisticOps, req);
4997
5038
  signalSubscribers(tblCache, req.mutatedParts);
4998
5039
  });
4999
5040
  }
@@ -5021,10 +5062,20 @@
5021
5062
  res.result = deepClone(result);
5022
5063
  }
5023
5064
  return res;
5065
+ }).catch(function (error) {
5066
+ if (container && cacheEntry)
5067
+ delArrayItem(container, cacheEntry);
5068
+ return Promise.reject(error);
5024
5069
  });
5025
5070
  cacheEntry = {
5026
5071
  obsSet: req.obsSet,
5027
- promise: promise,
5072
+ promise: promise.catch(function (error) {
5073
+ if (req.trans.aborted) {
5074
+ var trans = coreMW.transaction([tableName], 'readonly');
5075
+ return tableMW.query(__assign(__assign({}, req), { trans: trans }));
5076
+ }
5077
+ return Promise.reject(error);
5078
+ }),
5028
5079
  subscribers: new Set(),
5029
5080
  type: 'query',
5030
5081
  req: req,
@@ -5056,7 +5107,9 @@
5056
5107
  };
5057
5108
  });
5058
5109
  } });
5110
+ return tableMW;
5059
5111
  } });
5112
+ return coreMW;
5060
5113
  },
5061
5114
  };
5062
5115
 
@@ -5394,7 +5447,9 @@
5394
5447
  }
5395
5448
 
5396
5449
  function liveQuery(querier) {
5397
- return new Observable(function (observer) {
5450
+ var hasValue = false;
5451
+ var currentValue = undefined;
5452
+ var observable = new Observable(function (observer) {
5398
5453
  var scopeFuncIsAsync = isAsyncFunction(querier);
5399
5454
  function execute(ctx) {
5400
5455
  if (scopeFuncIsAsync) {
@@ -5416,14 +5471,20 @@
5416
5471
  return closed;
5417
5472
  },
5418
5473
  unsubscribe: function () {
5474
+ if (closed)
5475
+ return;
5419
5476
  closed = true;
5420
5477
  if (abortController)
5421
5478
  abortController.abort();
5422
- globalEvents.storagemutated.unsubscribe(mutationListener);
5423
- txs.forEach(function (idbtrans) { try {
5424
- idbtrans.abort();
5425
- }
5426
- catch (_a) { } });
5479
+ if (startedListening)
5480
+ globalEvents.storagemutated.unsubscribe(mutationListener);
5481
+ txs.forEach(function (idbtrans) {
5482
+ idbtrans.aborted = true;
5483
+ try {
5484
+ idbtrans.abort();
5485
+ }
5486
+ catch (_a) { }
5487
+ });
5427
5488
  },
5428
5489
  };
5429
5490
  observer.start && observer.start(subscription);
@@ -5449,10 +5510,13 @@
5449
5510
  abortController.abort();
5450
5511
  abortController = new AbortController();
5451
5512
  if (txs.length) {
5452
- txs.forEach(function (idbtrans) { try {
5453
- idbtrans.abort();
5454
- }
5455
- catch (_a) { } });
5513
+ txs.forEach(function (idbtrans) {
5514
+ idbtrans.aborted = true;
5515
+ try {
5516
+ idbtrans.abort();
5517
+ }
5518
+ catch (_a) { }
5519
+ });
5456
5520
  txs.length = 0;
5457
5521
  }
5458
5522
  var ctx = {
@@ -5463,18 +5527,21 @@
5463
5527
  trans: null
5464
5528
  };
5465
5529
  var ret = execute(ctx);
5466
- if (!startedListening) {
5467
- globalEvents(DEXIE_STORAGE_MUTATED_EVENT_NAME, mutationListener);
5468
- startedListening = true;
5469
- }
5470
5530
  Promise.resolve(ret).then(function (result) {
5531
+ hasValue = true;
5532
+ currentValue = result;
5471
5533
  if (closed || ctx.signal.aborted) {
5472
5534
  return;
5473
5535
  }
5474
5536
  accumMuts = {};
5475
5537
  currentObs = subscr;
5538
+ if (keys(currentObs).length > 0 && !startedListening) {
5539
+ globalEvents(DEXIE_STORAGE_MUTATED_EVENT_NAME, mutationListener);
5540
+ startedListening = true;
5541
+ }
5476
5542
  observer.next && observer.next(result);
5477
5543
  }, function (err) {
5544
+ hasValue = false;
5478
5545
  if (!['DatabaseClosedError', 'AbortError'].includes(err === null || err === void 0 ? void 0 : err.name)) {
5479
5546
  if (closed)
5480
5547
  return;
@@ -5485,6 +5552,9 @@
5485
5552
  doQuery();
5486
5553
  return subscription;
5487
5554
  });
5555
+ observable.hasValue = function () { return hasValue; };
5556
+ observable.getValue = function () { return currentValue; };
5557
+ return observable;
5488
5558
  }
5489
5559
 
5490
5560
  var Dexie = Dexie$1;
@@ -5600,6 +5670,7 @@
5600
5670
  try {
5601
5671
  propagatingLocally = true;
5602
5672
  globalEvents.storagemutated.fire(updateParts);
5673
+ invalidateCachedQueries(updateParts);
5603
5674
  }
5604
5675
  finally {
5605
5676
  propagatingLocally = wasMe;