forked from localForage/localForage-getItems
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
694 additions
and
2,236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"presets": [ "es2015-rollup" ] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"parser": "babel-eslint", | ||
"parserOptions": { | ||
"ecmaVersion": 6, | ||
"sourceType": "module", | ||
"ecmaFeatures": { | ||
"modules": true | ||
} | ||
}, | ||
"extends": "eslint:recommended", | ||
"env": { | ||
"node": true, | ||
"mocha": true | ||
}, | ||
"rules":{ | ||
"no-console": 0 | ||
}, | ||
"globals": { | ||
"localforage": true, | ||
"Promise": true, | ||
"console": true, | ||
"self": true, | ||
"System": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
build | ||
bower_components | ||
node_modules | ||
components |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
language: node_js | ||
node_js: | ||
- "4.0" | ||
- "5.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
import localforage from 'localforage'; | ||
|
||
function getSerializerPromise(localForageInstance) { | ||
if (getSerializerPromise.result) { | ||
return getSerializerPromise.result; | ||
} | ||
if (!localForageInstance || typeof localForageInstance.getSerializer !== 'function') { | ||
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); | ||
}); | ||
} | ||
} | ||
|
||
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 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) { | ||
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 = 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]) { | ||
|
||
// The cursor has passed beyond this key. Check next. | ||
i++; | ||
|
||
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); | ||
}); | ||
executeCallback(promise, callback); | ||
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); | ||
}); | ||
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); | ||
}); | ||
executeCallback(promise, callback); | ||
return promise; | ||
} | ||
|
||
function localforageGetItems(keys, callback) { | ||
var localforageInstance = this; | ||
var currentDriver = localforageInstance.driver(); | ||
|
||
if (currentDriver === localforageInstance.INDEXEDDB) { | ||
return getItemsIndexedDB.call(localforageInstance, keys, callback); | ||
} else if (currentDriver === localforageInstance.WEBSQL) { | ||
return getItemsWebsql.call(localforageInstance, keys, callback); | ||
} else { | ||
return getItemsGeneric.call(localforageInstance, keys, callback); | ||
} | ||
} | ||
|
||
function extendPrototype(localforage) { | ||
var localforagePrototype = Object.getPrototypeOf(localforage); | ||
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 { getItemsGeneric, localforageGetItems, extendPrototype, extendPrototypeResult }; |
Oops, something went wrong.