compat.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
  2. function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  3. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  4. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
  5. function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
  6. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  7. function promisifyRequest(request) {
  8. return new Promise(function (resolve, reject) {
  9. // @ts-ignore - file size hacks
  10. request.oncomplete = request.onsuccess = function () {
  11. return resolve(request.result);
  12. }; // @ts-ignore - file size hacks
  13. request.onabort = request.onerror = function () {
  14. return reject(request.error);
  15. };
  16. });
  17. }
  18. function createStore(dbName, storeName) {
  19. var request = indexedDB.open(dbName);
  20. request.onupgradeneeded = function () {
  21. return request.result.createObjectStore(storeName);
  22. };
  23. var dbp = promisifyRequest(request);
  24. return function (txMode, callback) {
  25. return dbp.then(function (db) {
  26. return callback(db.transaction(storeName, txMode).objectStore(storeName));
  27. });
  28. };
  29. }
  30. var defaultGetStoreFunc;
  31. function defaultGetStore() {
  32. if (!defaultGetStoreFunc) {
  33. defaultGetStoreFunc = createStore('keyval-store', 'keyval');
  34. }
  35. return defaultGetStoreFunc;
  36. }
  37. /**
  38. * Get a value by its key.
  39. *
  40. * @param key
  41. * @param customStore Method to get a custom store. Use with caution (see the docs).
  42. */
  43. function get(key) {
  44. var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore();
  45. return customStore('readonly', function (store) {
  46. return promisifyRequest(store.get(key));
  47. });
  48. }
  49. /**
  50. * Set a value with a key.
  51. *
  52. * @param key
  53. * @param value
  54. * @param customStore Method to get a custom store. Use with caution (see the docs).
  55. */
  56. function set(key, value) {
  57. var customStore = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultGetStore();
  58. return customStore('readwrite', function (store) {
  59. store.put(value, key);
  60. return promisifyRequest(store.transaction);
  61. });
  62. }
  63. /**
  64. * Set multiple values at once. This is faster than calling set() multiple times.
  65. * It's also atomic – if one of the pairs can't be added, none will be added.
  66. *
  67. * @param entries Array of entries, where each entry is an array of `[key, value]`.
  68. * @param customStore Method to get a custom store. Use with caution (see the docs).
  69. */
  70. function setMany(entries) {
  71. var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore();
  72. return customStore('readwrite', function (store) {
  73. entries.forEach(function (entry) {
  74. return store.put(entry[1], entry[0]);
  75. });
  76. return promisifyRequest(store.transaction);
  77. });
  78. }
  79. /**
  80. * Get multiple values by their keys
  81. *
  82. * @param keys
  83. * @param customStore Method to get a custom store. Use with caution (see the docs).
  84. */
  85. function getMany(keys) {
  86. var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore();
  87. return customStore('readonly', function (store) {
  88. return Promise.all(keys.map(function (key) {
  89. return promisifyRequest(store.get(key));
  90. }));
  91. });
  92. }
  93. /**
  94. * Update a value. This lets you see the old value and update it as an atomic operation.
  95. *
  96. * @param key
  97. * @param updater A callback that takes the old value and returns a new value.
  98. * @param customStore Method to get a custom store. Use with caution (see the docs).
  99. */
  100. function update(key, updater) {
  101. var customStore = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : defaultGetStore();
  102. return customStore('readwrite', function (store) {
  103. return (// Need to create the promise manually.
  104. // If I try to chain promises, the transaction closes in browsers
  105. // that use a promise polyfill (IE10/11).
  106. new Promise(function (resolve, reject) {
  107. store.get(key).onsuccess = function () {
  108. try {
  109. store.put(updater(this.result), key);
  110. resolve(promisifyRequest(store.transaction));
  111. } catch (err) {
  112. reject(err);
  113. }
  114. };
  115. })
  116. );
  117. });
  118. }
  119. /**
  120. * Delete a particular key from the store.
  121. *
  122. * @param key
  123. * @param customStore Method to get a custom store. Use with caution (see the docs).
  124. */
  125. function del(key) {
  126. var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore();
  127. return customStore('readwrite', function (store) {
  128. store.delete(key);
  129. return promisifyRequest(store.transaction);
  130. });
  131. }
  132. /**
  133. * Delete multiple keys at once.
  134. *
  135. * @param keys List of keys to delete.
  136. * @param customStore Method to get a custom store. Use with caution (see the docs).
  137. */
  138. function delMany(keys) {
  139. var customStore = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultGetStore();
  140. return customStore('readwrite', function (store) {
  141. keys.forEach(function (key) {
  142. return store.delete(key);
  143. });
  144. return promisifyRequest(store.transaction);
  145. });
  146. }
  147. /**
  148. * Clear all values in the store.
  149. *
  150. * @param customStore Method to get a custom store. Use with caution (see the docs).
  151. */
  152. function clear() {
  153. var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore();
  154. return customStore('readwrite', function (store) {
  155. store.clear();
  156. return promisifyRequest(store.transaction);
  157. });
  158. }
  159. function eachCursor(store, callback) {
  160. store.openCursor().onsuccess = function () {
  161. if (!this.result) return;
  162. callback(this.result);
  163. this.result.continue();
  164. };
  165. return promisifyRequest(store.transaction);
  166. }
  167. /**
  168. * Get all keys in the store.
  169. *
  170. * @param customStore Method to get a custom store. Use with caution (see the docs).
  171. */
  172. function keys() {
  173. var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore();
  174. return customStore('readonly', function (store) {
  175. // Fast path for modern browsers
  176. if (store.getAllKeys) {
  177. return promisifyRequest(store.getAllKeys());
  178. }
  179. var items = [];
  180. return eachCursor(store, function (cursor) {
  181. return items.push(cursor.key);
  182. }).then(function () {
  183. return items;
  184. });
  185. });
  186. }
  187. /**
  188. * Get all values in the store.
  189. *
  190. * @param customStore Method to get a custom store. Use with caution (see the docs).
  191. */
  192. function values() {
  193. var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore();
  194. return customStore('readonly', function (store) {
  195. // Fast path for modern browsers
  196. if (store.getAll) {
  197. return promisifyRequest(store.getAll());
  198. }
  199. var items = [];
  200. return eachCursor(store, function (cursor) {
  201. return items.push(cursor.value);
  202. }).then(function () {
  203. return items;
  204. });
  205. });
  206. }
  207. /**
  208. * Get all entries in the store. Each entry is an array of `[key, value]`.
  209. *
  210. * @param customStore Method to get a custom store. Use with caution (see the docs).
  211. */
  212. function entries() {
  213. var customStore = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : defaultGetStore();
  214. return customStore('readonly', function (store) {
  215. // Fast path for modern browsers
  216. // (although, hopefully we'll get a simpler path some day)
  217. if (store.getAll && store.getAllKeys) {
  218. return Promise.all([promisifyRequest(store.getAllKeys()), promisifyRequest(store.getAll())]).then(function (_ref) {
  219. var _ref2 = _slicedToArray(_ref, 2),
  220. keys = _ref2[0],
  221. values = _ref2[1];
  222. return keys.map(function (key, i) {
  223. return [key, values[i]];
  224. });
  225. });
  226. }
  227. var items = [];
  228. return customStore('readonly', function (store) {
  229. return eachCursor(store, function (cursor) {
  230. return items.push([cursor.key, cursor.value]);
  231. }).then(function () {
  232. return items;
  233. });
  234. });
  235. });
  236. }
  237. export { clear, createStore, del, delMany, entries, get, getMany, keys, promisifyRequest, set, setMany, update, values };