compat.cjs 9.4 KB

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