@whitesev/utils 1.0.5 → 1.0.6
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 +2123 -2269
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +2123 -2269
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +2123 -2269
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +2123 -2269
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +2123 -2269
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +2123 -2269
- package/dist/index.umd.js.map +1 -1
- package/dist/src/Dictionary.d.ts +82 -0
- package/dist/src/Hooks.d.ts +11 -0
- package/dist/src/Httpx.d.ts +1201 -0
- package/dist/src/LockFunction.d.ts +31 -0
- package/dist/src/Log.d.ts +96 -0
- package/dist/src/Progress.d.ts +37 -0
- package/dist/src/UtilsGMMenu.d.ts +156 -0
- package/dist/src/index.d.ts +20 -27
- package/dist/src/indexedDB.d.ts +73 -0
- package/dist/src/tryCatch.d.ts +31 -0
- package/package.json +36 -36
- package/src/Dictionary.ts +152 -0
- package/src/{Hooks/Hooks.js → Hooks.ts} +31 -17
- package/src/{Httpx/index.d.ts → Httpx.ts} +837 -46
- package/src/LockFunction.ts +62 -0
- package/src/Log.ts +281 -0
- package/src/Progress.ts +143 -0
- package/src/UtilsGMMenu.ts +681 -0
- package/src/index.ts +17 -29
- package/src/indexedDB.ts +421 -0
- package/src/tryCatch.ts +107 -0
- package/tsconfig.json +1 -1
- package/dist/src/Dictionary/Dictionary.d.ts +0 -85
- package/dist/src/Hooks/Hooks.d.ts +0 -5
- package/dist/src/Httpx/Httpx.d.ts +0 -50
- package/dist/src/LockFunction/LockFunction.d.ts +0 -16
- package/dist/src/Log/Log.d.ts +0 -66
- package/dist/src/Progress/Progress.d.ts +0 -6
- package/dist/src/UtilsGMMenu/UtilsGMMenu.d.ts +0 -119
- package/dist/src/indexedDB/indexedDB.d.ts +0 -165
- package/dist/src/tryCatch/tryCatch.d.ts +0 -31
- package/src/Dictionary/Dictionary.js +0 -157
- package/src/Dictionary/index.d.ts +0 -52
- package/src/Hooks/index.d.ts +0 -16
- package/src/Httpx/Httpx.js +0 -747
- package/src/LockFunction/LockFunction.js +0 -35
- package/src/LockFunction/index.d.ts +0 -47
- package/src/Log/Log.js +0 -256
- package/src/Log/index.d.ts +0 -91
- package/src/Progress/Progress.js +0 -98
- package/src/Progress/index.d.ts +0 -30
- package/src/UtilsGMMenu/UtilsGMMenu.js +0 -464
- package/src/UtilsGMMenu/index.d.ts +0 -224
- package/src/indexedDB/index.d.ts +0 -128
- package/src/indexedDB/indexedDB.js +0 -355
- package/src/tryCatch/index.d.ts +0 -6
- package/src/tryCatch/tryCatch.js +0 -100
package/src/index.ts
CHANGED
|
@@ -1,27 +1,18 @@
|
|
|
1
1
|
/// <reference path="./ajaxHooker/index.d.ts" />
|
|
2
|
-
/// <reference path="./Dictionary/index.d.ts" />
|
|
3
|
-
/// <reference path="./Hooks/index.d.ts" />
|
|
4
|
-
/// <reference path="./Httpx/index.d.ts" />
|
|
5
|
-
/// <reference path="./indexedDB/index.d.ts" />
|
|
6
|
-
/// <reference path="./LockFunction/index.d.ts" />
|
|
7
|
-
/// <reference path="./Log/index.d.ts" />
|
|
8
|
-
/// <reference path="./Progress/index.d.ts" />
|
|
9
|
-
/// <reference path="./tryCatch/index.d.ts" />
|
|
10
|
-
/// <reference path="./UtilsGMMenu/index.d.ts" />
|
|
11
2
|
import { ColorConversion } from "./ColorConversion";
|
|
12
3
|
import { GBKEncoder } from "./GBKEncoder";
|
|
13
4
|
import { UtilsCore } from "./UtilsCore";
|
|
14
5
|
import { UtilsGMCookie } from "./UtilsGMCookie";
|
|
15
6
|
import { AjaxHooker } from "./ajaxHooker/ajaxHooker.js";
|
|
16
|
-
import { GMMenu } from "./UtilsGMMenu
|
|
17
|
-
import { Hooks } from "./Hooks
|
|
18
|
-
import { Httpx } from "./Httpx
|
|
19
|
-
import { indexedDB } from "./indexedDB
|
|
20
|
-
import { LockFunction } from "./LockFunction
|
|
21
|
-
import { Log } from "./Log
|
|
22
|
-
import { Progress } from "./Progress
|
|
23
|
-
import { TryCatch } from "./tryCatch
|
|
24
|
-
import { UtilsDictionary } from "./Dictionary
|
|
7
|
+
import { GMMenu } from "./UtilsGMMenu";
|
|
8
|
+
import { Hooks } from "./Hooks";
|
|
9
|
+
import { Httpx } from "./Httpx";
|
|
10
|
+
import { indexedDB } from "./indexedDB";
|
|
11
|
+
import { LockFunction } from "./LockFunction";
|
|
12
|
+
import { Log } from "./Log";
|
|
13
|
+
import { Progress } from "./Progress";
|
|
14
|
+
import { TryCatch } from "./tryCatch";
|
|
15
|
+
import { UtilsDictionary } from "./Dictionary";
|
|
25
16
|
|
|
26
17
|
export declare var unsafeWindow: Window & typeof globalThis;
|
|
27
18
|
|
|
@@ -692,9 +683,7 @@ class Utils {
|
|
|
692
683
|
* > true
|
|
693
684
|
* dictionary.concat(dictionary2);
|
|
694
685
|
**/
|
|
695
|
-
Dictionary
|
|
696
|
-
new <K, V>(): UtilsDictionaryConstructor<K, V>;
|
|
697
|
-
} = UtilsDictionary as any;
|
|
686
|
+
Dictionary = UtilsDictionary;
|
|
698
687
|
/**
|
|
699
688
|
* 主动触发事件
|
|
700
689
|
* @param element 元素
|
|
@@ -1845,7 +1834,7 @@ class Utils {
|
|
|
1845
1834
|
// 删除键为menu_key的菜单
|
|
1846
1835
|
GM_Menu.delete("menu_key");
|
|
1847
1836
|
**/
|
|
1848
|
-
GM_Menu
|
|
1837
|
+
GM_Menu = GMMenu;
|
|
1849
1838
|
/**
|
|
1850
1839
|
* 基于Function prototype,能够勾住和释放任何函数
|
|
1851
1840
|
*
|
|
@@ -1870,7 +1859,7 @@ class Utils {
|
|
|
1870
1859
|
}
|
|
1871
1860
|
testFunction.hook(testFunction,myFunction,window);
|
|
1872
1861
|
**/
|
|
1873
|
-
Hooks
|
|
1862
|
+
Hooks = Hooks;
|
|
1874
1863
|
|
|
1875
1864
|
/**
|
|
1876
1865
|
* 为减少代码量和回调,把GM_xmlhttpRequest封装
|
|
@@ -1910,7 +1899,7 @@ class Utils {
|
|
|
1910
1899
|
})
|
|
1911
1900
|
// 优先级为 默认details < 全局details < 单独的details
|
|
1912
1901
|
*/
|
|
1913
|
-
Httpx
|
|
1902
|
+
Httpx = Httpx;
|
|
1914
1903
|
/**
|
|
1915
1904
|
* 浏览器端的indexedDB操作封装
|
|
1916
1905
|
* @example
|
|
@@ -1936,7 +1925,7 @@ class Utils {
|
|
|
1936
1925
|
console.log(resolve,'清除数据库---->>>>>>name')
|
|
1937
1926
|
})
|
|
1938
1927
|
**/
|
|
1939
|
-
indexedDB
|
|
1928
|
+
indexedDB = indexedDB;
|
|
1940
1929
|
/**
|
|
1941
1930
|
* 判断目标函数是否是Native Code
|
|
1942
1931
|
* @param target
|
|
@@ -2590,7 +2579,7 @@ class Utils {
|
|
|
2590
2579
|
await lock.run();
|
|
2591
2580
|
> 1
|
|
2592
2581
|
**/
|
|
2593
|
-
LockFunction
|
|
2582
|
+
LockFunction = LockFunction;
|
|
2594
2583
|
/**
|
|
2595
2584
|
* 日志对象
|
|
2596
2585
|
* @param _GM_info_ 油猴管理器的API GM_info,或者是一个对象,如{"script":{name:"Utils.Log"}}
|
|
@@ -2620,7 +2609,7 @@ class Utils {
|
|
|
2620
2609
|
log.success("颜色为#31dc02");
|
|
2621
2610
|
> 颜色为#31dc02
|
|
2622
2611
|
*/
|
|
2623
|
-
Log
|
|
2612
|
+
Log = Log;
|
|
2624
2613
|
/**
|
|
2625
2614
|
* 合并数组内的JSON的值字符串
|
|
2626
2615
|
* @param data 需要合并的数组
|
|
@@ -3214,7 +3203,7 @@ class Utils {
|
|
|
3214
3203
|
let progress = new Utils.Process({canvasNode:document.querySelector("canvas")});
|
|
3215
3204
|
progress.draw();
|
|
3216
3205
|
* **/
|
|
3217
|
-
Progress
|
|
3206
|
+
Progress = Progress;
|
|
3218
3207
|
/**
|
|
3219
3208
|
* 劫持Event的isTrust为true,注入时刻,ducument-start
|
|
3220
3209
|
* @param isTrustValue (可选)让isTrusted为true
|
|
@@ -4030,7 +4019,6 @@ class Utils {
|
|
|
4030
4019
|
}
|
|
4031
4020
|
/**
|
|
4032
4021
|
* 提供一个封装了 try-catch 的函数,可以执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。
|
|
4033
|
-
* @returns 返回一个对象,其中包含 error 和 run 两个方法。
|
|
4034
4022
|
* @example
|
|
4035
4023
|
* Utils.tryCatch().error().run(()=>{console.log(1)});
|
|
4036
4024
|
* > 1
|
package/src/indexedDB.ts
ADDED
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
import type { AnyObject } from ".";
|
|
2
|
+
|
|
3
|
+
declare interface UtilsIDBOpenErrorResult {
|
|
4
|
+
code: number;
|
|
5
|
+
msg: string;
|
|
6
|
+
event: Event;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
class indexedDB {
|
|
10
|
+
#dbName: string;
|
|
11
|
+
#storeName: string;
|
|
12
|
+
#dbVersion: number;
|
|
13
|
+
/* websql的版本号,由于ios的问题,版本号的写法不一样 */
|
|
14
|
+
#slqVersion = "1";
|
|
15
|
+
/* 监听IndexDB */
|
|
16
|
+
#indexedDB =
|
|
17
|
+
window.indexedDB ||
|
|
18
|
+
(window as any).mozIndexedDB ||
|
|
19
|
+
(window as any).webkitIndexedDB ||
|
|
20
|
+
(window as any).msIndexedDB;
|
|
21
|
+
/* 缓存数据库,避免同一个页面重复创建和销毁 */
|
|
22
|
+
#db: {
|
|
23
|
+
[key: string]: IDBDatabase;
|
|
24
|
+
} = {};
|
|
25
|
+
#store: IDBObjectStore = null as any;
|
|
26
|
+
#errorCode = {
|
|
27
|
+
/* 错误码 */
|
|
28
|
+
success: {
|
|
29
|
+
code: 200,
|
|
30
|
+
msg: "操作成功",
|
|
31
|
+
},
|
|
32
|
+
error: {
|
|
33
|
+
code: 401,
|
|
34
|
+
msg: "操作失败",
|
|
35
|
+
},
|
|
36
|
+
open: { code: 91001, msg: "打开数据库失败" },
|
|
37
|
+
save: { code: 91002, msg: "保存数据失败" },
|
|
38
|
+
get: { code: 91003, msg: "获取数据失败" },
|
|
39
|
+
delete: { code: 91004, msg: "删除数据失败" },
|
|
40
|
+
deleteAll: { code: 91005, msg: "清空数据库失败" },
|
|
41
|
+
regexpGet: { code: 91006, msg: "正则获取数据失败" },
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* @param dbName 数据存储名,默认为:default_db
|
|
45
|
+
* @param storeName 表名,默认为:default_form
|
|
46
|
+
* @param dbVersion indexDB的版本号,默认为:1
|
|
47
|
+
*/
|
|
48
|
+
constructor(
|
|
49
|
+
dbName = "default_db",
|
|
50
|
+
storeName = "default_form",
|
|
51
|
+
dbVersion = 1
|
|
52
|
+
) {
|
|
53
|
+
this.#dbName = dbName;
|
|
54
|
+
this.#storeName = storeName;
|
|
55
|
+
this.#dbVersion = dbVersion;
|
|
56
|
+
if (!this.#indexedDB) {
|
|
57
|
+
alert("很抱歉,您的浏览器不支持indexedDB");
|
|
58
|
+
throw new TypeError("很抱歉,您的浏览器不支持indexedDB");
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* 创建 “表”
|
|
64
|
+
* @param dbName 表名
|
|
65
|
+
*/
|
|
66
|
+
createStore(dbName: string) {
|
|
67
|
+
let txn, store;
|
|
68
|
+
txn = this.#db[dbName].transaction(
|
|
69
|
+
this.#storeName,
|
|
70
|
+
"readwrite"
|
|
71
|
+
) as IDBTransaction;
|
|
72
|
+
/* IndexDB的读写权限 */
|
|
73
|
+
store = txn.objectStore(this.#storeName) as IDBObjectStore;
|
|
74
|
+
this.#store = store;
|
|
75
|
+
return store;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* 打开数据库
|
|
79
|
+
* @param callback 回调
|
|
80
|
+
* @param dbName 数据库名
|
|
81
|
+
*/
|
|
82
|
+
private open(
|
|
83
|
+
callback: (
|
|
84
|
+
idbStore: IDBObjectStore | UtilsIDBOpenErrorResult,
|
|
85
|
+
success: boolean
|
|
86
|
+
) => void,
|
|
87
|
+
dbName: string
|
|
88
|
+
) {
|
|
89
|
+
let that = this;
|
|
90
|
+
/* 打开数据库 */
|
|
91
|
+
/* 如果支持IndexDB */
|
|
92
|
+
if (!that.#db[dbName]) {
|
|
93
|
+
/* 如果缓存中没有,则进行数据库的创建或打开,提高效率 */
|
|
94
|
+
let request = that.#indexedDB.open(dbName, that.#dbVersion);
|
|
95
|
+
request.onerror = function (event: Event) {
|
|
96
|
+
callback(
|
|
97
|
+
{
|
|
98
|
+
code: that.#errorCode.open.code,
|
|
99
|
+
msg: that.#errorCode.open.msg,
|
|
100
|
+
event: event,
|
|
101
|
+
},
|
|
102
|
+
false
|
|
103
|
+
);
|
|
104
|
+
};
|
|
105
|
+
request.onsuccess = function (event: Event) {
|
|
106
|
+
if (!that.#db[dbName]) {
|
|
107
|
+
let target = event.target as IDBRequest;
|
|
108
|
+
that.#db[dbName] = target.result;
|
|
109
|
+
}
|
|
110
|
+
let store = that.createStore(dbName);
|
|
111
|
+
callback(store, true);
|
|
112
|
+
};
|
|
113
|
+
request.onupgradeneeded = function (event: Event) {
|
|
114
|
+
let target = event.target as IDBRequest;
|
|
115
|
+
that.#db[dbName] = target.result;
|
|
116
|
+
let store = that.#db[dbName].createObjectStore(that.#storeName, {
|
|
117
|
+
keyPath: "key",
|
|
118
|
+
});
|
|
119
|
+
store.transaction.oncomplete = function (event: Event) {
|
|
120
|
+
callback(store, true);
|
|
121
|
+
};
|
|
122
|
+
};
|
|
123
|
+
} else {
|
|
124
|
+
/* 如果缓存中已经打开了数据库,就直接使用 */
|
|
125
|
+
let store = that.createStore(dbName);
|
|
126
|
+
callback(store, true);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* 保存数据到数据库
|
|
131
|
+
* @param key 数据key
|
|
132
|
+
* @param value 数据值
|
|
133
|
+
*/
|
|
134
|
+
async save(
|
|
135
|
+
key: string,
|
|
136
|
+
value: any
|
|
137
|
+
): Promise<{
|
|
138
|
+
success: boolean;
|
|
139
|
+
code: number;
|
|
140
|
+
msg: string;
|
|
141
|
+
|
|
142
|
+
event?: Event;
|
|
143
|
+
}> {
|
|
144
|
+
let that = this;
|
|
145
|
+
return new Promise((resolve) => {
|
|
146
|
+
let dbName = that.#dbName;
|
|
147
|
+
let inData = {
|
|
148
|
+
key: key,
|
|
149
|
+
value: value,
|
|
150
|
+
};
|
|
151
|
+
that.open(function (idbStore, success) {
|
|
152
|
+
if (!success) {
|
|
153
|
+
resolve({
|
|
154
|
+
success: false,
|
|
155
|
+
code: that.#errorCode.save.code,
|
|
156
|
+
msg: that.#errorCode.save.msg,
|
|
157
|
+
});
|
|
158
|
+
} else {
|
|
159
|
+
idbStore = idbStore as IDBObjectStore;
|
|
160
|
+
let request = idbStore.put(inData);
|
|
161
|
+
request.onsuccess = function (event: Event) {
|
|
162
|
+
/* 保存成功有success 字段 */
|
|
163
|
+
let target = event.target as IDBRequest;
|
|
164
|
+
resolve({
|
|
165
|
+
success: true,
|
|
166
|
+
code: that.#errorCode.success.code,
|
|
167
|
+
msg: that.#errorCode.success.msg,
|
|
168
|
+
|
|
169
|
+
event: event,
|
|
170
|
+
});
|
|
171
|
+
};
|
|
172
|
+
request.onerror = function (event: Event) {
|
|
173
|
+
let target = event.target as IDBRequest;
|
|
174
|
+
resolve({
|
|
175
|
+
success: false,
|
|
176
|
+
|
|
177
|
+
code: that.#errorCode.save.code,
|
|
178
|
+
msg: that.#errorCode.save.msg,
|
|
179
|
+
event: event,
|
|
180
|
+
});
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
}, dbName);
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* 根据key获取值
|
|
189
|
+
* @param key 数据key
|
|
190
|
+
*/
|
|
191
|
+
async get<T extends any>(
|
|
192
|
+
key: string
|
|
193
|
+
): Promise<{
|
|
194
|
+
success: boolean;
|
|
195
|
+
code: number;
|
|
196
|
+
msg: string;
|
|
197
|
+
data: T;
|
|
198
|
+
|
|
199
|
+
event?: Event;
|
|
200
|
+
result?: any;
|
|
201
|
+
}> {
|
|
202
|
+
let that = this;
|
|
203
|
+
return new Promise((resolve) => {
|
|
204
|
+
let dbName = that.#dbName;
|
|
205
|
+
that.open(function (idbStore, success) {
|
|
206
|
+
/* 判断返回的数据中是否有error字段 */
|
|
207
|
+
if (!success) {
|
|
208
|
+
resolve({
|
|
209
|
+
success: false,
|
|
210
|
+
code: that.#errorCode.get.code,
|
|
211
|
+
msg: that.#errorCode.get.msg,
|
|
212
|
+
data: void 0 as any,
|
|
213
|
+
});
|
|
214
|
+
} else {
|
|
215
|
+
idbStore = idbStore as IDBObjectStore;
|
|
216
|
+
let request = idbStore.get(key);
|
|
217
|
+
request.onsuccess = function (event: any) {
|
|
218
|
+
let target = event.target as IDBRequest;
|
|
219
|
+
let result = target.result;
|
|
220
|
+
/* result 返回的是 {key: string, value: any} */
|
|
221
|
+
/* 键值对存储 */
|
|
222
|
+
let data: T = result ? result.value : void 0;
|
|
223
|
+
if (data) {
|
|
224
|
+
resolve({
|
|
225
|
+
success: true,
|
|
226
|
+
code: that.#errorCode.success.code,
|
|
227
|
+
msg: that.#errorCode.success.msg,
|
|
228
|
+
data: data,
|
|
229
|
+
|
|
230
|
+
event: event,
|
|
231
|
+
result: result,
|
|
232
|
+
});
|
|
233
|
+
} else {
|
|
234
|
+
resolve({
|
|
235
|
+
success: false,
|
|
236
|
+
code: that.#errorCode.error.code,
|
|
237
|
+
msg: that.#errorCode.error.msg,
|
|
238
|
+
data: void 0 as any,
|
|
239
|
+
|
|
240
|
+
event: event,
|
|
241
|
+
result: result,
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
request.onerror = function (event: any) {
|
|
246
|
+
let target = event.target as IDBRequest;
|
|
247
|
+
resolve({
|
|
248
|
+
success: false,
|
|
249
|
+
code: that.#errorCode.get.code,
|
|
250
|
+
msg: that.#errorCode.get.msg,
|
|
251
|
+
data: void 0 as any,
|
|
252
|
+
|
|
253
|
+
event: event,
|
|
254
|
+
});
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
}, dbName);
|
|
258
|
+
});
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* 正则获取数据
|
|
263
|
+
* @param key 数据键
|
|
264
|
+
*/
|
|
265
|
+
async regexpGet<T extends any>(
|
|
266
|
+
key: string
|
|
267
|
+
): Promise<{
|
|
268
|
+
success: boolean;
|
|
269
|
+
code: number;
|
|
270
|
+
msg: string;
|
|
271
|
+
data: T[];
|
|
272
|
+
|
|
273
|
+
event?: Event;
|
|
274
|
+
}> {
|
|
275
|
+
let list: T[] = [];
|
|
276
|
+
let that = this;
|
|
277
|
+
return new Promise((resolve) => {
|
|
278
|
+
/* 正则查询 */
|
|
279
|
+
let dbName = that.#dbName;
|
|
280
|
+
that.open(function (idbStore, success) {
|
|
281
|
+
/* 判断返回的数据中是否有error字段 */
|
|
282
|
+
if (!success) {
|
|
283
|
+
resolve({
|
|
284
|
+
success: false,
|
|
285
|
+
code: that.#errorCode.regexpGet.code,
|
|
286
|
+
msg: that.#errorCode.regexpGet.msg,
|
|
287
|
+
data: [],
|
|
288
|
+
});
|
|
289
|
+
} else {
|
|
290
|
+
idbStore = idbStore as IDBObjectStore;
|
|
291
|
+
let request = idbStore.getAll();
|
|
292
|
+
request.onsuccess = function (event: any) {
|
|
293
|
+
let target = event.target as IDBRequest;
|
|
294
|
+
let result = target.result;
|
|
295
|
+
if (result.length !== 0) {
|
|
296
|
+
result.forEach((item: any, index: any) => {
|
|
297
|
+
if (item["key"].match(key)) {
|
|
298
|
+
let concatList = item["value"];
|
|
299
|
+
concatList["key"] = item["key"];
|
|
300
|
+
list = [...list, concatList];
|
|
301
|
+
}
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
resolve({
|
|
305
|
+
success: true,
|
|
306
|
+
code: that.#errorCode.success.code,
|
|
307
|
+
msg: that.#errorCode.success.msg,
|
|
308
|
+
data: list,
|
|
309
|
+
|
|
310
|
+
event: event,
|
|
311
|
+
});
|
|
312
|
+
};
|
|
313
|
+
request.onerror = function (event: any) {
|
|
314
|
+
let target = event.target as IDBRequest;
|
|
315
|
+
resolve({
|
|
316
|
+
success: false,
|
|
317
|
+
code: that.#errorCode.get.code,
|
|
318
|
+
msg: that.#errorCode.get.msg,
|
|
319
|
+
data: [],
|
|
320
|
+
|
|
321
|
+
event: event,
|
|
322
|
+
});
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
}, dbName);
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/**
|
|
330
|
+
* 删除数据
|
|
331
|
+
* @param {string} key 数据键
|
|
332
|
+
*/
|
|
333
|
+
async delete(key: string): Promise<{
|
|
334
|
+
success: boolean;
|
|
335
|
+
code: number;
|
|
336
|
+
msg: string;
|
|
337
|
+
|
|
338
|
+
event?: Event;
|
|
339
|
+
}> {
|
|
340
|
+
let that = this;
|
|
341
|
+
return new Promise((resolve) => {
|
|
342
|
+
/* 根据key删除某条数据 */
|
|
343
|
+
let dbName = that.#dbName;
|
|
344
|
+
that.open(function (idbStore, success) {
|
|
345
|
+
if (!success) {
|
|
346
|
+
resolve({
|
|
347
|
+
success: false,
|
|
348
|
+
code: that.#errorCode.delete.code,
|
|
349
|
+
msg: that.#errorCode.delete.msg,
|
|
350
|
+
});
|
|
351
|
+
} else {
|
|
352
|
+
idbStore = idbStore as IDBObjectStore;
|
|
353
|
+
let request = idbStore.get(key);
|
|
354
|
+
request.onsuccess = function (event: any) {
|
|
355
|
+
let target = event.target as IDBRequest;
|
|
356
|
+
let recode = target.result;
|
|
357
|
+
if (recode) {
|
|
358
|
+
/* 成功 */
|
|
359
|
+
request = (idbStore as IDBObjectStore).delete(key);
|
|
360
|
+
resolve({
|
|
361
|
+
success: true,
|
|
362
|
+
code: that.#errorCode.success.code,
|
|
363
|
+
msg: that.#errorCode.success.msg,
|
|
364
|
+
});
|
|
365
|
+
} else {
|
|
366
|
+
resolve({
|
|
367
|
+
success: false,
|
|
368
|
+
code: that.#errorCode.error.code,
|
|
369
|
+
msg: that.#errorCode.error.msg,
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
};
|
|
373
|
+
request.onerror = function (event: any) {
|
|
374
|
+
let target = event.target as IDBRequest;
|
|
375
|
+
resolve({
|
|
376
|
+
success: false,
|
|
377
|
+
code: that.#errorCode.delete.code,
|
|
378
|
+
msg: that.#errorCode.delete.msg,
|
|
379
|
+
|
|
380
|
+
event: event,
|
|
381
|
+
});
|
|
382
|
+
};
|
|
383
|
+
}
|
|
384
|
+
}, dbName);
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* 删除所有数据
|
|
390
|
+
*/
|
|
391
|
+
async deleteAll(): Promise<{
|
|
392
|
+
success: boolean;
|
|
393
|
+
code: number;
|
|
394
|
+
msg: string;
|
|
395
|
+
}> {
|
|
396
|
+
let that = this;
|
|
397
|
+
return new Promise((resolve) => {
|
|
398
|
+
/* 清空数据库 */
|
|
399
|
+
let dbName = that.#dbName;
|
|
400
|
+
that.open(function (idbStore, success) {
|
|
401
|
+
if (!success) {
|
|
402
|
+
resolve({
|
|
403
|
+
success: false,
|
|
404
|
+
code: that.#errorCode.deleteAll.code,
|
|
405
|
+
msg: that.#errorCode.deleteAll.msg,
|
|
406
|
+
});
|
|
407
|
+
} else {
|
|
408
|
+
idbStore = idbStore as IDBObjectStore;
|
|
409
|
+
idbStore.clear();
|
|
410
|
+
resolve({
|
|
411
|
+
success: true,
|
|
412
|
+
code: that.#errorCode.success.code,
|
|
413
|
+
msg: that.#errorCode.success.msg,
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
}, dbName);
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
export { indexedDB };
|
package/src/tryCatch.ts
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { Utils } from ".";
|
|
2
|
+
|
|
3
|
+
export declare interface UtilsTryCatchConfig {
|
|
4
|
+
log: boolean;
|
|
5
|
+
}
|
|
6
|
+
/** tryCatch */
|
|
7
|
+
export declare interface UtilsTryCatchType {
|
|
8
|
+
run: UtilsTryCatchType;
|
|
9
|
+
config: UtilsTryCatchType;
|
|
10
|
+
error: UtilsTryCatchType;
|
|
11
|
+
}
|
|
12
|
+
const TryCatch = function (...args: any) {
|
|
13
|
+
/* 定义变量和函数 */
|
|
14
|
+
let callbackFunction = null;
|
|
15
|
+
let context = null;
|
|
16
|
+
let handleError = (error: Error) => {};
|
|
17
|
+
let defaultDetails = {
|
|
18
|
+
log: true,
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const TryCatchCore = {
|
|
22
|
+
/**
|
|
23
|
+
*
|
|
24
|
+
* @param paramDetails 配置
|
|
25
|
+
* @returns
|
|
26
|
+
*/
|
|
27
|
+
config(paramDetails: UtilsTryCatchConfig) {
|
|
28
|
+
defaultDetails = Utils.assign(defaultDetails, paramDetails);
|
|
29
|
+
return TryCatchCore;
|
|
30
|
+
},
|
|
31
|
+
/**
|
|
32
|
+
* 处理错误
|
|
33
|
+
* @param handler
|
|
34
|
+
*/
|
|
35
|
+
error(handler: ((...args: any[]) => any) | string | Function) {
|
|
36
|
+
// @ts-ignore
|
|
37
|
+
handleError = handler;
|
|
38
|
+
return TryCatchCore;
|
|
39
|
+
},
|
|
40
|
+
/**
|
|
41
|
+
* 执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。
|
|
42
|
+
* @param callback 待执行函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
|
|
43
|
+
* @param __context__ 待执行函数的作用域,用于apply指定
|
|
44
|
+
* @returns 如果函数有返回值,则返回该返回值;否则返回 tryCatchObj 函数以支持链式调用。
|
|
45
|
+
* @throws {Error} 如果传入参数不符合要求,则会抛出相应类型的错误。
|
|
46
|
+
*/
|
|
47
|
+
run<A extends any[], R>(
|
|
48
|
+
callback: ((...args: A) => R) | string | Function,
|
|
49
|
+
__context__?: any
|
|
50
|
+
): UtilsTryCatchType {
|
|
51
|
+
callbackFunction = callback;
|
|
52
|
+
context = __context__ || this;
|
|
53
|
+
let result = executeTryCatch(callbackFunction, handleError, context);
|
|
54
|
+
// @ts-ignore
|
|
55
|
+
return result !== void 0 ? result : TryCatchCore;
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* 执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。
|
|
61
|
+
* @param callback - 待执行函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
|
|
62
|
+
* @param handleErrorFunc - 错误处理函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
|
|
63
|
+
* @param funcThis - 待执行函数的作用域,用于apply指定
|
|
64
|
+
* @returns 如果函数有返回值,则返回该返回值;否则返回 undefined。
|
|
65
|
+
*/
|
|
66
|
+
function executeTryCatch<A extends any[], R>(
|
|
67
|
+
callback: string | ((...args: A) => R) | Function,
|
|
68
|
+
handleErrorFunc: string | ((...args: any[]) => any),
|
|
69
|
+
funcThis?: any
|
|
70
|
+
) {
|
|
71
|
+
let result = void 0;
|
|
72
|
+
try {
|
|
73
|
+
if (typeof callback === "string") {
|
|
74
|
+
(function () {
|
|
75
|
+
eval(callback as string);
|
|
76
|
+
}).apply(funcThis, args);
|
|
77
|
+
} else {
|
|
78
|
+
result = (callback as Function).apply(funcThis, args);
|
|
79
|
+
}
|
|
80
|
+
} catch (error) {
|
|
81
|
+
if (defaultDetails.log) {
|
|
82
|
+
callback = callback as Function;
|
|
83
|
+
console.log(
|
|
84
|
+
`%c ${callback?.name ? callback?.name : callback + "出现错误"} `,
|
|
85
|
+
"color: #f20000"
|
|
86
|
+
);
|
|
87
|
+
console.log(`%c 错误原因:${error}`, "color: #f20000");
|
|
88
|
+
console.trace(callback);
|
|
89
|
+
}
|
|
90
|
+
if (handleErrorFunc) {
|
|
91
|
+
if (typeof handleErrorFunc === "string") {
|
|
92
|
+
result = function () {
|
|
93
|
+
return eval(handleErrorFunc);
|
|
94
|
+
// @ts-ignore
|
|
95
|
+
}.apply(funcThis, [...args, error]);
|
|
96
|
+
} else {
|
|
97
|
+
result = handleErrorFunc.apply(funcThis, [...args, error]);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return TryCatchCore;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
export { TryCatch };
|