index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import safariFix from 'safari-14-idb-fix';
  2. function promisifyRequest(request) {
  3. return new Promise((resolve, reject) => {
  4. // @ts-ignore - file size hacks
  5. request.oncomplete = request.onsuccess = () => resolve(request.result);
  6. // @ts-ignore - file size hacks
  7. request.onabort = request.onerror = () => reject(request.error);
  8. });
  9. }
  10. function createStore(dbName, storeName) {
  11. const dbp = safariFix().then(() => {
  12. const request = indexedDB.open(dbName);
  13. request.onupgradeneeded = () => request.result.createObjectStore(storeName);
  14. return promisifyRequest(request);
  15. });
  16. return (txMode, callback) => dbp.then((db) => callback(db.transaction(storeName, txMode).objectStore(storeName)));
  17. }
  18. let defaultGetStoreFunc;
  19. function defaultGetStore() {
  20. if (!defaultGetStoreFunc) {
  21. defaultGetStoreFunc = createStore('keyval-store', 'keyval');
  22. }
  23. return defaultGetStoreFunc;
  24. }
  25. /**
  26. * Get a value by its key.
  27. *
  28. * @param key
  29. * @param customStore Method to get a custom store. Use with caution (see the docs).
  30. */
  31. function get(key, customStore = defaultGetStore()) {
  32. return customStore('readonly', (store) => promisifyRequest(store.get(key)));
  33. }
  34. /**
  35. * Set a value with a key.
  36. *
  37. * @param key
  38. * @param value
  39. * @param customStore Method to get a custom store. Use with caution (see the docs).
  40. */
  41. function set(key, value, customStore = defaultGetStore()) {
  42. return customStore('readwrite', (store) => {
  43. store.put(value, key);
  44. return promisifyRequest(store.transaction);
  45. });
  46. }
  47. /**
  48. * Set multiple values at once. This is faster than calling set() multiple times.
  49. * It's also atomic – if one of the pairs can't be added, none will be added.
  50. *
  51. * @param entries Array of entries, where each entry is an array of `[key, value]`.
  52. * @param customStore Method to get a custom store. Use with caution (see the docs).
  53. */
  54. function setMany(entries, customStore = defaultGetStore()) {
  55. return customStore('readwrite', (store) => {
  56. entries.forEach((entry) => store.put(entry[1], entry[0]));
  57. return promisifyRequest(store.transaction);
  58. });
  59. }
  60. /**
  61. * Get multiple values by their keys
  62. *
  63. * @param keys
  64. * @param customStore Method to get a custom store. Use with caution (see the docs).
  65. */
  66. function getMany(keys, customStore = defaultGetStore()) {
  67. return customStore('readonly', (store) => Promise.all(keys.map((key) => promisifyRequest(store.get(key)))));
  68. }
  69. /**
  70. * Update a value. This lets you see the old value and update it as an atomic operation.
  71. *
  72. * @param key
  73. * @param updater A callback that takes the old value and returns a new value.
  74. * @param customStore Method to get a custom store. Use with caution (see the docs).
  75. */
  76. function update(key, updater, customStore = defaultGetStore()) {
  77. return customStore('readwrite', (store) =>
  78. // Need to create the promise manually.
  79. // If I try to chain promises, the transaction closes in browsers
  80. // that use a promise polyfill (IE10/11).
  81. new Promise((resolve, reject) => {
  82. store.get(key).onsuccess = function () {
  83. try {
  84. store.put(updater(this.result), key);
  85. resolve(promisifyRequest(store.transaction));
  86. }
  87. catch (err) {
  88. reject(err);
  89. }
  90. };
  91. }));
  92. }
  93. /**
  94. * Delete a particular key from the store.
  95. *
  96. * @param key
  97. * @param customStore Method to get a custom store. Use with caution (see the docs).
  98. */
  99. function del(key, customStore = defaultGetStore()) {
  100. return customStore('readwrite', (store) => {
  101. store.delete(key);
  102. return promisifyRequest(store.transaction);
  103. });
  104. }
  105. /**
  106. * Delete multiple keys at once.
  107. *
  108. * @param keys List of keys to delete.
  109. * @param customStore Method to get a custom store. Use with caution (see the docs).
  110. */
  111. function delMany(keys, customStore = defaultGetStore()) {
  112. return customStore('readwrite', (store) => {
  113. keys.forEach((key) => store.delete(key));
  114. return promisifyRequest(store.transaction);
  115. });
  116. }
  117. /**
  118. * Clear all values in the store.
  119. *
  120. * @param customStore Method to get a custom store. Use with caution (see the docs).
  121. */
  122. function clear(customStore = defaultGetStore()) {
  123. return customStore('readwrite', (store) => {
  124. store.clear();
  125. return promisifyRequest(store.transaction);
  126. });
  127. }
  128. function eachCursor(customStore, callback) {
  129. return customStore('readonly', (store) => {
  130. // This would be store.getAllKeys(), but it isn't supported by Edge or Safari.
  131. // And openKeyCursor isn't supported by Safari.
  132. store.openCursor().onsuccess = function () {
  133. if (!this.result)
  134. return;
  135. callback(this.result);
  136. this.result.continue();
  137. };
  138. return promisifyRequest(store.transaction);
  139. });
  140. }
  141. /**
  142. * Get all keys in the store.
  143. *
  144. * @param customStore Method to get a custom store. Use with caution (see the docs).
  145. */
  146. function keys(customStore = defaultGetStore()) {
  147. const items = [];
  148. return eachCursor(customStore, (cursor) => items.push(cursor.key)).then(() => items);
  149. }
  150. /**
  151. * Get all values in the store.
  152. *
  153. * @param customStore Method to get a custom store. Use with caution (see the docs).
  154. */
  155. function values(customStore = defaultGetStore()) {
  156. const items = [];
  157. return eachCursor(customStore, (cursor) => items.push(cursor.value)).then(() => items);
  158. }
  159. /**
  160. * Get all entries in the store. Each entry is an array of `[key, value]`.
  161. *
  162. * @param customStore Method to get a custom store. Use with caution (see the docs).
  163. */
  164. function entries(customStore = defaultGetStore()) {
  165. const items = [];
  166. return eachCursor(customStore, (cursor) => items.push([cursor.key, cursor.value])).then(() => items);
  167. }
  168. export { clear, createStore, del, delMany, entries, get, getMany, keys, promisifyRequest, set, setMany, update, values };