Skip to content

Commit

Permalink
Added support for providers implementing async load and clear methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
zjordan committed Jan 7, 2016
1 parent e6b7aee commit 76759d7
Showing 1 changed file with 38 additions and 18 deletions.
56 changes: 38 additions & 18 deletions lib/Storeit.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,17 +225,27 @@ function Storeit(namespace, storageProvider) {
// Here are a few built-in methods and properties.
that.clear = function () {
var publishReady = initialize(); // Initialize only if needed and return true if initalization was performed.
getIndex().forEach(function (key) { // Remove everything from provider, loaded or not.
storageProvider.removeItem(nskey(key));
storageProvider.removeItem(mkey(key));
});
// Publish and remove (or simply remove) each value from cache.
Object.keys(cache).reverse().forEach(options.publishRemoveOnClear ? removeKey : removeKeyFromCache);
storageProvider.removeItem(ikey("primary"));
storageProvider.removeItem(namespace); // Remove the storageMetadata for the namespace.
publish(EventName.cleared);
if (publishReady) {
originalPublish(EventName.ready);
if (storageProvider.clear) {
storageProvider.clear(namespace).then(function () {
cache = {};
publish(EventName.cleared);
if (publishReady) {
originalPublish(EventName.ready);
}
});
} else {
getIndex().forEach(function (key) { // Remove everything from provider, loaded or not.
storageProvider.removeItem(nskey(key));
storageProvider.removeItem(mkey(key));
});
// Publish and remove (or simply remove) each value from cache.
Object.keys(cache).reverse().forEach(options.publishRemoveOnClear ? removeKey : removeKeyFromCache);
storageProvider.removeItem(ikey("primary"));
storageProvider.removeItem(namespace); // Remove the storageMetadata for the namespace.
publish(EventName.cleared);
if (publishReady) {
originalPublish(EventName.ready);
}
}
};

Expand Down Expand Up @@ -263,13 +273,23 @@ function Storeit(namespace, storageProvider) {

that.load = function () {
throwIfUninitialized = _.noop; // Allow other methods to work without throwing.
var index = getIndex();
initializeItemSerializer(!!index.length);
index.forEach(function (key) { // For each key in "namespace#index:primary"...
var value = storageProvider.getItem(nskey(key));
setCache(key, value); // Build cache and publish an "added" events.
});
originalPublish(EventName.ready); // Publish even if options.publish is false.
if (storageProvider.load) {
initializeItemSerializer(true); // TODO: redesign the way we initialze serializers.
storageProvider.load(namespace).then(function (data) {
Object.keys(data).forEach(function (key) { // For each key in "namespace#index:primary"...
setCache(key, data[key]); // Build cache and publish an "added" events.
});
originalPublish(EventName.ready); // Publish even if options.publish is false.
});
} else {
var index = getIndex();
initializeItemSerializer(!!index.length);
index.forEach(function (key) { // For each key in "namespace#index:primary"...
var value = storageProvider.getItem(nskey(key));
setCache(key, value); // Build cache and publish an "added" events.
});
originalPublish(EventName.ready); // Publish even if options.publish is false.
}
};

// Expose these internal functions to the mixins.
Expand Down

0 comments on commit 76759d7

Please sign in to comment.