dexie 3.2.0-beta.3 → 3.2.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.
@@ -4,7 +4,7 @@
4
4
  *
5
5
  * By David Fahlander, david.fahlander@gmail.com
6
6
  *
7
- * Version 3.2.0-beta.3, Mon Aug 02 2021
7
+ * Version 3.2.0, Thu Nov 18 2021
8
8
  *
9
9
  * http://dexie.org
10
10
  *
@@ -1236,7 +1236,7 @@ function tempTransaction(db, mode, storeNames, fn) {
1236
1236
  }
1237
1237
  }
1238
1238
 
1239
- const DEXIE_VERSION = '3.2.0-beta.3';
1239
+ const DEXIE_VERSION = '3.2.0';
1240
1240
  const maxString = String.fromCharCode(65535);
1241
1241
  const minKey = -Infinity;
1242
1242
  const INVALID_KEY_ARGUMENT = "Invalid key provided. Keys must be of type string, number, Date or Array<string | number | Date>.";
@@ -1258,37 +1258,11 @@ function combine(filter1, filter2) {
1258
1258
  filter2;
1259
1259
  }
1260
1260
 
1261
- let domDeps;
1262
- try {
1263
- domDeps = {
1264
- indexedDB: _global.indexedDB || _global.mozIndexedDB || _global.webkitIndexedDB || _global.msIndexedDB,
1265
- IDBKeyRange: _global.IDBKeyRange || _global.webkitIDBKeyRange
1266
- };
1267
- }
1268
- catch (e) {
1269
- domDeps = { indexedDB: null, IDBKeyRange: null };
1270
- }
1271
-
1272
- function safariMultiStoreFix(storeNames) {
1273
- return storeNames.length === 1 ? storeNames[0] : storeNames;
1274
- }
1275
- let getMaxKey = (IdbKeyRange) => {
1276
- try {
1277
- IdbKeyRange.only([[]]);
1278
- getMaxKey = () => [[]];
1279
- return [[]];
1280
- }
1281
- catch (e) {
1282
- getMaxKey = () => maxString;
1283
- return maxString;
1284
- }
1285
- };
1286
-
1287
1261
  const AnyRange = {
1288
1262
  type: 3 ,
1289
1263
  lower: -Infinity,
1290
1264
  lowerOpen: false,
1291
- get upper() { return getMaxKey(domDeps.IDBKeyRange); },
1265
+ upper: [[]],
1292
1266
  upperOpen: false
1293
1267
  };
1294
1268
 
@@ -1738,6 +1712,82 @@ function iterate(cursorPromise, filter, fn, valueMapper) {
1738
1712
  });
1739
1713
  }
1740
1714
 
