-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathyux-storage.js
137 lines (123 loc) · 4.39 KB
/
yux-storage.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
137
/**
* @yux-storage.js
* @author xboxyan
* @email [email protected]
* Created: 20-11-10
* Update: 21-03-13
*/
export class YuxDB {
constructor(projectName = 'yux-project') {
this.projectName = projectName;
this.ready(projectName);
}
ready(projectName=this.projectName) {
return new Promise((resolve, reject) => {
if (this.db) {
resolve(this);
} else {
this.objectStoreName = 'yux-store';
const request = window.indexedDB.open(projectName, 1);
request.onsuccess = (event) => {
this.db = event.target.result;
resolve(this);
};
request.onupgradeneeded = (event) => {
const db = event.target.result;
if (!db.objectStoreNames.contains(this.objectStoreName)) {
db.createObjectStore(this.objectStoreName);
}
};
request.onerror = (event) => {
reject(event);
}
}
})
}
init(request, callback) {
return new Promise((resolve, reject) => {
const success = value => {
if (callback && typeof callback === 'function') {
callback(false, value);
}
resolve(value);
}
const error = event => {
if (callback && typeof callback === 'function') {
callback(event);
}
reject(event);
}
return this.ready().then(() => {
request(success, error);
}).catch(error)
})
}
eventList = []
// 触发事件
triggerEvent() {
document.dispatchEvent(new CustomEvent('yuxStorage', {
detail: [...arguments]
}))
this.eventList.forEach(fn => {
fn(...arguments)
})
}
// 自定义事件
addEventListener(callback) {
this.eventList.push(callback);
}
setItem(key, value, callback) {
return this.init((success, error)=>{
const request = this.db.transaction(this.objectStoreName, 'readwrite').objectStore(this.objectStoreName).put(value, key);
request.onsuccess = () => {
success(value);
this.triggerEvent('setItem',{key,value},this.projectName);
};
request.onerror = error;
// this.dispatchEvent(new CustomEvent('update'))
}, callback)
}
getItem(key, callback) {
return this.init((success, error)=>{
const request = this.db.transaction(this.objectStoreName).objectStore(this.objectStoreName).get(key);
request.onsuccess = () => success(request.result);
request.onerror = error;
}, callback)
}
removeItem(key, callback) {
return this.init((success, error)=>{
const request = this.db.transaction(this.objectStoreName, 'readwrite').objectStore(this.objectStoreName).delete(key);
request.onsuccess = () => {
success(key);
this.triggerEvent('removeItem',{key},this.projectName);
};
request.onerror = error;
}, callback)
}
key(index, callback) {
return this.init((success, error)=>{
const request = this.db.transaction(this.objectStoreName).objectStore(this.objectStoreName).getAllKeys();
request.onsuccess = () => success(request.result[index]);
request.onerror = error;
}, callback)
}
keys(callback) {
return this.init((success, error)=>{
const request = this.db.transaction(this.objectStoreName).objectStore(this.objectStoreName).getAllKeys();
request.onsuccess = () => success(request.result);
request.onerror = error;
}, callback)
}
clear(callback) {
return this.init((success, error)=>{
const request = this.db.transaction(this.objectStoreName, 'readwrite').objectStore(this.objectStoreName).clear();
request.onsuccess = () => {
success(null);
this.triggerEvent('clear',{ },this.projectName);
};;
request.onerror = error;
}, callback)
}
}
window.yuxStorage = new YuxDB()
export default window.yuxStorage;