-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathlocaldata.js
136 lines (130 loc) · 3.75 KB
/
localdata.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
(function() {
var win = typeof window !== 'undefined' ? window : {}
var indexedDB = win.indexedDB || win.mozIndexedDB || win.webkitIndexedDB || win.msIndexedDB;
if (typeof window !== 'undefined' && !indexedDB) {
console.error('indexDB not supported');
return;
}
var db,
request = indexedDB.open('ldb', 1);
request.onsuccess = function(evt) {
db = this.result;
};
request.onerror = function(event) {
console.error('indexedDB request error');
console.log(event);
};
request.onupgradeneeded = function(event) {
db = null;
var store = event.target.result.createObjectStore('s', {
keyPath: 'k'
});
store.transaction.oncomplete = function (e){
db = e.target.db;
};
};
// if using proxy mode comment this
var ldb = {
ready: false,
get: function(key, callback) {
if (!db) {
setTimeout(function () { ldb.get(key, callback); }, 50);
return;
}
db.transaction('s').objectStore('s').get(key).onsuccess = function(event) {
var result = (event.target.result && event.target.result['v']) || null;
callback(result);
};
},
set: function(key, value, callback) {
if (!db) {
setTimeout(function () { ldb.set(key, value, callback); }, 50);
return;
}
let txn = db.transaction('s', 'readwrite');
txn.oncomplete = function(event) {
var toString$ = {}.toString;
if (toString$.call(callback).slice(8, -1) === 'Function') {
callback();
}
}
txn.objectStore('s').put({
'k': key,
'v': value,
});
txn.commit();
},
delete: function(key, callback) {
if (!db) {
setTimeout(function () { ldb.delete(key, callback); }, 50);
return;
}
db.transaction('s', 'readwrite').objectStore('s').delete(key).onsuccess = function(event) {
if (callback) callback();
};
},
list: function(callback) {
if (!db) {
setTimeout(function () { ldb.list(callback); }, 50);
return;
}
db.transaction('s').objectStore('s').getAllKeys().onsuccess = function(event) {
var result = (event.target.result) || null;
callback(result);
};
},
getAll: function(callback) {
if (!db) {
setTimeout(function () { ldb.getAll(callback); }, 50);
return;
}
db.transaction('s').objectStore('s').getAll().onsuccess = function(event) {
var result = (event.target.result) || null;
callback(result);
};
},
clear: function(callback) {
if (!db) {
setTimeout(function () { ldb.clear(callback); }, 50);
return;
}
db.transaction('s', 'readwrite').objectStore('s').clear().onsuccess = function(event) {
if (callback) callback();
};
},
}
const exported = {
'get': ldb.get,
'set': ldb.set,
'delete': ldb.delete,
'list': ldb.list,
'getAll': ldb.getAll,
'clear': ldb.clear,
};
win['ldb'] = exported
if (typeof module !== 'undefined') {
module.exports = exported;
}
// Use only for apps that will only work on latest devices only
// window.ldb = new Proxy({}, {
// get: function(func, key, callback) {
// return (key === 'get') ? getValue : function(callback) {
// return getValue(key, callback)
// };
// },
// set: function(func, key, value) {
// let txn = db.transaction('s', 'readwrite');
// txn.oncomplete = function(event) {
// var toString$ = {}.toString;
// if (toString$.call(callback).slice(8, -1) === 'Function') {
// callback();
// }
// }
// txn.objectStore('s').put({
// 'k': key,
// 'v': value,
// });
// txn.commit();
// }
// });
})();