localforage-getitems.es6.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import localforage from 'localforage';
  2. function getSerializerPromise(localForageInstance) {
  3. if (getSerializerPromise.result) {
  4. return getSerializerPromise.result;
  5. }
  6. if (!localForageInstance || typeof localForageInstance.getSerializer !== 'function') {
  7. return Promise.reject(new Error('localforage.getSerializer() was not available! ' + 'localforage v1.4+ is required!'));
  8. }
  9. getSerializerPromise.result = localForageInstance.getSerializer();
  10. return getSerializerPromise.result;
  11. }
  12. function executeCallback(promise, callback) {
  13. if (callback) {
  14. promise.then(function (result) {
  15. callback(null, result);
  16. }, function (error) {
  17. callback(error);
  18. });
  19. }
  20. return promise;
  21. }
  22. function getItemKeyValue(key, callback) {
  23. var localforageInstance = this;
  24. var promise = localforageInstance.getItem(key).then(function (value) {
  25. return {
  26. key: key,
  27. value: value
  28. };
  29. });
  30. executeCallback(promise, callback);
  31. return promise;
  32. }
  33. function getItemsGeneric(keys /*, callback*/) {
  34. var localforageInstance = this;
  35. var promise = new Promise(function (resolve, reject) {
  36. var itemPromises = [];
  37. for (var i = 0, len = keys.length; i < len; i++) {
  38. itemPromises.push(getItemKeyValue.call(localforageInstance, keys[i]));
  39. }
  40. Promise.all(itemPromises).then(function (keyValuePairs) {
  41. var result = {};
  42. for (var i = 0, len = keyValuePairs.length; i < len; i++) {
  43. var keyValuePair = keyValuePairs[i];
  44. result[keyValuePair.key] = keyValuePair.value;
  45. }
  46. resolve(result);
  47. }).catch(reject);
  48. });
  49. return promise;
  50. }
  51. function getAllItemsUsingIterate() {
  52. var localforageInstance = this;
  53. var accumulator = {};
  54. return localforageInstance.iterate(function (value, key /*, iterationNumber*/) {
  55. accumulator[key] = value;
  56. }).then(function () {
  57. return accumulator;
  58. });
  59. }
  60. function getIDBKeyRange() {
  61. /* global IDBKeyRange, webkitIDBKeyRange, mozIDBKeyRange */
  62. if (typeof IDBKeyRange !== 'undefined') {
  63. return IDBKeyRange;
  64. }
  65. if (typeof webkitIDBKeyRange !== 'undefined') {
  66. return webkitIDBKeyRange;
  67. }
  68. if (typeof mozIDBKeyRange !== 'undefined') {
  69. return mozIDBKeyRange;
  70. }
  71. }
  72. var idbKeyRange = getIDBKeyRange();
  73. function getItemsIndexedDB(keys /*, callback*/) {
  74. keys = keys.slice();
  75. var localforageInstance = this;
  76. function comparer(a, b) {
  77. return a < b ? -1 : a > b ? 1 : 0;
  78. }
  79. var promise = new Promise(function (resolve, reject) {
  80. localforageInstance.ready().then(function () {
  81. // Thanks https://hacks.mozilla.org/2014/06/breaking-the-borders-of-indexeddb/
  82. var dbInfo = localforageInstance._dbInfo;
  83. var store = dbInfo.db.transaction(dbInfo.storeName, 'readonly').objectStore(dbInfo.storeName);
  84. var set = keys.sort(comparer);
  85. var keyRangeValue = idbKeyRange.bound(keys[0], keys[keys.length - 1], false, false);
  86. var req;
  87. if ('getAll' in store) {
  88. req = store.getAll(keyRangeValue);
  89. req.onsuccess = function () {
  90. var value = req.result;
  91. if (value === undefined) {
  92. value = null;
  93. }
  94. resolve(value);
  95. };
  96. } else {
  97. req = store.openCursor(keyRangeValue);
  98. var result = {};
  99. var i = 0;
  100. req.onsuccess = function () /*event*/{
  101. var cursor = req.result; // event.target.result;
  102. if (!cursor) {
  103. resolve(result);
  104. return;
  105. }
  106. var key = cursor.key;
  107. while (key > set[i]) {
  108. i++; // The cursor has passed beyond this key. Check next.
  109. if (i === set.length) {
  110. // There is no next. Stop searching.
  111. resolve(result);
  112. return;
  113. }
  114. }
  115. if (key === set[i]) {
  116. // The current cursor value should be included and we should continue
  117. // a single step in case next item has the same key or possibly our
  118. // next key in set.
  119. var value = cursor.value;
  120. if (value === undefined) {
  121. value = null;
  122. }
  123. result[key] = value;
  124. // onfound(cursor.value);
  125. cursor.continue();
  126. } else {
  127. // cursor.key not yet at set[i]. Forward cursor to the next key to hunt for.
  128. cursor.continue(set[i]);
  129. }
  130. };
  131. }
  132. req.onerror = function () /*event*/{
  133. reject(req.error);
  134. };
  135. }).catch(reject);
  136. });
  137. return promise;
  138. }
  139. function getItemsWebsql(keys /*, callback*/) {
  140. var localforageInstance = this;
  141. var promise = new Promise(function (resolve, reject) {
  142. localforageInstance.ready().then(function () {
  143. return getSerializerPromise(localforageInstance);
  144. }).then(function (serializer) {
  145. var dbInfo = localforageInstance._dbInfo;
  146. dbInfo.db.transaction(function (t) {
  147. var queryParts = new Array(keys.length);
  148. for (var i = 0, len = keys.length; i < len; i++) {
  149. queryParts[i] = '?';
  150. }
  151. t.executeSql('SELECT * FROM ' + dbInfo.storeName + ' WHERE (key IN (' + queryParts.join(',') + '))', keys, function (t, results) {
  152. var result = {};
  153. var rows = results.rows;
  154. for (var i = 0, len = rows.length; i < len; i++) {
  155. var item = rows.item(i);
  156. var value = item.value;
  157. // Check to see if this is serialized content we need to
  158. // unpack.
  159. if (value) {
  160. value = serializer.deserialize(value);
  161. }
  162. result[item.key] = value;
  163. }
  164. resolve(result);
  165. }, function (t, error) {
  166. reject(error);
  167. });
  168. });
  169. }).catch(reject);
  170. });
  171. return promise;
  172. }
  173. function localforageGetItems(keys, callback) {
  174. var localforageInstance = this;
  175. var promise;
  176. if (!arguments.length || keys === null) {
  177. promise = getAllItemsUsingIterate.apply(localforageInstance);
  178. } else {
  179. var currentDriver = localforageInstance.driver();
  180. if (currentDriver === localforageInstance.INDEXEDDB) {
  181. promise = getItemsIndexedDB.apply(localforageInstance, arguments);
  182. } else if (currentDriver === localforageInstance.WEBSQL) {
  183. promise = getItemsWebsql.apply(localforageInstance, arguments);
  184. } else {
  185. promise = getItemsGeneric.apply(localforageInstance, arguments);
  186. }
  187. }
  188. executeCallback(promise, callback);
  189. return promise;
  190. }
  191. function extendPrototype(localforage$$1) {
  192. var localforagePrototype = Object.getPrototypeOf(localforage$$1);
  193. if (localforagePrototype) {
  194. localforagePrototype.getItems = localforageGetItems;
  195. localforagePrototype.getItems.indexedDB = function () {
  196. return getItemsIndexedDB.apply(this, arguments);
  197. };
  198. localforagePrototype.getItems.websql = function () {
  199. return getItemsWebsql.apply(this, arguments);
  200. };
  201. localforagePrototype.getItems.generic = function () {
  202. return getItemsGeneric.apply(this, arguments);
  203. };
  204. }
  205. }
  206. var extendPrototypeResult = extendPrototype(localforage);
  207. export { localforageGetItems, extendPrototype, extendPrototypeResult, getItemsGeneric };