|
1 | 1 | 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), () => {}); |
33 | 5 | }, |
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 | + }); |
48 | 11 | } |
49 | 12 | }; |
0 commit comments