localforage-getitems.js 8.7 KB

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