暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.cjs 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var wrapIdbValue = require('./wrap-idb-value.cjs');
  4. /**
  5. * Open a database.
  6. *
  7. * @param name Name of the database.
  8. * @param version Schema version.
  9. * @param callbacks Additional callbacks.
  10. */
  11. function openDB(name, version, { blocked, upgrade, blocking, terminated } = {}) {
  12. const request = indexedDB.open(name, version);
  13. const openPromise = wrapIdbValue.wrap(request);
  14. if (upgrade) {
  15. request.addEventListener('upgradeneeded', (event) => {
  16. upgrade(wrapIdbValue.wrap(request.result), event.oldVersion, event.newVersion, wrapIdbValue.wrap(request.transaction));
  17. });
  18. }
  19. if (blocked)
  20. request.addEventListener('blocked', () => blocked());
  21. openPromise
  22. .then((db) => {
  23. if (terminated)
  24. db.addEventListener('close', () => terminated());
  25. if (blocking)
  26. db.addEventListener('versionchange', () => blocking());
  27. })
  28. .catch(() => { });
  29. return openPromise;
  30. }
  31. /**
  32. * Delete a database.
  33. *
  34. * @param name Name of the database.
  35. */
  36. function deleteDB(name, { blocked } = {}) {
  37. const request = indexedDB.deleteDatabase(name);
  38. if (blocked)
  39. request.addEventListener('blocked', () => blocked());
  40. return wrapIdbValue.wrap(request).then(() => undefined);
  41. }
  42. const readMethods = ['get', 'getKey', 'getAll', 'getAllKeys', 'count'];
  43. const writeMethods = ['put', 'add', 'delete', 'clear'];
  44. const cachedMethods = new Map();
  45. function getMethod(target, prop) {
  46. if (!(target instanceof IDBDatabase &&
  47. !(prop in target) &&
  48. typeof prop === 'string')) {
  49. return;
  50. }
  51. if (cachedMethods.get(prop))
  52. return cachedMethods.get(prop);
  53. const targetFuncName = prop.replace(/FromIndex$/, '');
  54. const useIndex = prop !== targetFuncName;
  55. const isWrite = writeMethods.includes(targetFuncName);
  56. if (
  57. // Bail if the target doesn't exist on the target. Eg, getAll isn't in Edge.
  58. !(targetFuncName in (useIndex ? IDBIndex : IDBObjectStore).prototype) ||
  59. !(isWrite || readMethods.includes(targetFuncName))) {
  60. return;
  61. }
  62. const method = async function (storeName, ...args) {
  63. // isWrite ? 'readwrite' : undefined gzipps better, but fails in Edge :(
  64. const tx = this.transaction(storeName, isWrite ? 'readwrite' : 'readonly');
  65. let target = tx.store;
  66. if (useIndex)
  67. target = target.index(args.shift());
  68. // Must reject if op rejects.
  69. // If it's a write operation, must reject if tx.done rejects.
  70. // Must reject with op rejection first.
  71. // Must resolve with op value.
  72. // Must handle both promises (no unhandled rejections)
  73. return (await Promise.all([
  74. target[targetFuncName](...args),
  75. isWrite && tx.done,
  76. ]))[0];
  77. };
  78. cachedMethods.set(prop, method);
  79. return method;
  80. }
  81. wrapIdbValue.replaceTraps((oldTraps) => ({
  82. ...oldTraps,
  83. get: (target, prop, receiver) => getMethod(target, prop) || oldTraps.get(target, prop, receiver),
  84. has: (target, prop) => !!getMethod(target, prop) || oldTraps.has(target, prop),
  85. }));
  86. exports.unwrap = wrapIdbValue.unwrap;
  87. exports.wrap = wrapIdbValue.wrap;
  88. exports.deleteDB = deleteDB;
  89. exports.openDB = openDB;