123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- import localforage from 'localforage';
- function getSerializerPromise(localForageInstance) {
- if (getSerializerPromise.result) {
- return getSerializerPromise.result;
- }
- if (!localForageInstance || typeof localForageInstance.getSerializer !== 'function') {
- return Promise.reject(new Error('localforage.getSerializer() was not available! ' + 'localforage v1.4+ is required!'));
- }
- getSerializerPromise.result = localForageInstance.getSerializer();
- return getSerializerPromise.result;
- }
- function executeCallback(promise, callback) {
- if (callback) {
- promise.then(function (result) {
- callback(null, result);
- }, function (error) {
- callback(error);
- });
- }
- return promise;
- }
- function getItemKeyValue(key, callback) {
- var localforageInstance = this;
- var promise = localforageInstance.getItem(key).then(function (value) {
- return {
- key: key,
- value: value
- };
- });
- executeCallback(promise, callback);
- return promise;
- }
- function getItemsGeneric(keys /*, callback*/) {
- var localforageInstance = this;
- var promise = new Promise(function (resolve, reject) {
- var itemPromises = [];
- for (var i = 0, len = keys.length; i < len; i++) {
- itemPromises.push(getItemKeyValue.call(localforageInstance, keys[i]));
- }
- Promise.all(itemPromises).then(function (keyValuePairs) {
- var result = {};
- for (var i = 0, len = keyValuePairs.length; i < len; i++) {
- var keyValuePair = keyValuePairs[i];
- result[keyValuePair.key] = keyValuePair.value;
- }
- resolve(result);
- }).catch(reject);
- });
- return promise;
- }
- function getAllItemsUsingIterate() {
- var localforageInstance = this;
- var accumulator = {};
- return localforageInstance.iterate(function (value, key /*, iterationNumber*/) {
- accumulator[key] = value;
- }).then(function () {
- return accumulator;
- });
- }
- function getIDBKeyRange() {
- /* global IDBKeyRange, webkitIDBKeyRange, mozIDBKeyRange */
- if (typeof IDBKeyRange !== 'undefined') {
- return IDBKeyRange;
- }
- if (typeof webkitIDBKeyRange !== 'undefined') {
- return webkitIDBKeyRange;
- }
- if (typeof mozIDBKeyRange !== 'undefined') {
- return mozIDBKeyRange;
- }
- }
- var idbKeyRange = getIDBKeyRange();
- function getItemsIndexedDB(keys /*, callback*/) {
- keys = keys.slice();
- var localforageInstance = this;
- function comparer(a, b) {
- return a < b ? -1 : a > b ? 1 : 0;
- }
- var promise = new Promise(function (resolve, reject) {
- localforageInstance.ready().then(function () {
- // Thanks https://hacks.mozilla.org/2014/06/breaking-the-borders-of-indexeddb/
- var dbInfo = localforageInstance._dbInfo;
- var store = dbInfo.db.transaction(dbInfo.storeName, 'readonly').objectStore(dbInfo.storeName);
- var set = keys.sort(comparer);
- var keyRangeValue = idbKeyRange.bound(keys[0], keys[keys.length - 1], false, false);
- var req;
- if ('getAll' in store) {
- req = store.getAll(keyRangeValue);
- req.onsuccess = function () {
- var value = req.result;
- if (value === undefined) {
- value = null;
- }
- resolve(value);
- };
- } else {
- req = store.openCursor(keyRangeValue);
- var result = {};
- var i = 0;
- req.onsuccess = function () /*event*/{
- var cursor = req.result; // event.target.result;
- if (!cursor) {
- resolve(result);
- return;
- }
- var key = cursor.key;
- while (key > set[i]) {
- i++; // The cursor has passed beyond this key. Check next.
- if (i === set.length) {
- // There is no next. Stop searching.
- resolve(result);
- return;
- }
- }
- if (key === set[i]) {
- // The current cursor value should be included and we should continue
- // a single step in case next item has the same key or possibly our
- // next key in set.
- var value = cursor.value;
- if (value === undefined) {
- value = null;
- }
- result[key] = value;
- // onfound(cursor.value);
- cursor.continue();
- } else {
- // cursor.key not yet at set[i]. Forward cursor to the next key to hunt for.
- cursor.continue(set[i]);
- }
- };
- }
- req.onerror = function () /*event*/{
- reject(req.error);
- };
- }).catch(reject);
- });
- return promise;
- }
- function getItemsWebsql(keys /*, callback*/) {
- var localforageInstance = this;
- var promise = new Promise(function (resolve, reject) {
- localforageInstance.ready().then(function () {
- return getSerializerPromise(localforageInstance);
- }).then(function (serializer) {
- var dbInfo = localforageInstance._dbInfo;
- dbInfo.db.transaction(function (t) {
- var queryParts = new Array(keys.length);
- for (var i = 0, len = keys.length; i < len; i++) {
- queryParts[i] = '?';
- }
- t.executeSql('SELECT * FROM ' + dbInfo.storeName + ' WHERE (key IN (' + queryParts.join(',') + '))', keys, function (t, results) {
- var result = {};
- var rows = results.rows;
- for (var i = 0, len = rows.length; i < len; i++) {
- var item = rows.item(i);
- var value = item.value;
- // Check to see if this is serialized content we need to
- // unpack.
- if (value) {
- value = serializer.deserialize(value);
- }
- result[item.key] = value;
- }
- resolve(result);
- }, function (t, error) {
- reject(error);
- });
- });
- }).catch(reject);
- });
- return promise;
- }
- function localforageGetItems(keys, callback) {
- var localforageInstance = this;
- var promise;
- if (!arguments.length || keys === null) {
- promise = getAllItemsUsingIterate.apply(localforageInstance);
- } else {
- var currentDriver = localforageInstance.driver();
- if (currentDriver === localforageInstance.INDEXEDDB) {
- promise = getItemsIndexedDB.apply(localforageInstance, arguments);
- } else if (currentDriver === localforageInstance.WEBSQL) {
- promise = getItemsWebsql.apply(localforageInstance, arguments);
- } else {
- promise = getItemsGeneric.apply(localforageInstance, arguments);
- }
- }
- executeCallback(promise, callback);
- return promise;
- }
- function extendPrototype(localforage$$1) {
- var localforagePrototype = Object.getPrototypeOf(localforage$$1);
- if (localforagePrototype) {
- localforagePrototype.getItems = localforageGetItems;
- localforagePrototype.getItems.indexedDB = function () {
- return getItemsIndexedDB.apply(this, arguments);
- };
- localforagePrototype.getItems.websql = function () {
- return getItemsWebsql.apply(this, arguments);
- };
- localforagePrototype.getItems.generic = function () {
- return getItemsGeneric.apply(this, arguments);
- };
- }
- }
- var extendPrototypeResult = extendPrototype(localforage);
- export { localforageGetItems, extendPrototype, extendPrototypeResult, getItemsGeneric };
|