forked from DVLP/localStorageDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
localStorageDB.js
88 lines (80 loc) · 2.3 KB
/
localStorageDB.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
(function() {
var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
if (!indexedDB) {
console.error('indexDB not supported');
return;
}
var db,
keyValue = {
k: '',
v: ''
},
request = indexedDB.open('d2', 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;
};
};
function getValue(key, callback) {
if(!db) {
setTimeout(function () {
getValue(key, callback);
}, 100);
return;
}
db.transaction('s').objectStore('s').get(key).onsuccess = function(event) {
var result = (event.target.result && event.target.result.v) || null;
callback(result);
};
}
// if using proxy mode comment this
window['ldb'] = {
get: getValue,
set: function(key, value, callback) {
// no callback for set needed because every next transaction will be anyway executed after this one
keyValue.k = key;
keyValue.v = 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(keyValue);
txn.commit();
}
}
// 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) {
// keyValue.k = key;
// keyValue.v = 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(keyValue);
// txn.commit();
// }
// });
})();