This repository was archived by the owner on Apr 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangularCache.js
More file actions
189 lines (142 loc) · 6.38 KB
/
Copy pathangularCache.js
File metadata and controls
189 lines (142 loc) · 6.38 KB
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
183
184
185
186
187
188
189
angular.module('storage.cache', []).factory('AngularCache', function AngularCache($q, $timeout) {
var supported = isStorageSupported();
var clearLocalStorage = function () {
};
function setDataType(type, key) {
if (type === "string") {
return localStorage.getItem(key);
} else if (type === "int") {
return parseInt(localStorage.getItem(key));
} else if (type === "float") {
return parseFloat(localStorage.getItem(key));
} else if (type === "bool") {
var val = localStorage.getItem(key);
return val === "true" ? true : false;
} else if (type === "json") {
return JSON.parse(localStorage.getItem(key));
}
// Make sure an error is returned
// FIX IT
return Error;
}
function saveToLocalStorage(type, key, value) {
if (type === "json") {
localStorage.setItem(key, JSON.stringify(value));
} else {
localStorage.setItem(key, value);
}
}
var cacheType = "gets";
var systemData;
return {
/*
* options include:
* cacheType: string
* - gets (default)
* - age
*
* options.sysDataKey: string
* This is the key the AngularCache will use to store all the settings
* and how cache will take place.
* - default: angularCache_system_data
*
* systemOverride: true or false
* If you want
*
*/
init: function (options) {
if (options.cacheType)
cacheType = options.cacheType;
systemData = options.sysDataKey
? localStorage.getItem()
: localStorage.getItem("angularCache_system_data");
if(systemData && !options.systemOverride) {
// Load Settings
} else {
// Load Defaults
}
},
/*
* How this will work:
* Key - General, normal Key
* Value - A json, that stores the data and some of its metadata
* {
* lastHit - Date
* hitCount - Integer
* dateAdded - Date
* dateModified - Date
* modifficationCount - Integer
* data
*/
sync2: function () {
},
//TODO:
// - pass one argument
// - finish proper promise-chaining
// Sync localStorage with the servers and return a promise containing the cache.
// The cache has two properties:
// 1. cache.tempStorage : a return tyoe which contains the data from localStorage. This is the data you would usually want
// show before making servers, assuming we have that data stored in the first place.
// 2. A second promise, which is linked to loading data from the server. This is what
sync: function (input) {
var cache = {};
// Step 1
// Run any pre-configerations the user wants
input.onStart();
// Step 2
// Set the data type
cache.tempStorage = setDataType(input.dataType, input.key);
// Step 3
// Defer the result and return a promise to make things async
var deferred = $q.defer();
$timeout(function () {
// This means there was data already in the cache. There is basically two things we want to accomplish
// 1. Assuming there is already data in the cache, load that. However, we should keep in mind the server
// might have updated data.
// 2. There is nothing in localStorage. Therefore, we will need to do a complete load of the server. At this point,
// we have no idea how the data is being loaded from the server, therefore, the user will be resposible for loading
// from the server.
// In either case, we will still make a server call. The only difference is, at this point in time, cache.tempStorage
// is no longer empty (or is), and lets the user set the data of their view to cache.tempStorage
// Create a second promise. Again, we have no idea how getData works, just that the user is
// responsible for any form of animations, or loading pre-runs.
var secondDeffer = asyncGetFromServer(input.timeout, input.getData);
secondDeffer.then(function (dataFromServer) {
// First save that data to localStorage
saveToLocalStorage(input.dataType , input.key, dataFromServer)
// Next bit, which still might not work, allow the user to make an onSyncComplete call.
// This allows the user to performs certain actions such as hide animation or resync the data shown.
input.onSyncComplete(dataFromServer);
}, function (reason) {
// However, something might have happened, i.e internet failure and the getData function might have failed,
// allowing the user to handle that using onSyncFail
input.onSyncFail(reason);
});
}, input.timeout);
deferred.resolve(cache);
return deferred.promise;
}
};
// Check to see if localStorage is supported
function isStorageSupported() {
try {
return 'localStorage' in window && window['localStorage'] !== null;
} catch (e) {
return false;
}
}
// Returns a promise
// Details
function asyncGetFromServer(timeout, getData) {
var deferredServerCall = $q.defer();
$timeout(function () {
var data = getData();
if (data) {
deferredServerCall.resolve(data);
} else {
deferredServerCall.reject("Data is empty");
}
}, timeout);
return deferredServerCall.promise;
}
});