-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for providers implementing async load and clear methods. #16
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -110,6 +110,11 @@ function Storeit(namespace, storageProvider) { | |
return value; | ||
} | ||
|
||
function setIndex() { | ||
var keys = Object.keys(cache); | ||
storageProvider.setMetadata(ikey("primary"), keys); | ||
} | ||
|
||
function removeKey(key) { | ||
if (has(key)) { | ||
var removedValue = removeKeyFromCache(key); | ||
|
@@ -169,6 +174,13 @@ function Storeit(namespace, storageProvider) { | |
return results; | ||
} | ||
|
||
function setSerializerName() { | ||
var data = { | ||
itemSerializer: storageProvider.itemSerializer | ||
}; | ||
storageProvider.setMetadata(namespace, data); | ||
} | ||
|
||
function setValue(key, value, metadata) { | ||
if (typeof key !== "string" && isObject(key)) { | ||
metadata = value; | ||
|
@@ -195,11 +207,6 @@ function Storeit(namespace, storageProvider) { | |
return storageProvider.getMetadata(ikey("primary")) || []; | ||
} | ||
|
||
function setIndex() { | ||
var keys = Object.keys(cache); | ||
storageProvider.setMetadata(ikey("primary"), keys); | ||
} | ||
|
||
// Read in the base namespace key. | ||
function initializeItemSerializer(hasItems) { | ||
// Is there is a itemSerializer specified, we MUST use it. | ||
|
@@ -211,19 +218,11 @@ function Storeit(namespace, storageProvider) { | |
storageProvider.itemSerializer = itemSerializerName; | ||
} | ||
|
||
function setSerializerName() { | ||
var data = { | ||
itemSerializer: storageProvider.itemSerializer | ||
}; | ||
storageProvider.setMetadata(namespace, data); | ||
} | ||
|
||
function publishProxy() { | ||
publish.apply(null, arguments); | ||
} | ||
|
||
// Here are a few built-in methods and properties. | ||
that.clear = function () { | ||
function syncClear() { | ||
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)); | ||
|
@@ -237,6 +236,26 @@ function Storeit(namespace, storageProvider) { | |
if (publishReady) { | ||
originalPublish(EventName.ready); | ||
} | ||
} | ||
|
||
function asyncClear() { | ||
storageProvider.clear(namespace).done(function () { | ||
var publishReady = initialize(); // Initialize only if needed and return true if initalization was performed. | ||
cache = {}; | ||
publish(EventName.cleared); | ||
if (publishReady) { | ||
originalPublish(EventName.ready); | ||
} | ||
}); | ||
} | ||
|
||
// Here are a few built-in methods and properties. | ||
that.clear = function () { | ||
if (storageProvider.clear) { // If provider implements clear() - use it. | ||
asyncClear(); | ||
} else { | ||
syncClear(); | ||
} | ||
}; | ||
|
||
Object.defineProperty(that, "options", { | ||
|
@@ -261,7 +280,7 @@ function Storeit(namespace, storageProvider) { | |
enumerable: true | ||
}); | ||
|
||
that.load = function () { | ||
function syncLoad() { | ||
throwIfUninitialized = _.noop; // Allow other methods to work without throwing. | ||
var index = getIndex(); | ||
initializeItemSerializer(!!index.length); | ||
|
@@ -270,6 +289,25 @@ function Storeit(namespace, storageProvider) { | |
setCache(key, value); // Build cache and publish an "added" events. | ||
}); | ||
originalPublish(EventName.ready); // Publish even if options.publish is false. | ||
} | ||
|
||
function asyncLoad() { | ||
throwIfUninitialized = _.noop; // Allow other methods to work without throwing. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. |
||
initializeItemSerializer(true); // TODO: redesign the way we initialze serializers. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. |
||
storageProvider.load(namespace).done(function (data) { | ||
Object.keys(data).forEach(function (key) { // For each key in "namespace#index:primary"... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. |
||
setCache(key, data[key]); // Build cache and publish an "added" events. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. |
||
}); | ||
originalPublish(EventName.ready); // Publish even if options.publish is false. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. |
||
}); | ||
} | ||
|
||
that.load = function () { | ||
if (storageProvider.load) { // If provider implements load() - use it. | ||
asyncLoad(); | ||
} else { // If prvider does not implement load(), data is loaded using a series of getItem calls. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. |
||
syncLoad(); | ||
} | ||
}; | ||
|
||
// Expose these internal functions to the mixins. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line is too long.