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.
feat: add getItems() overload retrieving all data
- Loading branch information
Showing
2 changed files
with
62 additions
and
24 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,52 @@ | ||
import { executeCallback, getItemKeyValue } from './utils'; | ||
|
||
export 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; | ||
} | ||
|
||
export function getAllItemsUsingKeys() { | ||
var localforageInstance = this; | ||
return localforageInstance.keys().then(function(keys){ | ||
return localforageInstance.getItems(keys); | ||
}); | ||
} | ||
|
||
export function getAllItemsUsingKeysParallel() { | ||
var localforageInstance = this; | ||
return localforageInstance.keys().then(function(keys){ | ||
var itemPromises = []; | ||
for (var i = 0, len = keys.length; i < len; i++) { | ||
itemPromises.push(localforageInstance.getItem(keys[i])); | ||
} | ||
return Promise.all(itemPromises); | ||
}); | ||
} | ||
|
||
export function getAllItemsUsingIterate() { | ||
var localforageInstance = this; | ||
var accumulator = {}; | ||
return localforageInstance.iterate(function(value, key/*, iterationNumber*/) { | ||
accumulator[key] = value; | ||
}).then(function() { | ||
return accumulator; | ||
}); | ||
} |
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