Skip to content

Commit 79df60d

Browse files
Merge pull request #89 from mh7777777/bfLocalStorageImplementation
Changing from indexeddb to BF's local storage.
2 parents c338c44 + 6417f4c commit 79df60d

File tree

3 files changed

+42
-66
lines changed

3 files changed

+42
-66
lines changed

package-lock.json

Lines changed: 30 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

widget/cache.js

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,12 @@
11
var cache = {
2-
db: null,
3-
dbName: '',
4-
indexName: '',
5-
objectStoreName: '',
6-
perm: 'readwrite',
7-
init: function (options, callback) {
8-
if (!options || !callback) return;
9-
console.warn(options);
10-
11-
options.dbName ? this.dbName = options.dbName : callback('DB Name required', null);
12-
options.indexName ? this.indexName = options.indexName : callback('Index Name required', null);
13-
options.objectStoreName ? this.objectStoreName = options.objectStoreName : callback('Object Store Name required', null);
14-
this.perm = options.perm ? options.perm : 'readwrite';
15-
console.warn('cacheInit');
16-
var _this = this;
17-
var request = window.indexedDB.open(this.dbName, 3);
18-
request.onerror = function (event) {
19-
console.error(event);
20-
};
21-
// runs on init or version update
22-
request.onupgradeneeded = function (event) {
23-
_this.db = event.target.result;
24-
var objectStore = _this.db.createObjectStore(_this.objectStoreName);
25-
objectStore.createIndex(_this.indexName, _this.indexName, {
26-
unique: false
27-
});
28-
};
29-
request.onsuccess = function () {
30-
_this.db = request.result;
31-
callback(null);
32-
}
2+
storeName: 'cacheStore',
3+
saveCache: function(object) {
4+
window.buildfire.localStorage.setItem(`${this.storeName}_${window.buildfire.getContext().instanceId}`, JSON.stringify(object), () => {});
335
},
34-
saveCache: function (object) {
35-
if (!this.db) throw new Error('Error saving cache: db ref not found');
36-
var cacheTransaction = this.db.transaction(this.objectStoreName, this.perm);
37-
var cacheObjStore = cacheTransaction.objectStore(this.objectStoreName);
38-
cacheObjStore.put(object, this.indexName);
39-
},
40-
getCache: function (callback) {
41-
if (!this.db) throw new Error('Error getting cache: db ref not found');
42-
var cacheTransaction = this.db.transaction(this.objectStoreName, this.perm);
43-
var cacheObjStore = cacheTransaction.objectStore(this.objectStoreName);
44-
var request = cacheObjStore.get(this.indexName);
45-
request.onsuccess = function (event) {
46-
callback(null, event.target.result);
47-
}
6+
getCache: function(callback) {
7+
window.buildfire.localStorage.getItem(`${this.storeName}_${window.buildfire.getContext().instanceId}`, (error, value) => {
8+
if (error) return callback(error, null);
9+
callback(null, JSON.parse(value));
10+
});
4811
}
4912
};

widget/controllers/widget.home.controller.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -291,16 +291,10 @@
291291
viewedItems.init();
292292

293293
var success = function (result) {
294-
cache.init({
295-
dbName: 'rss.cache',
296-
indexName: 'feed',
297-
objectStoreName: 'feed.cache'
298-
}, function () {
299-
cache.getCache(function (err, data) {
300-
// if the rss feed url has changed, ignore the cache and update when fetched
301-
if (err || !data || !WidgetHome.data.content || data.rssUrl != WidgetHome.data.content.rssUrl) return;
302-
getFeedDataSuccess(data);
303-
});
294+
cache.getCache(function (err, data) {
295+
// if the rss feed url has changed, ignore the cache and update when fetched
296+
if (err || !data || !WidgetHome.data.content || data.rssUrl != WidgetHome.data.content.rssUrl) return;
297+
getFeedDataSuccess(data);
304298
});
305299

306300
if (Object.keys(result.data).length > 0) {

0 commit comments

Comments
 (0)