@whitesev/utils 1.0.5 → 1.0.8
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 +530 -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/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 };
|
package/tsconfig.json
CHANGED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
export class UtilsDictionary {
|
|
2
|
-
items: {};
|
|
3
|
-
/**
|
|
4
|
-
* 检查是否有某一个键
|
|
5
|
-
* @param {string} key 键
|
|
6
|
-
* @returns {boolean}
|
|
7
|
-
*/
|
|
8
|
-
has(key: string): boolean;
|
|
9
|
-
/**
|
|
10
|
-
* 检查已有的键中是否以xx开头
|
|
11
|
-
* @param {string} key 需要匹配的键
|
|
12
|
-
* @returns {boolean}
|
|
13
|
-
*/
|
|
14
|
-
startsWith(key: string): boolean;
|
|
15
|
-
/**
|
|
16
|
-
* 获取以xx开头的键的值
|
|
17
|
-
* @param {string} key 需要匹配的键
|
|
18
|
-
* @returns {any}
|
|
19
|
-
*/
|
|
20
|
-
getStartsWith(key: string): any;
|
|
21
|
-
/**
|
|
22
|
-
* 为字典添加某一个值
|
|
23
|
-
* @param {string} key 键
|
|
24
|
-
* @param {any} val 值,默认为""
|
|
25
|
-
*/
|
|
26
|
-
set(key: string, val?: any): void;
|
|
27
|
-
/**
|
|
28
|
-
* 删除某一个键
|
|
29
|
-
* @param {string} key 键
|
|
30
|
-
* @returns {boolean}
|
|
31
|
-
*/
|
|
32
|
-
delete(key: string): boolean;
|
|
33
|
-
/**
|
|
34
|
-
* 获取某个键的值
|
|
35
|
-
* @param {string} key 键
|
|
36
|
-
* @returns {any}
|
|
37
|
-
*/
|
|
38
|
-
get(key: string): any;
|
|
39
|
-
/**
|
|
40
|
-
* 返回字典中的所有值
|
|
41
|
-
* @returns {any[]}
|
|
42
|
-
*/
|
|
43
|
-
values(): any[];
|
|
44
|
-
/**
|
|
45
|
-
* 清空字典
|
|
46
|
-
*/
|
|
47
|
-
clear(): void;
|
|
48
|
-
/**
|
|
49
|
-
* 获取字典的长度
|
|
50
|
-
* @returns {number}
|
|
51
|
-
*/
|
|
52
|
-
size(): number;
|
|
53
|
-
/**
|
|
54
|
-
* 获取字典所有的键
|
|
55
|
-
*/
|
|
56
|
-
keys(): string[];
|
|
57
|
-
/**
|
|
58
|
-
* 返回字典本身
|
|
59
|
-
* @returns {object}
|
|
60
|
-
*/
|
|
61
|
-
getItems(): object;
|
|
62
|
-
/**
|
|
63
|
-
* 合并另一个字典
|
|
64
|
-
* @param {object} data 需要合并的字典
|
|
65
|
-
*/
|
|
66
|
-
concat(data: object): void;
|
|
67
|
-
forEach(callbackfn: any): void;
|
|
68
|
-
/**
|
|
69
|
-
* 获取字典的长度,同this.size
|
|
70
|
-
* @returns {number}
|
|
71
|
-
*/
|
|
72
|
-
get length(): number;
|
|
73
|
-
/**
|
|
74
|
-
* 迭代器
|
|
75
|
-
*/
|
|
76
|
-
get entries(): () => Generator<any[], void, unknown>;
|
|
77
|
-
/**
|
|
78
|
-
* 是否可遍历
|
|
79
|
-
*/
|
|
80
|
-
get [Symbol.iterator](): () => Generator<any[], void, unknown>;
|
|
81
|
-
/**
|
|
82
|
-
* .toString()和.toLocaleString()输出的字符串
|
|
83
|
-
*/
|
|
84
|
-
get [Symbol.toStringTag](): string;
|
|
85
|
-
}
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
export function Httpx(__xmlHttpRequest__: any): void;
|
|
2
|
-
export class Httpx {
|
|
3
|
-
constructor(__xmlHttpRequest__: any);
|
|
4
|
-
/**
|
|
5
|
-
* 覆盖当前配置
|
|
6
|
-
* @param {HttpxDetailsConfig} details
|
|
7
|
-
*/
|
|
8
|
-
config: (details?: HttpxDetailsConfig) => void;
|
|
9
|
-
/**
|
|
10
|
-
* 修改xmlHttpRequest
|
|
11
|
-
* @param {Function} httpRequest 网络请求函数
|
|
12
|
-
*/
|
|
13
|
-
setXMLHttpRequest: (httpRequest: Function) => void;
|
|
14
|
-
/**
|
|
15
|
-
* GET 请求
|
|
16
|
-
* @param {...HttpxDetails|string} args
|
|
17
|
-
* @returns {Promise< HttpxAsyncResult >}
|
|
18
|
-
*/
|
|
19
|
-
get: (...args: (HttpxDetails | string)[]) => Promise<HttpxAsyncResult<any>>;
|
|
20
|
-
/**
|
|
21
|
-
* POST 请求
|
|
22
|
-
* @param {...HttpxDetails|string} args
|
|
23
|
-
* @returns {Promise< HttpxAsyncResult >}
|
|
24
|
-
*/
|
|
25
|
-
post: (...args: (HttpxDetails | string)[]) => Promise<HttpxAsyncResult<any>>;
|
|
26
|
-
/**
|
|
27
|
-
* HEAD 请求
|
|
28
|
-
* @param {...HttpxDetails|string} args
|
|
29
|
-
* @returns {Promise< HttpxAsyncResult >}
|
|
30
|
-
*/
|
|
31
|
-
head: (...args: (HttpxDetails | string)[]) => Promise<HttpxAsyncResult<any>>;
|
|
32
|
-
/**
|
|
33
|
-
* OPTIONS 请求
|
|
34
|
-
* @param {...HttpxDetails|string} args
|
|
35
|
-
* @returns {Promise< HttpxAsyncResult >}
|
|
36
|
-
*/
|
|
37
|
-
options: (...args: (HttpxDetails | string)[]) => Promise<HttpxAsyncResult<any>>;
|
|
38
|
-
/**
|
|
39
|
-
* DELETE 请求
|
|
40
|
-
* @param {...HttpxDetails|string} args
|
|
41
|
-
* @returns {Promise< HttpxAsyncResult >}
|
|
42
|
-
*/
|
|
43
|
-
delete: (...args: (HttpxDetails | string)[]) => Promise<HttpxAsyncResult<any>>;
|
|
44
|
-
/**
|
|
45
|
-
* PUT 请求
|
|
46
|
-
* @param {...HttpxDetails|string} args
|
|
47
|
-
* @returns {Promise< HttpxAsyncResult >}
|
|
48
|
-
*/
|
|
49
|
-
put: (...args: (HttpxDetails | string)[]) => Promise<HttpxAsyncResult<any>>;
|
|
50
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
export function LockFunction(callback: any, context: any, delayTime?: number): void;
|
|
2
|
-
export class LockFunction {
|
|
3
|
-
constructor(callback: any, context: any, delayTime?: number);
|
|
4
|
-
/**
|
|
5
|
-
* 锁
|
|
6
|
-
*/
|
|
7
|
-
lock: () => void;
|
|
8
|
-
/**
|
|
9
|
-
* 解锁
|
|
10
|
-
*/
|
|
11
|
-
unlock: () => void;
|
|
12
|
-
/**
|
|
13
|
-
* 执行
|
|
14
|
-
*/
|
|
15
|
-
run: (...args: any[]) => Promise<void>;
|
|
16
|
-
}
|