Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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