@whitesev/utils 2.1.3 → 2.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.amd.js +91 -102
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +91 -102
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +91 -102
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +91 -102
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +91 -102
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +91 -102
- package/dist/index.umd.js.map +1 -1
- package/dist/types/src/Utils.d.ts +2 -1
- package/dist/types/src/indexedDB.d.ts +31 -0
- package/package.json +1 -1
- package/src/Utils.ts +7 -3
- package/src/indexedDB.ts +129 -114
|
@@ -950,6 +950,7 @@ declare class Utils {
|
|
|
950
950
|
* @param target 需要监听的对象,可以是全局Window或者某个元素
|
|
951
951
|
* @param eventName 事件名,默认keypress
|
|
952
952
|
* @param callback 自己定义的回调事件,参数1为当前的key,参数2为组合按键,数组类型,包含ctrl、shift、alt和meta(win键或mac的cmd键)
|
|
953
|
+
* @param options 监听事件的配置
|
|
953
954
|
* @example
|
|
954
955
|
Utils.listenKeyboard(window,(keyName,keyValue,otherKey,event)=>{
|
|
955
956
|
if(keyName === "Enter"){
|
|
@@ -1006,7 +1007,7 @@ declare class Utils {
|
|
|
1006
1007
|
搜索 170
|
|
1007
1008
|
收藏 171
|
|
1008
1009
|
**/
|
|
1009
|
-
listenKeyboard(target: Window | Node | HTMLElement | typeof globalThis, eventName: "keyup" | "keypress" | "keydown", callback: (keyName: string, keyValue: string, otherCodeList: string[], event: KeyboardEvent) => void): {
|
|
1010
|
+
listenKeyboard(target: Window | Node | HTMLElement | typeof globalThis, eventName: "keyup" | "keypress" | "keydown", callback: (keyName: string, keyValue: string, otherCodeList: string[], event: KeyboardEvent) => void, options?: AddEventListenerOptions | boolean): {
|
|
1010
1011
|
removeListen(): void;
|
|
1011
1012
|
};
|
|
1012
1013
|
/**
|
|
@@ -23,9 +23,13 @@ declare class indexedDB {
|
|
|
23
23
|
* @param value 数据值
|
|
24
24
|
*/
|
|
25
25
|
save<T extends any>(key: string, value: T): Promise<{
|
|
26
|
+
/** 本操作是否成功 */
|
|
26
27
|
success: boolean;
|
|
28
|
+
/** 状态码 */
|
|
27
29
|
code: number;
|
|
30
|
+
/** 状态码对应的消息 */
|
|
28
31
|
msg: string;
|
|
32
|
+
/** 执行操作触发的事件,如果是在open阶段失败的话该值为空 */
|
|
29
33
|
event?: {
|
|
30
34
|
srcElement: IDBRequest<T>;
|
|
31
35
|
target: IDBRequest<T>;
|
|
@@ -36,9 +40,13 @@ declare class indexedDB {
|
|
|
36
40
|
* @param key 数据key
|
|
37
41
|
*/
|
|
38
42
|
has(key: string): Promise<{
|
|
43
|
+
/** 本操作是否成功 */
|
|
39
44
|
success: boolean;
|
|
45
|
+
/** 状态码 */
|
|
40
46
|
code: number;
|
|
47
|
+
/** 状态码对应的消息 */
|
|
41
48
|
msg: string;
|
|
49
|
+
/** 执行操作触发的事件,如果是在open阶段失败的话该值为空 */
|
|
42
50
|
event?: {
|
|
43
51
|
srcElement: IDBRequest;
|
|
44
52
|
target: IDBRequest;
|
|
@@ -49,14 +57,20 @@ declare class indexedDB {
|
|
|
49
57
|
* @param key 数据key
|
|
50
58
|
*/
|
|
51
59
|
get<T extends any>(key: string): Promise<{
|
|
60
|
+
/** 本操作是否成功 */
|
|
52
61
|
success: boolean;
|
|
62
|
+
/** 状态码 */
|
|
53
63
|
code: number;
|
|
64
|
+
/** 状态码对应的消息 */
|
|
54
65
|
msg: string;
|
|
66
|
+
/** 获取的数据 */
|
|
55
67
|
data: T;
|
|
68
|
+
/** 执行操作触发的事件,如果是在open阶段失败的话该值为空 */
|
|
56
69
|
event?: {
|
|
57
70
|
srcElement: IDBRequest<T>;
|
|
58
71
|
target: IDBRequest<T>;
|
|
59
72
|
} & Event;
|
|
73
|
+
/** 获取的结果,里面的数据提取为data */
|
|
60
74
|
result?: {
|
|
61
75
|
key: string;
|
|
62
76
|
value: T;
|
|
@@ -67,10 +81,15 @@ declare class indexedDB {
|
|
|
67
81
|
* @param key 数据key,可以是正则
|
|
68
82
|
*/
|
|
69
83
|
regexpGet<T extends any>(key: string | RegExp): Promise<{
|
|
84
|
+
/** 本操作是否成功 */
|
|
70
85
|
success: boolean;
|
|
86
|
+
/** 状态码 */
|
|
71
87
|
code: number;
|
|
88
|
+
/** 状态码对应的消息 */
|
|
72
89
|
msg: string;
|
|
90
|
+
/** 获取到的数据列表 */
|
|
73
91
|
data: T[];
|
|
92
|
+
/** 执行操作触发的事件,如果是在open阶段失败的话该值为空 */
|
|
74
93
|
event?: {
|
|
75
94
|
srcElement: IDBRequest<T>;
|
|
76
95
|
target: IDBRequest<T>;
|
|
@@ -81,9 +100,13 @@ declare class indexedDB {
|
|
|
81
100
|
* @param key 数据key
|
|
82
101
|
*/
|
|
83
102
|
delete(key: string): Promise<{
|
|
103
|
+
/** 本操作是否成功 */
|
|
84
104
|
success: boolean;
|
|
105
|
+
/** 状态码 */
|
|
85
106
|
code: number;
|
|
107
|
+
/** 状态码对应的消息 */
|
|
86
108
|
msg: string;
|
|
109
|
+
/** 执行操作触发的事件,如果是在open阶段失败的话该值为空 */
|
|
87
110
|
event?: {
|
|
88
111
|
srcElement: IDBRequest;
|
|
89
112
|
target: IDBRequest;
|
|
@@ -93,9 +116,17 @@ declare class indexedDB {
|
|
|
93
116
|
* 删除所有数据
|
|
94
117
|
*/
|
|
95
118
|
deleteAll(): Promise<{
|
|
119
|
+
/** 本操作是否成功 */
|
|
96
120
|
success: boolean;
|
|
121
|
+
/** 状态码 */
|
|
97
122
|
code: number;
|
|
123
|
+
/** 状态码对应的消息 */
|
|
98
124
|
msg: string;
|
|
125
|
+
/** 执行操作触发的事件,如果是在open阶段失败的话该值为空 */
|
|
126
|
+
event?: {
|
|
127
|
+
srcElement: IDBRequest;
|
|
128
|
+
target: IDBRequest;
|
|
129
|
+
} & Event;
|
|
99
130
|
}>;
|
|
100
131
|
}
|
|
101
132
|
export { indexedDB };
|
package/package.json
CHANGED
package/src/Utils.ts
CHANGED
|
@@ -2309,6 +2309,7 @@ class Utils {
|
|
|
2309
2309
|
* @param target 需要监听的对象,可以是全局Window或者某个元素
|
|
2310
2310
|
* @param eventName 事件名,默认keypress
|
|
2311
2311
|
* @param callback 自己定义的回调事件,参数1为当前的key,参数2为组合按键,数组类型,包含ctrl、shift、alt和meta(win键或mac的cmd键)
|
|
2312
|
+
* @param options 监听事件的配置
|
|
2312
2313
|
* @example
|
|
2313
2314
|
Utils.listenKeyboard(window,(keyName,keyValue,otherKey,event)=>{
|
|
2314
2315
|
if(keyName === "Enter"){
|
|
@@ -2373,7 +2374,8 @@ class Utils {
|
|
|
2373
2374
|
keyValue: string,
|
|
2374
2375
|
otherCodeList: string[],
|
|
2375
2376
|
event: KeyboardEvent
|
|
2376
|
-
) => void
|
|
2377
|
+
) => void,
|
|
2378
|
+
options?: AddEventListenerOptions | boolean
|
|
2377
2379
|
): {
|
|
2378
2380
|
removeListen(): void;
|
|
2379
2381
|
};
|
|
@@ -2385,7 +2387,8 @@ class Utils {
|
|
|
2385
2387
|
keyValue: string,
|
|
2386
2388
|
otherCodeList: string[],
|
|
2387
2389
|
event: KeyboardEvent
|
|
2388
|
-
) => void
|
|
2390
|
+
) => void,
|
|
2391
|
+
options?: AddEventListenerOptions | boolean
|
|
2389
2392
|
): {
|
|
2390
2393
|
removeListen(): void;
|
|
2391
2394
|
} {
|
|
@@ -2418,7 +2421,8 @@ class Utils {
|
|
|
2418
2421
|
callback(keyName, keyValue.toString(), otherCodeList, event);
|
|
2419
2422
|
}
|
|
2420
2423
|
};
|
|
2421
|
-
|
|
2424
|
+
|
|
2425
|
+
target.addEventListener(eventName, keyEvent as any, options);
|
|
2422
2426
|
return {
|
|
2423
2427
|
removeListen() {
|
|
2424
2428
|
target.removeEventListener(eventName, keyEvent as any);
|
package/src/indexedDB.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
declare interface UtilsIDBOpenErrorResult {
|
|
2
2
|
code: number;
|
|
3
3
|
msg: string;
|
|
4
|
-
event:
|
|
4
|
+
event: {
|
|
5
|
+
srcElement: IDBRequest;
|
|
6
|
+
target: IDBRequest;
|
|
7
|
+
} & Event;
|
|
5
8
|
}
|
|
6
9
|
class indexedDB {
|
|
7
10
|
#dbName: string;
|
|
@@ -22,22 +25,22 @@ class indexedDB {
|
|
|
22
25
|
} = {};
|
|
23
26
|
// @ts-ignore
|
|
24
27
|
#store: IDBObjectStore = null as any;
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
+
/** 状态码 */
|
|
29
|
+
#statusCode = {
|
|
30
|
+
operationSuccess: {
|
|
28
31
|
code: 200,
|
|
29
32
|
msg: "操作成功",
|
|
30
33
|
},
|
|
31
|
-
|
|
34
|
+
operationFailed: {
|
|
32
35
|
code: 401,
|
|
33
36
|
msg: "操作失败",
|
|
34
37
|
},
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
openFailed: { code: 91001, msg: "打开数据库失败" },
|
|
39
|
+
saveFailed: { code: 91002, msg: "保存数据失败" },
|
|
40
|
+
getFailed: { code: 91003, msg: "获取数据失败" },
|
|
41
|
+
deleteFailed: { code: 91004, msg: "删除数据失败" },
|
|
42
|
+
deleteAllFailed: { code: 91005, msg: "清空数据库失败" },
|
|
43
|
+
regexpGetFailed: { code: 91006, msg: "正则获取数据失败" },
|
|
41
44
|
};
|
|
42
45
|
/**
|
|
43
46
|
* @param dbName 数据存储名,默认为:default_db
|
|
@@ -55,7 +58,6 @@ class indexedDB {
|
|
|
55
58
|
if (!this.#indexedDB) {
|
|
56
59
|
alert("很抱歉,您的浏览器不支持indexedDB");
|
|
57
60
|
throw new TypeError("很抱歉,您的浏览器不支持indexedDB");
|
|
58
|
-
return;
|
|
59
61
|
}
|
|
60
62
|
}
|
|
61
63
|
/**
|
|
@@ -80,8 +82,9 @@ class indexedDB {
|
|
|
80
82
|
*/
|
|
81
83
|
private open(
|
|
82
84
|
callback: (
|
|
83
|
-
|
|
84
|
-
|
|
85
|
+
/** 数据库实例 */
|
|
86
|
+
idbStore: IDBObjectStore | null,
|
|
87
|
+
error?: UtilsIDBOpenErrorResult
|
|
85
88
|
) => void,
|
|
86
89
|
dbName: string
|
|
87
90
|
) {
|
|
@@ -91,15 +94,12 @@ class indexedDB {
|
|
|
91
94
|
if (!that.#db[dbName]) {
|
|
92
95
|
/* 如果缓存中没有,则进行数据库的创建或打开,提高效率 */
|
|
93
96
|
let request = that.#indexedDB.open(dbName, that.#dbVersion);
|
|
94
|
-
request.onerror = function (event:
|
|
95
|
-
callback(
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
},
|
|
101
|
-
false
|
|
102
|
-
);
|
|
97
|
+
request.onerror = function (event: any) {
|
|
98
|
+
callback(null, {
|
|
99
|
+
code: that.#statusCode.openFailed.code,
|
|
100
|
+
msg: that.#statusCode.openFailed.msg,
|
|
101
|
+
event: event,
|
|
102
|
+
});
|
|
103
103
|
};
|
|
104
104
|
request.onsuccess = function (event: Event) {
|
|
105
105
|
if (!that.#db[dbName]) {
|
|
@@ -107,7 +107,7 @@ class indexedDB {
|
|
|
107
107
|
that.#db[dbName] = target.result;
|
|
108
108
|
}
|
|
109
109
|
let store = that.createStore(dbName);
|
|
110
|
-
callback(store
|
|
110
|
+
callback(store);
|
|
111
111
|
};
|
|
112
112
|
request.onupgradeneeded = function (event: Event) {
|
|
113
113
|
let target = event.target as IDBRequest;
|
|
@@ -116,13 +116,13 @@ class indexedDB {
|
|
|
116
116
|
keyPath: "key",
|
|
117
117
|
});
|
|
118
118
|
store.transaction.oncomplete = function (event: Event) {
|
|
119
|
-
callback(store
|
|
119
|
+
callback(store);
|
|
120
120
|
};
|
|
121
121
|
};
|
|
122
122
|
} else {
|
|
123
123
|
/* 如果缓存中已经打开了数据库,就直接使用 */
|
|
124
|
-
let store =
|
|
125
|
-
callback(store
|
|
124
|
+
let store = this.createStore(dbName);
|
|
125
|
+
callback(store);
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
128
|
/**
|
|
@@ -134,10 +134,13 @@ class indexedDB {
|
|
|
134
134
|
key: string,
|
|
135
135
|
value: T
|
|
136
136
|
): Promise<{
|
|
137
|
+
/** 本操作是否成功 */
|
|
137
138
|
success: boolean;
|
|
139
|
+
/** 状态码 */
|
|
138
140
|
code: number;
|
|
141
|
+
/** 状态码对应的消息 */
|
|
139
142
|
msg: string;
|
|
140
|
-
|
|
143
|
+
/** 执行操作触发的事件,如果是在open阶段失败的话该值为空 */
|
|
141
144
|
event?: {
|
|
142
145
|
srcElement: IDBRequest<T>;
|
|
143
146
|
target: IDBRequest<T>;
|
|
@@ -145,37 +148,34 @@ class indexedDB {
|
|
|
145
148
|
}> {
|
|
146
149
|
let that = this;
|
|
147
150
|
return new Promise((resolve) => {
|
|
148
|
-
let dbName =
|
|
151
|
+
let dbName = this.#dbName;
|
|
149
152
|
let inData = {
|
|
150
153
|
key: key,
|
|
151
154
|
value: value,
|
|
152
155
|
};
|
|
153
|
-
|
|
154
|
-
if (
|
|
156
|
+
this.open(function (idbStore) {
|
|
157
|
+
if (idbStore == null) {
|
|
155
158
|
resolve({
|
|
156
159
|
success: false,
|
|
157
|
-
code: that.#
|
|
158
|
-
msg: that.#
|
|
160
|
+
code: that.#statusCode.saveFailed.code,
|
|
161
|
+
msg: that.#statusCode.saveFailed.msg,
|
|
159
162
|
});
|
|
160
163
|
} else {
|
|
161
|
-
idbStore = idbStore as IDBObjectStore;
|
|
162
164
|
let request = idbStore.put(inData);
|
|
163
165
|
request.onsuccess = function (event: any) {
|
|
164
166
|
/* 保存成功有success 字段 */
|
|
165
167
|
resolve({
|
|
166
168
|
success: true,
|
|
167
|
-
code: that.#
|
|
168
|
-
msg: that.#
|
|
169
|
-
|
|
169
|
+
code: that.#statusCode.operationSuccess.code,
|
|
170
|
+
msg: that.#statusCode.operationSuccess.msg,
|
|
170
171
|
event: event,
|
|
171
172
|
});
|
|
172
173
|
};
|
|
173
174
|
request.onerror = function (event: any) {
|
|
174
175
|
resolve({
|
|
175
176
|
success: false,
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
msg: that.#errorCode.save.msg,
|
|
177
|
+
code: that.#statusCode.saveFailed.code,
|
|
178
|
+
msg: that.#statusCode.saveFailed.msg,
|
|
179
179
|
event: event,
|
|
180
180
|
});
|
|
181
181
|
};
|
|
@@ -189,9 +189,13 @@ class indexedDB {
|
|
|
189
189
|
* @param key 数据key
|
|
190
190
|
*/
|
|
191
191
|
async has(key: string): Promise<{
|
|
192
|
+
/** 本操作是否成功 */
|
|
192
193
|
success: boolean;
|
|
194
|
+
/** 状态码 */
|
|
193
195
|
code: number;
|
|
196
|
+
/** 状态码对应的消息 */
|
|
194
197
|
msg: string;
|
|
198
|
+
/** 执行操作触发的事件,如果是在open阶段失败的话该值为空 */
|
|
195
199
|
event?: {
|
|
196
200
|
srcElement: IDBRequest;
|
|
197
201
|
target: IDBRequest;
|
|
@@ -200,32 +204,31 @@ class indexedDB {
|
|
|
200
204
|
let that = this;
|
|
201
205
|
return new Promise((resolve) => {
|
|
202
206
|
let dbName = this.#dbName;
|
|
203
|
-
this.open(function (idbStore
|
|
207
|
+
this.open(function (idbStore) {
|
|
204
208
|
/* 判断返回的数据中是否有error字段 */
|
|
205
|
-
if (
|
|
209
|
+
if (idbStore == null) {
|
|
206
210
|
resolve({
|
|
207
211
|
success: false,
|
|
208
|
-
code: that.#
|
|
209
|
-
msg: that.#
|
|
212
|
+
code: that.#statusCode.getFailed.code,
|
|
213
|
+
msg: that.#statusCode.getFailed.msg,
|
|
210
214
|
});
|
|
211
215
|
} else {
|
|
212
|
-
idbStore = idbStore as IDBObjectStore;
|
|
213
216
|
let request = idbStore.get(key);
|
|
214
217
|
request.onsuccess = function (event: any) {
|
|
215
218
|
/* result 返回的是 {key: string, value: any} */
|
|
216
219
|
/* 键值对存储 */
|
|
217
220
|
resolve({
|
|
218
221
|
success: true,
|
|
219
|
-
code: that.#
|
|
220
|
-
msg: that.#
|
|
222
|
+
code: that.#statusCode.operationSuccess.code,
|
|
223
|
+
msg: that.#statusCode.operationSuccess.msg,
|
|
221
224
|
event: event,
|
|
222
225
|
});
|
|
223
226
|
};
|
|
224
227
|
request.onerror = function (event: any) {
|
|
225
228
|
resolve({
|
|
226
229
|
success: false,
|
|
227
|
-
code: that.#
|
|
228
|
-
msg: that.#
|
|
230
|
+
code: that.#statusCode.getFailed.code,
|
|
231
|
+
msg: that.#statusCode.getFailed.msg,
|
|
229
232
|
event: event,
|
|
230
233
|
});
|
|
231
234
|
};
|
|
@@ -240,15 +243,20 @@ class indexedDB {
|
|
|
240
243
|
async get<T extends any>(
|
|
241
244
|
key: string
|
|
242
245
|
): Promise<{
|
|
246
|
+
/** 本操作是否成功 */
|
|
243
247
|
success: boolean;
|
|
248
|
+
/** 状态码 */
|
|
244
249
|
code: number;
|
|
250
|
+
/** 状态码对应的消息 */
|
|
245
251
|
msg: string;
|
|
252
|
+
/** 获取的数据 */
|
|
246
253
|
data: T;
|
|
247
|
-
|
|
254
|
+
/** 执行操作触发的事件,如果是在open阶段失败的话该值为空 */
|
|
248
255
|
event?: {
|
|
249
256
|
srcElement: IDBRequest<T>;
|
|
250
257
|
target: IDBRequest<T>;
|
|
251
258
|
} & Event;
|
|
259
|
+
/** 获取的结果,里面的数据提取为data */
|
|
252
260
|
result?: {
|
|
253
261
|
key: string;
|
|
254
262
|
value: T;
|
|
@@ -257,17 +265,16 @@ class indexedDB {
|
|
|
257
265
|
let that = this;
|
|
258
266
|
return new Promise((resolve) => {
|
|
259
267
|
let dbName = this.#dbName;
|
|
260
|
-
this.open(function (idbStore
|
|
268
|
+
this.open(function (idbStore) {
|
|
261
269
|
/* 判断返回的数据中是否有error字段 */
|
|
262
|
-
if (
|
|
270
|
+
if (idbStore == null) {
|
|
263
271
|
resolve({
|
|
264
272
|
success: false,
|
|
265
|
-
code: that.#
|
|
266
|
-
msg: that.#
|
|
273
|
+
code: that.#statusCode.getFailed.code,
|
|
274
|
+
msg: that.#statusCode.getFailed.msg,
|
|
267
275
|
data: void 0 as any,
|
|
268
276
|
});
|
|
269
277
|
} else {
|
|
270
|
-
idbStore = idbStore as IDBObjectStore;
|
|
271
278
|
let request = idbStore.get(key);
|
|
272
279
|
request.onsuccess = function (event: any) {
|
|
273
280
|
let target = event.target as IDBRequest;
|
|
@@ -283,8 +290,8 @@ class indexedDB {
|
|
|
283
290
|
if (data) {
|
|
284
291
|
resolve({
|
|
285
292
|
success: true,
|
|
286
|
-
code: that.#
|
|
287
|
-
msg: that.#
|
|
293
|
+
code: that.#statusCode.operationSuccess.code,
|
|
294
|
+
msg: that.#statusCode.operationSuccess.msg,
|
|
288
295
|
data: data,
|
|
289
296
|
|
|
290
297
|
event: event,
|
|
@@ -293,8 +300,8 @@ class indexedDB {
|
|
|
293
300
|
} else {
|
|
294
301
|
resolve({
|
|
295
302
|
success: false,
|
|
296
|
-
code: that.#
|
|
297
|
-
msg: that.#
|
|
303
|
+
code: that.#statusCode.operationFailed.code,
|
|
304
|
+
msg: that.#statusCode.operationFailed.msg,
|
|
298
305
|
data: void 0 as any,
|
|
299
306
|
|
|
300
307
|
event: event,
|
|
@@ -303,12 +310,10 @@ class indexedDB {
|
|
|
303
310
|
}
|
|
304
311
|
};
|
|
305
312
|
request.onerror = function (event: any) {
|
|
306
|
-
// @ts-ignore
|
|
307
|
-
let target = event.target as IDBRequest;
|
|
308
313
|
resolve({
|
|
309
314
|
success: false,
|
|
310
|
-
code: that.#
|
|
311
|
-
msg: that.#
|
|
315
|
+
code: that.#statusCode.getFailed.code,
|
|
316
|
+
msg: that.#statusCode.getFailed.msg,
|
|
312
317
|
data: void 0 as any,
|
|
313
318
|
|
|
314
319
|
event: event,
|
|
@@ -326,11 +331,16 @@ class indexedDB {
|
|
|
326
331
|
async regexpGet<T extends any>(
|
|
327
332
|
key: string | RegExp
|
|
328
333
|
): Promise<{
|
|
334
|
+
/** 本操作是否成功 */
|
|
329
335
|
success: boolean;
|
|
336
|
+
/** 状态码 */
|
|
330
337
|
code: number;
|
|
338
|
+
/** 状态码对应的消息 */
|
|
331
339
|
msg: string;
|
|
340
|
+
/** 获取到的数据列表 */
|
|
332
341
|
data: T[];
|
|
333
342
|
|
|
343
|
+
/** 执行操作触发的事件,如果是在open阶段失败的话该值为空 */
|
|
334
344
|
event?: {
|
|
335
345
|
srcElement: IDBRequest<T>;
|
|
336
346
|
target: IDBRequest<T>;
|
|
@@ -341,17 +351,16 @@ class indexedDB {
|
|
|
341
351
|
return new Promise((resolve) => {
|
|
342
352
|
/* 正则查询 */
|
|
343
353
|
let dbName = that.#dbName;
|
|
344
|
-
|
|
354
|
+
this.open(function (idbStore) {
|
|
345
355
|
/* 判断返回的数据中是否有error字段 */
|
|
346
|
-
if (
|
|
356
|
+
if (idbStore == null) {
|
|
347
357
|
resolve({
|
|
348
358
|
success: false,
|
|
349
|
-
code: that.#
|
|
350
|
-
msg: that.#
|
|
359
|
+
code: that.#statusCode.regexpGetFailed.code,
|
|
360
|
+
msg: that.#statusCode.regexpGetFailed.msg,
|
|
351
361
|
data: [],
|
|
352
362
|
});
|
|
353
363
|
} else {
|
|
354
|
-
idbStore = idbStore as IDBObjectStore;
|
|
355
364
|
let request = idbStore.getAll();
|
|
356
365
|
request.onsuccess = function (event: any) {
|
|
357
366
|
let target = event.target as IDBRequest<
|
|
@@ -371,20 +380,18 @@ class indexedDB {
|
|
|
371
380
|
}
|
|
372
381
|
resolve({
|
|
373
382
|
success: true,
|
|
374
|
-
code: that.#
|
|
375
|
-
msg: that.#
|
|
383
|
+
code: that.#statusCode.operationSuccess.code,
|
|
384
|
+
msg: that.#statusCode.operationSuccess.msg,
|
|
376
385
|
data: list,
|
|
377
386
|
|
|
378
387
|
event: event,
|
|
379
388
|
});
|
|
380
389
|
};
|
|
381
390
|
request.onerror = function (event: any) {
|
|
382
|
-
// @ts-ignore
|
|
383
|
-
let target = event.target as IDBRequest;
|
|
384
391
|
resolve({
|
|
385
392
|
success: false,
|
|
386
|
-
code: that.#
|
|
387
|
-
msg: that.#
|
|
393
|
+
code: that.#statusCode.getFailed.code,
|
|
394
|
+
msg: that.#statusCode.getFailed.msg,
|
|
388
395
|
data: [],
|
|
389
396
|
|
|
390
397
|
event: event,
|
|
@@ -400,10 +407,13 @@ class indexedDB {
|
|
|
400
407
|
* @param key 数据key
|
|
401
408
|
*/
|
|
402
409
|
async delete(key: string): Promise<{
|
|
410
|
+
/** 本操作是否成功 */
|
|
403
411
|
success: boolean;
|
|
412
|
+
/** 状态码 */
|
|
404
413
|
code: number;
|
|
414
|
+
/** 状态码对应的消息 */
|
|
405
415
|
msg: string;
|
|
406
|
-
|
|
416
|
+
/** 执行操作触发的事件,如果是在open阶段失败的话该值为空 */
|
|
407
417
|
event?: {
|
|
408
418
|
srcElement: IDBRequest;
|
|
409
419
|
target: IDBRequest;
|
|
@@ -413,43 +423,29 @@ class indexedDB {
|
|
|
413
423
|
return new Promise((resolve) => {
|
|
414
424
|
/* 根据key删除某条数据 */
|
|
415
425
|
let dbName = that.#dbName;
|
|
416
|
-
|
|
417
|
-
if (
|
|
426
|
+
this.open(function (idbStore) {
|
|
427
|
+
if (idbStore == null) {
|
|
418
428
|
resolve({
|
|
419
429
|
success: false,
|
|
420
|
-
code: that.#
|
|
421
|
-
msg: that.#
|
|
430
|
+
code: that.#statusCode.deleteFailed.code,
|
|
431
|
+
msg: that.#statusCode.deleteFailed.msg,
|
|
422
432
|
});
|
|
423
433
|
} else {
|
|
424
|
-
|
|
425
|
-
let request = idbStore.
|
|
434
|
+
// 删除键
|
|
435
|
+
let request = idbStore.delete(key);
|
|
426
436
|
request.onsuccess = function (event: any) {
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
success: true,
|
|
434
|
-
code: that.#errorCode.success.code,
|
|
435
|
-
msg: that.#errorCode.success.msg,
|
|
436
|
-
});
|
|
437
|
-
} else {
|
|
438
|
-
resolve({
|
|
439
|
-
success: false,
|
|
440
|
-
code: that.#errorCode.error.code,
|
|
441
|
-
msg: that.#errorCode.error.msg,
|
|
442
|
-
});
|
|
443
|
-
}
|
|
437
|
+
resolve({
|
|
438
|
+
success: true,
|
|
439
|
+
code: that.#statusCode.operationSuccess.code,
|
|
440
|
+
msg: that.#statusCode.operationSuccess.msg,
|
|
441
|
+
event: event,
|
|
442
|
+
});
|
|
444
443
|
};
|
|
445
444
|
request.onerror = function (event: any) {
|
|
446
|
-
// @ts-ignore
|
|
447
|
-
let target = event.target as IDBRequest;
|
|
448
445
|
resolve({
|
|
449
446
|
success: false,
|
|
450
|
-
code: that.#
|
|
451
|
-
msg: that.#
|
|
452
|
-
|
|
447
|
+
code: that.#statusCode.deleteFailed.code,
|
|
448
|
+
msg: that.#statusCode.deleteFailed.msg,
|
|
453
449
|
event: event,
|
|
454
450
|
});
|
|
455
451
|
};
|
|
@@ -462,29 +458,48 @@ class indexedDB {
|
|
|
462
458
|
* 删除所有数据
|
|
463
459
|
*/
|
|
464
460
|
async deleteAll(): Promise<{
|
|
461
|
+
/** 本操作是否成功 */
|
|
465
462
|
success: boolean;
|
|
463
|
+
/** 状态码 */
|
|
466
464
|
code: number;
|
|
465
|
+
/** 状态码对应的消息 */
|
|
467
466
|
msg: string;
|
|
467
|
+
/** 执行操作触发的事件,如果是在open阶段失败的话该值为空 */
|
|
468
|
+
event?: {
|
|
469
|
+
srcElement: IDBRequest;
|
|
470
|
+
target: IDBRequest;
|
|
471
|
+
} & Event;
|
|
468
472
|
}> {
|
|
469
473
|
let that = this;
|
|
470
474
|
return new Promise((resolve) => {
|
|
471
475
|
/* 清空数据库 */
|
|
472
476
|
let dbName = that.#dbName;
|
|
473
|
-
|
|
474
|
-
if (
|
|
477
|
+
this.open(function (idbStore) {
|
|
478
|
+
if (idbStore == null) {
|
|
475
479
|
resolve({
|
|
476
480
|
success: false,
|
|
477
|
-
code: that.#
|
|
478
|
-
msg: that.#
|
|
481
|
+
code: that.#statusCode.deleteAllFailed.code,
|
|
482
|
+
msg: that.#statusCode.deleteAllFailed.msg,
|
|
479
483
|
});
|
|
480
484
|
} else {
|
|
481
|
-
|
|
482
|
-
idbStore.clear();
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
485
|
+
// 清空
|
|
486
|
+
let operateResult = idbStore.clear();
|
|
487
|
+
operateResult.onsuccess = function (event: any) {
|
|
488
|
+
resolve({
|
|
489
|
+
success: true,
|
|
490
|
+
code: that.#statusCode.operationSuccess.code,
|
|
491
|
+
msg: that.#statusCode.operationSuccess.msg,
|
|
492
|
+
event: event,
|
|
493
|
+
});
|
|
494
|
+
};
|
|
495
|
+
operateResult.onerror = function (event: any) {
|
|
496
|
+
resolve({
|
|
497
|
+
success: false,
|
|
498
|
+
code: that.#statusCode.deleteAllFailed.code,
|
|
499
|
+
msg: that.#statusCode.deleteAllFailed.msg,
|
|
500
|
+
event: event,
|
|
501
|
+
});
|
|
502
|
+
};
|
|
488
503
|
}
|
|
489
504
|
}, dbName);
|
|
490
505
|
});
|