Skip to content

*Added a small fix to prevent setting a global variable. #1

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

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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm

## Directory-based project format
.idea/
339 changes: 185 additions & 154 deletions angular-chrome-storage.js
Original file line number Diff line number Diff line change
@@ -1,162 +1,193 @@
'use strict';

angular.module("chromeStorage",[])
.factory('chromeStorage', function($q) {
var area = chrome.storage.local; // change this to chrome.storage.sync for sync capabilities
angular.module("chromeStorage", [])
.factory('chromeStorage', function ($q) {
var area = chrome.storage.local; // change this to chrome.storage.sync for sync capabilities

/**
* These are provided and updated only for debugging purposes.
*/
var totalBytes = null;
var cache = {};
/**
* These are provided and updated only for debugging purposes.
*/
var totalBytes = null;
var cache = {};

/**
* A call to get the bytes in use, returns a promise of the size of an individual key, or
* of total bytes in use, if no key is specified.
*/
var getBytesInUse = function (keys){
var deferred = $q.defer();
/**
* A call to get the bytes in use, returns a promise of the size of an individual key, or
* of total bytes in use, if no key is specified.
*/
var getBytesInUse = function (keys) {
var deferred = $q.defer();

area.getBytesInUse(keys, function(bytesInUse) {
if (chrome.runtime.lasterror){
console.log("error retrieving bytes in use for keys " + keys);
deferred.reject(chrome.runtime.lasterror.message);
}
else {
console.log("retrieved bytes in use for keys " + keys + ": " + bytesInUse);
deferred.resolve(bytesInUse);
}
});
area.getBytesInUse(keys, function (bytesInUse) {
if (chrome.runtime.lasterror) {
console.log("error retrieving bytes in use for keys " + keys);
deferred.reject(chrome.runtime.lasterror.message);
}
else {
console.log("retrieved bytes in use for keys " + keys + ": " + bytesInUse);
deferred.resolve(bytesInUse);
}
});

return deferred.promise;
}
return deferred.promise;
}

return {
getDebuggingTotalBytesInUse: function() {
return totalBytes;
},
getDebuggingCache: function() {
return cache;
},
/**
* Returns the usage of the current storage quota, as a number between 0.0 and 1.0
*/
getDebuggingPercentUsed: function() {
var percent = totalBytes / area.QUOTA_BYTES;
return percent;
},
getDebuggingSizeOf: function(key) {
return angular.toJson(cache[key]).length;
},
updateDebuggingCache: function() {
var deferred = $q.defer();
area.get(null, function (value) {
if (chrome.runtime.lasterror){
deferred.reject(chrome.runtime.lasterror.message);
} else {
//console.log('get then for all keys : ' + angular.toJson(value));
deferred.resolve(value);
}
});
deferred.promise.then (function (data) {
cache = data;
});
},
updateDebuggingTotalBytes: function() {
getBytesInUse(null).then(function(data) {
console.log("total bytes in use: " + data);
totalBytes = data;
});
},
clearCache: function() {
// console.log('clearing local cache');
area.clear(function() {
if (chrome.runtime.lastError) {
console.error("error clearing local cache" + chrome.runtime.lastError);
} else {
console.log("cache has been cleared");
}
});
},
drop: function(key) {
area.remove(key, function(){
if (chrome.runtime.lasterror){
console.error(chrome.runtime.lasterror.message);
} else {
// console.log("key " + key + " has been dropped from the storage cache")
}
});
},
get: function(key) {
var deferred = $q.defer();
area.get(key, function (value) {
// console.log('getTotalBytesInUse then with key ' + key + " : " + angular.toJson(value));
var keyValue = value[key];
deferred.resolve(keyValue);
});
return deferred.promise;
},
/**
* gets the value of key from the cache, or calls the fallback function, and populates the cache
* with the value of the promise returned
*/
getOrElse: function(key, fallback) {
// console.log('getOrElse called with cached key ' + key);
var deferred = $q.defer();
area.get(key, function(value) {
// console.log('getOrElse then with cached key ' + key + " : " + angular.toJson(value));
var keyValue = value[key];
if (keyValue == undefined || keyValue == null) {
// console.log("no cached value for "+ key + ". using fallback method.");
fallback().then(function(data) {
keyValue = data;
// console.log("caching value for "+ key + " : " + angular.toJson(keyValue));
var saveObject = {};
saveObject[key] = keyValue;
area.set(saveObject, function() {
if (chrome.runtime.lasterror){
console.error(chrome.runtime.lasterror.message);
} else {
//console.log('saved ' + keyValue + " to key " + key);
}
});
deferred.resolve(keyValue);
});
} else {
deferred.resolve(keyValue);
}
});
return deferred.promise;
},
/**
* gets the value of key from the cache, or calls the fallback function, and populates the cache
* with the value of the promise returned
*/
forceGet: function(key, fallback) {
// console.log('getOrElse called with cached key ' + key);
var deferred = $q.defer();
fallback().then(function(data) {
keyValue = data;
// console.log("caching value for "+ key + " : " + angular.toJson(keyValue));
var saveObject = {};
saveObject[key] = keyValue;
area.set(saveObject, function() {
if (chrome.runtime.lasterror){
console.error(chrome.runtime.lasterror.message);
} else {
//console.log('saved ' + keyValue + " to key " + key);
}
});
deferred.resolve(keyValue);
});
return deferred.promise;
},
/**
* Returns the quota of the current storage method, in bytes
*/
getQuota: function() {
return area.QUOTA_BYTES;
}
return {
getDebuggingTotalBytesInUse: function () {
return totalBytes;
},
getDebuggingCache: function () {
return cache;
},
/**
* Returns the usage of the current storage quota, as a number between 0.0 and 1.0
*/
getDebuggingPercentUsed: function () {
var percent = totalBytes / area.QUOTA_BYTES;
return percent;
},
getDebuggingSizeOf: function (key) {
return angular.toJson(cache[key]).length;
},
updateDebuggingCache: function () {
var deferred = $q.defer();
area.get(null, function (value) {
if (chrome.runtime.lasterror) {
deferred.reject(chrome.runtime.lasterror.message);
} else {
//console.log('get then for all keys : ' + angular.toJson(value));
deferred.resolve(value);
}
});
deferred.promise.then(function (data) {
cache = data;
});
},
updateDebuggingTotalBytes: function () {
getBytesInUse(null).then(function (data) {
console.log("total bytes in use: " + data);
totalBytes = data;
});
},
clear: function () {
var deferred = $q.defer();
// console.log('clearing chrome storage');
area.clear(function () {
if (chrome.runtime.lastError) {
deferred.reject(chrome.runtime.lasterror.message);
//console.error("error clearing chrome storage" + chrome.runtime.lastError);
} else {
deferred.resolve();
//console.log("chrome storage has been cleared");
}
});
return deferred.promise;
},
drop: function (key) {
var deferred = $q.defer();

}
});
area.remove(key, function () {
if (chrome.runtime.lasterror) {
deferred.reject(chrome.runtime.lasterror.message);
//console.error(chrome.runtime.lasterror.message);
} else {
deferred.resolve();
// console.log("key " + key + " has been dropped from the storage cache")
}
});

return deferred.promise;
},
get: function (key) {
var deferred = $q.defer();
area.get(key, function (value) {
// console.log('getTotalBytesInUse then with key ' + key + " : " + angular.toJson(value));
var keyValue = value[key];
deferred.resolve(keyValue);
});
return deferred.promise;
},
set: function (key, val) {
var deferred = $q.defer();

if(angular.isDefined(key) && angular.isDefined(val)){
var saveObject = {};
saveObject[key] = val;
area.set(saveObject, function () {
if (chrome.runtime.lasterror) {
deferred.reject(chrome.runtime.lasterror.message);
//console.error(chrome.runtime.lasterror.message);
} else {
//console.log('saved ' + keyValue + " to key " + key);
deferred.resolve();
}
});
} else {
deferred.reject('provided key or value are undefined');
}

return deferred.promise;
},
/**
* gets the value of key from the chrome storage, or calls the fallback function, and populates the storage
* with the value of the promise returned
*/
getOrElse: function (key, fallback) {
// console.log('getOrElse called with cached key ' + key);
var deferred = $q.defer();
area.get(key, function (value) {
// console.log('getOrElse then with cached key ' + key + " : " + angular.toJson(value));
var keyValue = value[key];
if (keyValue == undefined || keyValue == null) {
// console.log("no cached value for "+ key + ". using fallback method.");
fallback().then(function (data) {
keyValue = data;
// console.log("caching value for "+ key + " : " + angular.toJson(keyValue));
var saveObject = {};
saveObject[key] = keyValue;
area.set(saveObject, function () {
if (chrome.runtime.lasterror) {
console.error(chrome.runtime.lasterror.message);
} else {
//console.log('saved ' + keyValue + " to key " + key);
}
});
deferred.resolve(keyValue);
});
} else {
deferred.resolve(keyValue);
}
});
return deferred.promise;
},
/**
* gets the value of key from the cache, or calls the fallback function, and populates the cache
* with the value of the promise returned
*/
forceGet: function (key, fallback) {
// console.log('getOrElse called with cached key ' + key);
var deferred = $q.defer();
fallback().then(function (data) {
var keyValue = data;
// console.log("caching value for "+ key + " : " + angular.toJson(keyValue));
var saveObject = {};
saveObject[key] = keyValue;
area.set(saveObject, function () {
if (chrome.runtime.lasterror) {
console.error(chrome.runtime.lasterror.message);
} else {
//console.log('saved ' + keyValue + " to key " + key);
}
});
deferred.resolve(keyValue);
});
return deferred.promise;
},
/**
* Returns the quota of the current storage method, in bytes
*/
getQuota: function () {
return area.QUOTA_BYTES;
}

}
});