-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxamsdb.js
86 lines (83 loc) · 2.7 KB
/
xamsdb.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
// created by michael piper
// the code is tested and trusted
function Xamsdb(name, debug = false) {
this.name = name
this.debug = debug
this.db = {}
this.oldData =""
if (localStorage.getItem(this.name)) {
var nt = localStorage.getItem(this.name)
this.oldData =nt
this.db = JSON.parse(nt)
} else {
this.oldData ="{}"
localStorage.setItem(this.name, '{}')
}
if (this.debug) console.log("DEBUG:ON")
else console.log("DEBUG:OFF")
}
Xamsdb.prototype = {
constructor: Xamsdb,
sayDB: function() {
if (this.debug === true) console.log(this.name)
return this.name
},
lockdb:function(){
localStorage.setItem(this.name+"+LOCK",localStorage.getItem(this.name))
localStorage.removeItem(this.name)
},
unlockdb:function(){
localStorage.setItem(this.name,localStorage.getItem(this.name+"+LOCK"))
localStorage.removeItem(this.name+"+LOCK")
},
getdb: function() {
if (this.debug === true) console.log(this.db, this.name)
return this.db
},
updatedb: function() {
var parent = this
function auto() {
for (var i in localStorage.length){
if(localStorage.key(i)===parent.name+"+LOCK"){
return null;
}
}
var data = parent.db,storage=localStorage.getItem(parent.name)
if (data === undefined) data = {}
data = JSON.stringify(data)
console.log(parent.oldData)
if (data !==parent.oldData && data !== storage){
// planning on setting data
parent.lockdb()
localStorage.setItem(parent.name+"+LOCK", data)
parent.oldData = data
parent.unlockdb()
if (parent.debug) console.log('xamsdb updated')
}else{
if (data !== storage) {
// planning on fetching data
parent.db = JSON.parse(storage)
parent.oldData = storage
if (parent.debug) console.info('xamsdb updated from storage')
}
else {
if (parent.debug) console.info('xamsdb looking for updated from storage')
}
}
}
if (this.Auto === true) {
setInterval(auto, 1000)
}
auto();
},
flush: function() {
this.db = {};
localStorage.setItem(this.name, '{}')
if (this.debug) console.log('xamsdb flushed')
},
AutoUpadateDB: function() {
this.Auto = true
this.updatedb()
},
}
export default Xamsdb