Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 53 additions & 15 deletions lib/Storeit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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));
Expand All @@ -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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long.

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", {
Expand All @@ -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);
Expand All @@ -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.

Choose a reason for hiding this comment

The 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.

Choose a reason for hiding this comment

The 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"...

Choose a reason for hiding this comment

The 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.

Choose a reason for hiding this comment

The 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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long.

Choose a reason for hiding this comment

The 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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long.

syncLoad();
}
};

// Expose these internal functions to the mixins.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "storeit",
"version": "2.3.1",
"version": "2.4.0",
"description": "A key/value storage system that publishes events.",
"main": "./lib/Storeit.js",
"homepage": "https://github.com/YuzuJS/storeit",
Expand Down