1715
+ function cmp(a, b) {
1716
+ try {
1717
+ const ta = type(a);
1718
+ const tb = type(b);
1719
+ if (ta !== tb) {
1720
+ if (ta === 'Array')
1721
+ return 1;
1722
+ if (tb === 'Array')
1723
+ return -1;
1724
+ if (ta === 'binary')
1725
+ return 1;
1726
+ if (tb === 'binary')
1727
+ return -1;
1728
+ if (ta === 'string')
1729
+ return 1;
1730
+ if (tb === 'string')
1731
+ return -1;
1732
+ if (ta === 'Date')
1733
+ return 1;
1734
+ if (tb !== 'Date')
1735
+ return NaN;
1736
+ return -1;
1737
+ }
1738
+ switch (ta) {
1739
+ case 'number':
1740
+ case 'Date':
1741
+ case 'string':
1742
+ return a > b ? 1 : a < b ? -1 : 0;
1743
+ case 'binary': {
1744
+ return compareUint8Arrays(getUint8Array(a), getUint8Array(b));
1745
+ }
1746
+ case 'Array':
1747
+ return compareArrays(a, b);
1748
+ }
1749
+ }
1750
+ catch (_a) { }
1751
+ return NaN;
1752
+ }
1753
+ function compareArrays(a, b) {
1754
+ const al = a.length;
1755
+ const bl = b.length;
1756
+ const l = al < bl ? al : bl;
1757
+ for (let i = 0; i < l; ++i) {
1758
+ const res = cmp(a[i], b[i]);
1759
+ if (res !== 0)
1760
+ return res;
1761
+ }
1762
+ return al === bl ? 0 : al < bl ? -1 : 1;
1763
+ }
1764
+ function compareUint8Arrays(a, b) {
1765
+ const al = a.length;
1766
+ const bl = b.length;
1767
+ const l = al < bl ? al : bl;
1768
+ for (let i = 0; i < l; ++i) {
1769
+ if (a[i] !== b[i])
1770
+ return a[i] < b[i] ? -1 : 1;
1771
+ }
1772
+ return al === bl ? 0 : al < bl ? -1 : 1;
1773
+ }
1774
+ function type(x) {
1775
+ const t = typeof x;
1776
+ if (t !== 'object')
1777
+ return t;
1778
+ if (ArrayBuffer.isView(x))
1779
+ return 'binary';
1780
+ const tsTag = toStringTag(x);
1781
+ return tsTag === 'ArrayBuffer' ? 'binary' : tsTag;
1782
+ }
1783
+ function getUint8Array(a) {
1784
+ if (a instanceof Uint8Array)
1785
+ return a;
1786
+ if (ArrayBuffer.isView(a))
1787
+ return new Uint8Array(a.buffer, a.byteOffset, a.byteLength);
1788
+ return new Uint8Array(a);
1789
+ }
1790
+
1741
1791
  class Collection {
1742
1792
  _read(fn, cb) {
1743
1793
  var ctx = this._ctx;
@@ -2010,7 +2060,6 @@ class Collection {
2010
2060
  const coreTable = ctx.table.core;
2011
2061
  const { outbound, extractKey } = coreTable.schema.primaryKey;
2012
2062
  const limit = this.db._options.modifyChunkSize || 200;
2013
- const { cmp } = this.db.core;
2014
2063
  const totalFailures = [];
2015
2064
  let successCount = 0;
2016
2065
  const failedKeys = [];
@@ -2528,7 +2577,9 @@ function preventDefault(event) {
2528
2577
  event.preventDefault();
2529
2578
  }
2530
2579
 
2531
- const globalEvents = Events(null, "txcommitted");
2580
+ const DEXIE_STORAGE_MUTATED_EVENT_NAME = 'storagemutated';
2581
+ const STORAGE_MUTATED_DOM_EVENT_NAME = 'x-storagemutated-1';
2582
+ const globalEvents = Events(null, DEXIE_STORAGE_MUTATED_EVENT_NAME);
2532
2583
 
2533
2584
  class Transaction {
2534
2585
  _lock() {
@@ -2593,7 +2644,7 @@ class Transaction {
2593
2644
  this.active = false;
2594
2645
  this._resolve();
2595
2646
  if ('mutatedParts' in idbtrans) {
2596
- globalEvents.txcommitted.fire(idbtrans["mutatedParts"]);
2647
+ globalEvents.storagemutated.fire(idbtrans["mutatedParts"]);
2597
2648
  }
2598
2649
  });
2599
2650
  return this;
@@ -2750,6 +2801,21 @@ function createTableSchema(name, primKey, indexes) {
2750
2801
  };
2751
2802
  }
2752
2803
 
2804
+ function safariMultiStoreFix(storeNames) {
2805
+ return storeNames.length === 1 ? storeNames[0] : storeNames;
2806
+ }
2807
+ let getMaxKey = (IdbKeyRange) => {
2808
+ try {
2809
+ IdbKeyRange.only([[]]);
2810
+ getMaxKey = () => [[]];
2811
+ return [[]];
2812
+ }
2813
+ catch (e) {
2814
+ getMaxKey = () => maxString;
2815
+ return maxString;
2816
+ }
2817
+ };
2818
+
2753
2819
  function getKeyExtractor(keyPath) {
2754
2820
  if (keyPath == null) {
2755
2821
  return () => undefined;
@@ -2782,8 +2848,7 @@ function getKeyPathAlias(keyPath) {
2782
2848
  keyPath :
2783
2849
  `[${keyPath.join('+')}]`;
2784
2850
  }
2785
- function createDBCore(db, indexedDB, IdbKeyRange, tmpTrans) {
2786
- const cmp = indexedDB.cmp.bind(indexedDB);
2851
+ function createDBCore(db, IdbKeyRange, tmpTrans) {
2787
2852
  function extractSchema(db, trans) {
2788
2853
  const tables = arrayify(db.objectStoreNames);
2789
2854
  return {
@@ -3112,7 +3177,6 @@ function createDBCore(db, indexedDB, IdbKeyRange, tmpTrans) {
3112
3177
  throw new Error(`Table '${name}' not found`);
3113
3178
  return tableMap[name];
3114
3179
  },
3115
- cmp,
3116
3180
  MIN_KEY: -Infinity,
3117
3181
  MAX_KEY: getMaxKey(IdbKeyRange),
3118
3182
  schema
@@ -3123,7 +3187,7 @@ function createMiddlewareStack(stackImpl, middlewares) {
3123
3187
  return middlewares.reduce((down, { create }) => ({ ...down, ...create(down) }), stackImpl);
3124
3188
  }
3125
3189
  function createMiddlewareStacks(middlewares, idbdb, { IDBKeyRange, indexedDB }, tmpTrans) {
3126
- const dbcore = createMiddlewareStack(createDBCore(idbdb, indexedDB, IDBKeyRange, tmpTrans), middlewares.dbcore);
3190
+ const dbcore = createMiddlewareStack(createDBCore(idbdb, IDBKeyRange, tmpTrans), middlewares.dbcore);
3127
3191
  return {
3128
3192
  dbcore
3129
3193
  };
@@ -3345,12 +3409,7 @@ function createMissingTables(newSchema, idbtrans) {
3345
3409
  });
3346
3410
  }
3347
3411
  function deleteRemovedTables(newSchema, idbtrans) {
3348
- for (var i = 0; i < idbtrans.db.objectStoreNames.length; ++i) {
3349
- var storeName = idbtrans.db.objectStoreNames[i];
3350
- if (newSchema[storeName] == null) {
3351
- idbtrans.db.deleteObjectStore(storeName);
3352
- }
3353
- }
3412
+ [].slice.call(idbtrans.db.objectStoreNames).forEach(storeName => newSchema[storeName] == null && idbtrans.db.deleteObjectStore(storeName));
3354
3413
  }
3355
3414
  function addIndex(store, idx) {
3356
3415
  store.createIndex(idx.name, idx.keyPath, { unique: idx.unique, multiEntry: idx.multi });
@@ -3517,18 +3576,17 @@ function vip(fn) {
3517
3576
  }
3518
3577
 
3519
3578
  function idbReady() {
3520
- var isSafari = !navigator.userAgentData && /Safari\//.test(navigator.userAgent) && !/Chrom(e|ium)\//.test(navigator.userAgent);
3521
- if (!isSafari || !indexedDB.databases) return Promise.resolve();
3522
- var intervalId;
3523
- return new Promise(function (resolve) {
3524
- var tryIdb = function tryIdb() {
3525
- return indexedDB.databases().finally(resolve);
3526
- };
3527
- intervalId = setInterval(tryIdb, 100);
3528
- tryIdb();
3529
- }).finally(function () {
3530
- return clearInterval(intervalId);
3531
- });
3579
+ var isSafari = !navigator.userAgentData &&
3580
+ /Safari\//.test(navigator.userAgent) &&
3581
+ !/Chrom(e|ium)\//.test(navigator.userAgent);
3582
+ if (!isSafari || !indexedDB.databases)
3583
+ return Promise.resolve();
3584
+ var intervalId;
3585
+ return new Promise(function (resolve) {
3586
+ var tryIdb = function () { return indexedDB.databases().finally(resolve); };
3587
+ intervalId = setInterval(tryIdb, 100);
3588
+ tryIdb();
3589
+ }).finally(function () { return clearInterval(intervalId); });
3532
3590
  }
3533
3591
 
3534
3592
  function dexieOpen(db) {
@@ -3549,7 +3607,7 @@ function dexieOpen(db) {
3549
3607
  }
3550
3608
  let resolveDbReady = state.dbReadyResolve,
3551
3609
  upgradeTransaction = null, wasCreated = false;
3552
- return DexiePromise.race([openCanceller, idbReady().then(() => new DexiePromise((resolve, reject) => {
3610
+ return DexiePromise.race([openCanceller, (typeof navigator === 'undefined' ? DexiePromise.resolve() : idbReady()).then(() => new DexiePromise((resolve, reject) => {
3553
3611
  throwIfCancelled();
3554
3612
  if (!indexedDB)
3555
3613
  throw new exceptions.MissingAPI();
@@ -3819,6 +3877,11 @@ function createVirtualIndexMiddleware(down) {
3819
3877
  cursor.continuePrimaryKey(pad(key, down.MAX_KEY, keyTail), primaryKey);
3820
3878
  }
3821
3879
  },
3880
+ primaryKey: {
3881
+ get() {
3882
+ return cursor.primaryKey;
3883
+ }
3884
+ },
3822
3885
  key: {
3823
3886
  get() {
3824
3887
  const key = cursor.key;
@@ -4020,24 +4083,6 @@ function getExistingValues(table, req, effectiveKeys) {
4020
4083
  : table.getMany({ trans: req.trans, keys: effectiveKeys, cache: "immutable" });
4021
4084
  }
4022
4085
 
4023
- let _cmp;
4024
- function cmp(a, b) {
4025
- if (_cmp)
4026
- return _cmp(a, b);
4027
- const { indexedDB } = domDeps;
4028
- if (!indexedDB)
4029
- throw new exceptions.MissingAPI();
4030
- _cmp = (a, b) => {
4031
- try {
4032
- return a == null || b == null ? NaN : indexedDB.cmp(a, b);
4033
- }
4034
- catch (_a) {
4035
- return NaN;
4036
- }
4037
- };
4038
- return _cmp(a, b);
4039
- }
4040
-
4041
4086
  function getFromTransactionCache(keys, cache, clone) {
4042
4087
  try {
4043
4088
  if (!cache)
@@ -4725,7 +4770,7 @@ class Dexie$1 {
4725
4770
  }
4726
4771
 
4727
4772
  const symbolObservable = typeof Symbol !== "undefined" && "observable" in Symbol
4728
- ? Symbol["observable"]
4773
+ ? Symbol.observable
4729
4774
  : "@@observable";
4730
4775
  class Observable {
4731
4776
  constructor(subscribe) {
@@ -4773,7 +4818,7 @@ function liveQuery(querier) {
4773
4818
  },
4774
4819
  unsubscribe: () => {
4775
4820
  closed = true;
4776
- globalEvents.txcommitted.unsubscribe(mutationListener);
4821
+ globalEvents.storagemutated.unsubscribe(mutationListener);
4777
4822
  },
4778
4823
  };
4779
4824
  observer.start && observer.start(subscription);
@@ -4794,7 +4839,7 @@ function liveQuery(querier) {
4794
4839
  const subscr = {};
4795
4840
  const ret = execute(subscr);
4796
4841
  if (!startedListening) {
4797
- globalEvents("txcommitted", mutationListener);
4842
+ globalEvents(DEXIE_STORAGE_MUTATED_EVENT_NAME, mutationListener);
4798
4843
  startedListening = true;
4799
4844
  }
4800
4845
  querying = true;
@@ -4821,6 +4866,17 @@ function liveQuery(querier) {
4821
4866
  });
4822
4867
  }
4823
4868
 
4869
+ let domDeps;
4870
+ try {
4871
+ domDeps = {
4872
+ indexedDB: _global.indexedDB || _global.mozIndexedDB || _global.webkitIndexedDB || _global.msIndexedDB,
4873
+ IDBKeyRange: _global.IDBKeyRange || _global.webkitIDBKeyRange
4874
+ };
4875
+ }
4876
+ catch (e) {
4877
+ domDeps = { indexedDB: null, IDBKeyRange: null };
4878
+ }
4879
+
4824
4880
  const Dexie = Dexie$1;
4825
4881
  props(Dexie, {
4826
4882
  ...fullNameExceptions,
@@ -4911,6 +4967,7 @@ props(Dexie, {
4911
4967
  shallowClone: shallowClone,
4912
4968
  deepClone: deepClone,
4913
4969
  getObjectDiff: getObjectDiff,
4970
+ cmp,
4914
4971
  asap: asap$1,
4915
4972
  minKey: minKey,
4916
4973
  addons: [],
@@ -4924,38 +4981,45 @@ props(Dexie, {
4924
4981
  });
4925
4982
  Dexie.maxKey = getMaxKey(Dexie.dependencies.IDBKeyRange);
4926
4983
 
4927
- function fireLocally(updateParts) {
4984
+ if (typeof dispatchEvent !== 'undefined' && typeof addEventListener !== 'undefined') {
4985
+ globalEvents(DEXIE_STORAGE_MUTATED_EVENT_NAME, updatedParts => {
4986
+ if (!propagatingLocally) {
4987
+ let event;
4988
+ if (isIEOrEdge) {
4989
+ event = document.createEvent('CustomEvent');
4990
+ event.initCustomEvent(STORAGE_MUTATED_DOM_EVENT_NAME, true, true, updatedParts);
4991
+ }
4992
+ else {
4993
+ event = new CustomEvent(STORAGE_MUTATED_DOM_EVENT_NAME, {
4994
+ detail: updatedParts
4995
+ });
4996
+ }
4997
+ propagatingLocally = true;
4998
+ dispatchEvent(event);
4999
+ propagatingLocally = false;
5000
+ }
5001
+ });
5002
+ addEventListener(STORAGE_MUTATED_DOM_EVENT_NAME, ({ detail }) => {
5003
+ if (!propagatingLocally) {
5004
+ propagateLocally(detail);
5005
+ }
5006
+ });
5007
+ }
5008
+ function propagateLocally(updateParts) {
4928
5009
  let wasMe = propagatingLocally;
4929
5010
  try {
4930
5011
  propagatingLocally = true;
4931
- globalEvents.txcommitted.fire(updateParts);
5012
+ globalEvents.storagemutated.fire(updateParts);
4932
5013
  }
4933
5014
  finally {
4934
5015
  propagatingLocally = wasMe;
4935
5016
  }
4936
5017
  }
4937
- let propagateLocally = fireLocally;
4938
5018
  let propagatingLocally = false;
4939
- let accumulatedParts = {};
4940
- if (typeof document !== 'undefined' && document.addEventListener) {
4941
- const fireIfVisible = () => {
4942
- if (document.visibilityState === "visible") {
4943
- if (Object.keys(accumulatedParts).length > 0) {
4944
- fireLocally(accumulatedParts);
4945
- }
4946
- accumulatedParts = {};
4947
- }
4948
- };
4949
- document.addEventListener("visibilitychange", fireIfVisible);
4950
- propagateLocally = (changedParts) => {
4951
- extendObservabilitySet(accumulatedParts, changedParts);
4952
- fireIfVisible();
4953
- };
4954
- }
4955
5019
 
4956
- if (typeof BroadcastChannel !== "undefined") {
4957
- const bc = new BroadcastChannel("dexie-txcommitted");
4958
- globalEvents("txcommitted", (changedParts) => {
5020
+ if (typeof BroadcastChannel !== 'undefined') {
5021
+ const bc = new BroadcastChannel(STORAGE_MUTATED_DOM_EVENT_NAME);
5022
+ globalEvents(DEXIE_STORAGE_MUTATED_EVENT_NAME, (changedParts) => {
4959
5023
  if (!propagatingLocally) {
4960
5024
  bc.postMessage(changedParts);
4961
5025
  }
@@ -4965,19 +5029,19 @@ if (typeof BroadcastChannel !== "undefined") {
4965
5029
  propagateLocally(ev.data);
4966
5030
  };
4967
5031
  }
4968
- else if (typeof self !== "undefined" && typeof navigator !== "undefined") {
4969
- globalEvents("txcommitted", (changedParts) => {
5032
+ else if (typeof self !== 'undefined' && typeof navigator !== 'undefined') {
5033
+ globalEvents(DEXIE_STORAGE_MUTATED_EVENT_NAME, (changedParts) => {
4970
5034
  try {
4971
5035
  if (!propagatingLocally) {
4972
- if (typeof localStorage !== "undefined") {
4973
- localStorage.setItem("dexie-txcommitted", JSON.stringify({
5036
+ if (typeof localStorage !== 'undefined') {
5037
+ localStorage.setItem(STORAGE_MUTATED_DOM_EVENT_NAME, JSON.stringify({
4974
5038
  trig: Math.random(),
4975
5039
  changedParts,
4976
5040
  }));
4977
5041
  }
4978
- if (typeof self["clients"] === "object") {
4979
- [...self["clients"].matchAll({ includeUncontrolled: true })].forEach((client) => client.postMessage({
4980
- type: "dexie-txcommitted",
5042
+ if (typeof self['clients'] === 'object') {
5043
+ [...self['clients'].matchAll({ includeUncontrolled: true })].forEach((client) => client.postMessage({
5044
+ type: STORAGE_MUTATED_DOM_EVENT_NAME,
4981
5045
  changedParts,
4982
5046
  }));
4983
5047
  }
@@ -4985,8 +5049,8 @@ else if (typeof self !== "undefined" && typeof navigator !== "undefined") {
4985
5049
  }
4986
5050
  catch (_a) { }
4987
5051
  });
4988
- addEventListener("storage", (ev) => {
4989
- if (ev.key === "dexie-txcommitted") {
5052
+ addEventListener('storage', (ev) => {
5053
+ if (ev.key === STORAGE_MUTATED_DOM_EVENT_NAME) {
4990
5054
  const data = JSON.parse(ev.newValue);
4991
5055
  if (data)
4992
5056
  propagateLocally(data.changedParts);
@@ -4998,7 +5062,7 @@ else if (typeof self !== "undefined" && typeof navigator !== "undefined") {
4998
5062
  }
4999
5063
  }
5000
5064
  function propagateMessageLocally({ data }) {
5001
- if (data && data.type === "dexie-txcommitted") {
5065
+ if (data && data.type === STORAGE_MUTATED_DOM_EVENT_NAME) {
5002
5066
  propagateLocally(data.changedParts);
5003
5067
  }
5004
5068
  }
@@ -5006,6 +5070,5 @@ function propagateMessageLocally({ data }) {
5006
5070
  DexiePromise.rejectionMapper = mapError;
5007
5071
  setDebug(debug, dexieStackFrameFilter);
5008
5072
 
5009
- export default Dexie$1;
5010
- export { Dexie$1 as Dexie, RangeSet, liveQuery, mergeRanges, rangesOverlap };
5073
+ export { Dexie$1 as Dexie, RangeSet, Dexie$1 as default, liveQuery, mergeRanges, rangesOverlap };
5011
5074
  //# sourceMappingURL=dexie.mjs.map