-
Notifications
You must be signed in to change notification settings - Fork 7
/
angular-chrome-storage.js
182 lines (174 loc) · 5.3 KB
/
angular-chrome-storage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
'use strict';
angular.module("chromeStorage",[])
.factory('chromeStorage', function($q) {
var area = null;
try {
area = chrome.storage.local; // change this to chrome.storage.sync for sync capabilities
} catch (err) {
console.log("could not initiate chrome local storage: " + err);
}
/**
* 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();
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 {
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;
},
/**
*
*/
set: function(key, value) {
var saveObject = {};
saveObject[key] = value;
area.set(saveObject, function() {
if (chrome.runtime.lasterror){
console.error(chrome.runtime.lasterror.message);
} else {
// console.log('saved ' + keyValue + " to key " + key);
}
});
},
/**
* 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;
}
}
});