rxtutils 1.1.4-beta.8 → 1.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/cjs/_utils/defaultEquals.cjs +4 -19
- package/cjs/cache/index.cjs +137 -163
- package/cjs/cache/indexDB.cjs +86 -99
- package/cjs/downloadBlob.cjs +14 -0
- package/cjs/hooks/index.cjs +3 -6
- package/cjs/hooks/useCombineControlValue.cjs +30 -41
- package/cjs/index.cjs +16 -17
- package/cjs/request/defaultHandlers.cjs +5 -27
- package/cjs/request/error.cjs +18 -30
- package/cjs/request/index.cjs +208 -144
- package/cjs/store/createGetter/index.cjs +26 -40
- package/cjs/store/createStateStore/index.cjs +47 -90
- package/cjs/store/index.cjs +4 -7
- package/cjs/validator/decorators.cjs +74 -221
- package/cjs/validator/index.cjs +4 -7
- package/cjs/validator/validator.cjs +101 -177
- package/es/_utils/defaultEquals.mjs +4 -17
- package/es/cache/index.d.ts +7 -11
- package/es/cache/index.mjs +139 -160
- package/es/cache/indexDB.d.ts +1 -3
- package/es/cache/indexDB.mjs +87 -98
- package/es/downloadBlob.d.ts +8 -0
- package/es/downloadBlob.mjs +14 -0
- package/es/hooks/index.d.ts +1 -1
- package/es/hooks/index.mjs +4 -1
- package/es/hooks/useCombineControlValue.d.ts +5 -8
- package/es/hooks/useCombineControlValue.mjs +31 -40
- package/es/index.d.ts +28 -8
- package/es/index.mjs +29 -7
- package/es/request/defaultHandlers.d.ts +24 -0
- package/es/request/defaultHandlers.mjs +8 -26
- package/es/request/error.d.ts +3 -6
- package/es/request/error.mjs +18 -28
- package/es/request/index.d.ts +35 -16
- package/es/request/index.mjs +208 -142
- package/es/store/createGetter/index.d.ts +6 -10
- package/es/store/createGetter/index.mjs +28 -39
- package/es/store/createStateStore/index.d.ts +9 -9
- package/es/store/createStateStore/index.mjs +49 -87
- package/es/store/index.d.ts +4 -2
- package/es/store/index.mjs +7 -2
- package/es/validator/decorators.d.ts +12 -21
- package/es/validator/decorators.mjs +84 -219
- package/es/validator/index.d.ts +2 -2
- package/es/validator/index.mjs +16 -2
- package/es/validator/validator.d.ts +5 -6
- package/es/validator/validator.mjs +102 -176
- package/package.json +62 -13
- package/cjs/cache/index.d.ts +0 -141
- package/cjs/cache/indexDB.d.ts +0 -52
- package/cjs/hooks/index.d.ts +0 -1
- package/cjs/hooks/useCombineControlValue.d.ts +0 -21
- package/cjs/index.d.ts +0 -8
- package/cjs/request/error.d.ts +0 -31
- package/cjs/request/index.d.ts +0 -140
- package/cjs/store/createGetter/index.d.ts +0 -30
- package/cjs/store/createStateStore/index.d.ts +0 -42
- package/cjs/store/index.d.ts +0 -2
- package/cjs/validator/decorators.d.ts +0 -159
- package/cjs/validator/index.d.ts +0 -2
- package/cjs/validator/validator.d.ts +0 -84
|
@@ -1,21 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* 默认的相等性比较函数
|
|
5
|
-
* 通过将两个值序列化为 JSON 字符串来比较它们是否相等
|
|
6
|
-
*
|
|
7
|
-
* @template Param 比较值的类型,默认为 any
|
|
8
|
-
* @param prev 前一个值
|
|
9
|
-
* @param next 后一个值
|
|
10
|
-
* @returns {boolean} 如果两个值相等则返回 true,否则返回 false
|
|
11
|
-
*
|
|
12
|
-
* @remarks
|
|
13
|
-
* - 这个函数通过 JSON.stringify 进行比较,适用于大多数简单的数据结构
|
|
14
|
-
* - 不适用于包含函数、undefined、Symbol 等无法序列化的值
|
|
15
|
-
* - 对于循环引用的对象会抛出错误
|
|
16
|
-
*/
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
17
3
|
function defaultEquals(prev, next) {
|
|
18
|
-
|
|
4
|
+
return JSON.stringify(prev) === JSON.stringify(next);
|
|
19
5
|
}
|
|
20
|
-
|
|
21
|
-
module.exports = defaultEquals;
|
|
6
|
+
exports.default = defaultEquals;
|
package/cjs/cache/index.cjs
CHANGED
|
@@ -1,170 +1,144 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
/** 存储类型映射表 */
|
|
11
|
-
var StorageMap = {
|
|
12
|
-
localStorage: localStorage,
|
|
13
|
-
sessionStorage: sessionStorage,
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
+
const moment = require("moment");
|
|
4
|
+
const indexDB = require("./indexDB.cjs");
|
|
5
|
+
const defaultEquals = require("../_utils/defaultEquals.cjs");
|
|
6
|
+
const StorageMap = {
|
|
7
|
+
localStorage,
|
|
8
|
+
sessionStorage
|
|
14
9
|
};
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
/** 内存中的缓存数组 */
|
|
33
|
-
this.cache = [];
|
|
34
|
-
this.cacheOptions = {
|
|
35
|
-
storageType: cacheType,
|
|
36
|
-
cacheKey: cacheKey,
|
|
37
|
-
cacheTime: cacheTime,
|
|
38
|
-
indexDBName: indexDBName,
|
|
39
|
-
cacheKeyEquals: cacheKeyEquals,
|
|
40
|
-
};
|
|
41
|
-
if (cacheType === "indexedDB") {
|
|
42
|
-
this.storage = new indexDB.IndexedDBStorage(indexDBName, "cacheStore");
|
|
43
|
-
}
|
|
44
|
-
else if (typeof cacheType === "string") {
|
|
45
|
-
this.storage = StorageMap[cacheType];
|
|
46
|
-
}
|
|
47
|
-
this._init();
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* 初始化缓存
|
|
51
|
-
* 从存储中加载已保存的缓存数据,并进行解析和过期处理
|
|
52
|
-
* @private
|
|
53
|
-
*/
|
|
54
|
-
Cache.prototype._init = function () {
|
|
55
|
-
return tslib.__awaiter(this, void 0, void 0, function () {
|
|
56
|
-
var _a, cacheType, cacheKey, _b, _c, _d;
|
|
57
|
-
return tslib.__generator(this, function (_e) {
|
|
58
|
-
switch (_e.label) {
|
|
59
|
-
case 0:
|
|
60
|
-
_a = this.cacheOptions, cacheType = _a.storageType, cacheKey = _a.cacheKey;
|
|
61
|
-
if (!(this.storage instanceof indexDB.IndexedDBStorage)) return [3 /*break*/, 2];
|
|
62
|
-
_b = this;
|
|
63
|
-
_d = (_c = JSON).parse;
|
|
64
|
-
return [4 /*yield*/, this.storage.getItem(cacheKey)];
|
|
65
|
-
case 1:
|
|
66
|
-
_b.cache = _d.apply(_c, [(_e.sent()) || "[]"]);
|
|
67
|
-
return [3 /*break*/, 3];
|
|
68
|
-
case 2:
|
|
69
|
-
if (this.storage instanceof Storage) {
|
|
70
|
-
this.storage = StorageMap[cacheType];
|
|
71
|
-
if (this.storage) {
|
|
72
|
-
if (typeof cacheKey === "string") {
|
|
73
|
-
try {
|
|
74
|
-
this.cache = JSON.parse(this.storage.getItem(cacheKey) || "[]");
|
|
75
|
-
}
|
|
76
|
-
catch (e) {
|
|
77
|
-
this.cache = [];
|
|
78
|
-
console.error("\u7F13\u5B58\u6570\u636E\u89E3\u6790\u5931\u8D25\uFF0Ckey:".concat(cacheKey));
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
_e.label = 3;
|
|
84
|
-
case 3:
|
|
85
|
-
this._filterExpired();
|
|
86
|
-
this._saveToStorage();
|
|
87
|
-
return [2 /*return*/];
|
|
88
|
-
}
|
|
89
|
-
});
|
|
90
|
-
});
|
|
91
|
-
};
|
|
92
|
-
/**
|
|
93
|
-
* 过滤掉已过期的缓存项
|
|
94
|
-
* 通过比较当前时间和过期时间,移除过期的缓存项
|
|
95
|
-
* @private
|
|
96
|
-
*/
|
|
97
|
-
Cache.prototype._filterExpired = function () {
|
|
98
|
-
var newCache = this.cache.filter(function (item) {
|
|
99
|
-
return moment(item.expireTime).isAfter(moment());
|
|
100
|
-
});
|
|
101
|
-
this.cache = newCache;
|
|
10
|
+
class Cache {
|
|
11
|
+
/**
|
|
12
|
+
* 构造函数
|
|
13
|
+
* @param cacheType 存储类型
|
|
14
|
+
* @param cacheKey 缓存键名
|
|
15
|
+
* @param cacheTime 缓存时间(秒)
|
|
16
|
+
* @param indexDBName IndexedDB 数据库名称,默认值为 '__apiCacheDatabase__'
|
|
17
|
+
* @param cacheKeyEquals 缓存键比较函数,默认使用 defaultEquals
|
|
18
|
+
*/
|
|
19
|
+
constructor(cacheType, cacheKey, cacheTime, indexDBName = "__apiCacheDatabase__", cacheKeyEquals = defaultEquals.default) {
|
|
20
|
+
this.cache = [];
|
|
21
|
+
this.cacheOptions = {
|
|
22
|
+
storageType: cacheType,
|
|
23
|
+
cacheKey,
|
|
24
|
+
cacheTime,
|
|
25
|
+
indexDBName,
|
|
26
|
+
cacheKeyEquals
|
|
102
27
|
};
|
|
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
|
-
this._saveToStorage();
|
|
135
|
-
};
|
|
136
|
-
/**
|
|
137
|
-
* 获取缓存数据
|
|
138
|
-
* @param params 查询参数
|
|
139
|
-
* @returns 如果找到有效的缓存数据则返回数据,否则返回 null
|
|
140
|
-
*/
|
|
141
|
-
Cache.prototype.getCache = function (params) {
|
|
142
|
-
var _this = this;
|
|
143
|
-
var itemIndex = this.cache.findIndex(function (item) {
|
|
144
|
-
return _this.cacheOptions.cacheKeyEquals(item.params, params);
|
|
145
|
-
});
|
|
146
|
-
var item = this.cache[itemIndex];
|
|
147
|
-
if (item) {
|
|
148
|
-
if (moment(item.expireTime).isAfter(moment())) {
|
|
149
|
-
return item.data;
|
|
150
|
-
}
|
|
151
|
-
else {
|
|
152
|
-
this.cache.splice(itemIndex, 1);
|
|
153
|
-
this._saveToStorage();
|
|
154
|
-
}
|
|
28
|
+
if (cacheType === "indexedDB") {
|
|
29
|
+
this.storage = new indexDB.IndexedDBStorage(indexDBName, "cacheStore");
|
|
30
|
+
} else if (typeof cacheType === "string") {
|
|
31
|
+
this.storage = StorageMap[cacheType];
|
|
32
|
+
}
|
|
33
|
+
this._init();
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* 初始化缓存
|
|
37
|
+
* 从存储中加载已保存的缓存数据,并进行解析和过期处理
|
|
38
|
+
* @private
|
|
39
|
+
*/
|
|
40
|
+
async _init() {
|
|
41
|
+
const { storageType: cacheType, cacheKey } = this.cacheOptions;
|
|
42
|
+
if (this.storage instanceof indexDB.IndexedDBStorage) {
|
|
43
|
+
this.cache = JSON.parse(
|
|
44
|
+
await this.storage.getItem(cacheKey) || "[]"
|
|
45
|
+
);
|
|
46
|
+
} else if (this.storage instanceof Storage) {
|
|
47
|
+
this.storage = StorageMap[cacheType];
|
|
48
|
+
if (this.storage) {
|
|
49
|
+
if (typeof cacheKey === "string") {
|
|
50
|
+
try {
|
|
51
|
+
this.cache = JSON.parse(
|
|
52
|
+
this.storage.getItem(cacheKey) || "[]"
|
|
53
|
+
);
|
|
54
|
+
} catch (e) {
|
|
55
|
+
this.cache = [];
|
|
56
|
+
console.error(`缓存数据解析失败,key:${cacheKey}`);
|
|
57
|
+
}
|
|
155
58
|
}
|
|
156
|
-
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
this._filterExpired();
|
|
62
|
+
this._saveToStorage();
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* 过滤掉已过期的缓存项
|
|
66
|
+
* 通过比较当前时间和过期时间,移除过期的缓存项
|
|
67
|
+
* @private
|
|
68
|
+
*/
|
|
69
|
+
_filterExpired() {
|
|
70
|
+
const newCache = this.cache.filter((item) => {
|
|
71
|
+
return moment(item.expireTime).isAfter(moment());
|
|
72
|
+
});
|
|
73
|
+
this.cache = newCache;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* 将当前缓存数据保存到存储中
|
|
77
|
+
* 如果设置了缓存键名且存储实例存在,则将缓存数据序列化后保存
|
|
78
|
+
* @private
|
|
79
|
+
*/
|
|
80
|
+
_saveToStorage() {
|
|
81
|
+
if (this.storage) {
|
|
82
|
+
if (typeof this.cacheOptions.cacheKey === "string") {
|
|
83
|
+
this.storage.setItem(
|
|
84
|
+
this.cacheOptions.cacheKey,
|
|
85
|
+
JSON.stringify(this.cache)
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* 设置缓存数据
|
|
92
|
+
* @param params 缓存的参数
|
|
93
|
+
* @param data 要缓存的数据
|
|
94
|
+
* @param cacheOptions 可选的缓存配置,可以覆盖默认的缓存时间
|
|
95
|
+
*/
|
|
96
|
+
setCache(params, data, cacheOptions) {
|
|
97
|
+
const { cacheTime, cacheKeyEquals = defaultEquals.default } = {
|
|
98
|
+
...this.cacheOptions,
|
|
99
|
+
...cacheOptions
|
|
157
100
|
};
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
101
|
+
const cacheItemIndex = this.cache.findIndex((item) => {
|
|
102
|
+
return cacheKeyEquals(item.params, params);
|
|
103
|
+
});
|
|
104
|
+
if (cacheItemIndex > -1) {
|
|
105
|
+
this.cache.splice(cacheItemIndex, 1);
|
|
106
|
+
}
|
|
107
|
+
this.cache.push({
|
|
108
|
+
params,
|
|
109
|
+
data,
|
|
110
|
+
expireTime: moment().add(cacheTime, "seconds").toJSON()
|
|
111
|
+
});
|
|
112
|
+
this._saveToStorage();
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* 获取缓存数据
|
|
116
|
+
* @param params 查询参数
|
|
117
|
+
* @returns 如果找到有效的缓存数据则返回数据,否则返回 null
|
|
118
|
+
*/
|
|
119
|
+
getCache(params) {
|
|
120
|
+
const itemIndex = this.cache.findIndex((item2) => {
|
|
121
|
+
return this.cacheOptions.cacheKeyEquals(item2.params, params);
|
|
122
|
+
});
|
|
123
|
+
const item = this.cache[itemIndex];
|
|
124
|
+
if (item) {
|
|
125
|
+
if (moment(item.expireTime).isAfter(moment())) {
|
|
126
|
+
return item.data;
|
|
127
|
+
} else {
|
|
128
|
+
this.cache.splice(itemIndex, 1);
|
|
164
129
|
this._saveToStorage();
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* 清空所有缓存数据
|
|
136
|
+
* 清空内存中的缓存数组并同步到存储中
|
|
137
|
+
*/
|
|
138
|
+
clear() {
|
|
139
|
+
this.cache = [];
|
|
140
|
+
this._saveToStorage();
|
|
141
|
+
}
|
|
142
|
+
}
|
|
169
143
|
exports.StorageMap = StorageMap;
|
|
170
144
|
exports.default = Cache;
|
package/cjs/cache/indexDB.cjs
CHANGED
|
@@ -1,100 +1,87 @@
|
|
|
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
|
-
return this._getStore().then(function (store) {
|
|
88
|
-
return new Promise(function (resolve, reject) {
|
|
89
|
-
var request = store.get(key);
|
|
90
|
-
request.onsuccess = function () {
|
|
91
|
-
return resolve(request.result ? request.result.value : undefined);
|
|
92
|
-
};
|
|
93
|
-
request.onerror = function () { var _a; return reject("Could not get the item: ".concat((_a = request.error) === null || _a === void 0 ? void 0 : _a.message)); };
|
|
94
|
-
});
|
|
95
|
-
});
|
|
96
|
-
};
|
|
97
|
-
return IndexedDBStorage;
|
|
98
|
-
}());
|
|
99
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
class IndexedDBStorage {
|
|
4
|
+
/**
|
|
5
|
+
* 构造函数
|
|
6
|
+
* @param dbName 数据库名称
|
|
7
|
+
* @param storeName 存储对象名称
|
|
8
|
+
*/
|
|
9
|
+
constructor(dbName, storeName) {
|
|
10
|
+
this.db = null;
|
|
11
|
+
this.dbName = dbName;
|
|
12
|
+
this.storeName = storeName;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* 打开数据库连接
|
|
16
|
+
* 如果数据库不存在则创建新的数据库和存储对象
|
|
17
|
+
* @private
|
|
18
|
+
* @returns Promise<IDBDatabase> 数据库连接实例
|
|
19
|
+
*/
|
|
20
|
+
_open() {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
if (this.db) {
|
|
23
|
+
resolve(this.db);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const request = indexedDB.open(this.dbName);
|
|
27
|
+
request.onupgradeneeded = (event) => {
|
|
28
|
+
const db = event.target.result;
|
|
29
|
+
db.createObjectStore(this.storeName, { keyPath: "key" });
|
|
30
|
+
};
|
|
31
|
+
request.onerror = () => {
|
|
32
|
+
reject(`IndexedDB open request error: ${request.error?.message}`);
|
|
33
|
+
};
|
|
34
|
+
request.onsuccess = (event) => {
|
|
35
|
+
this.db = event.target.result;
|
|
36
|
+
resolve(this.db);
|
|
37
|
+
};
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* 获取存储对象
|
|
42
|
+
* @param mode 事务模式,默认为只读模式
|
|
43
|
+
* - readonly: 只读模式
|
|
44
|
+
* - readwrite: 读写模式
|
|
45
|
+
* @private
|
|
46
|
+
* @returns Promise<IDBObjectStore> 存储对象实例
|
|
47
|
+
*/
|
|
48
|
+
_getStore(mode = "readonly") {
|
|
49
|
+
return this._open().then((db) => {
|
|
50
|
+
const transaction = db.transaction(this.storeName, mode);
|
|
51
|
+
return transaction.objectStore(this.storeName);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* 设置键值对
|
|
56
|
+
* @param key 键名
|
|
57
|
+
* @param value 要存储的值
|
|
58
|
+
* @returns Promise<void> 存储操作的结果
|
|
59
|
+
* @throws 当存储操作失败时抛出错误
|
|
60
|
+
*/
|
|
61
|
+
setItem(key, value) {
|
|
62
|
+
return this._getStore("readwrite").then((store) => {
|
|
63
|
+
return new Promise((resolve, reject) => {
|
|
64
|
+
const request = store.put({ key, value });
|
|
65
|
+
request.onsuccess = () => resolve();
|
|
66
|
+
request.onerror = () => reject(`Could not set the item: ${request.error?.message}`);
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* 获取键对应的值
|
|
72
|
+
* @param key 要获取的键名
|
|
73
|
+
* @returns Promise<T> 返回存储的值,如果不存在则返回 undefined
|
|
74
|
+
* @throws 当获取操作失败时抛出错误
|
|
75
|
+
* @template T 存储值的类型,默认为 any
|
|
76
|
+
*/
|
|
77
|
+
getItem(key) {
|
|
78
|
+
return this._getStore().then((store) => {
|
|
79
|
+
return new Promise((resolve, reject) => {
|
|
80
|
+
const request = store.get(key);
|
|
81
|
+
request.onsuccess = () => resolve(request.result ? request.result.value : void 0);
|
|
82
|
+
request.onerror = () => reject(`Could not get the item: ${request.error?.message}`);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
100
87
|
exports.IndexedDBStorage = IndexedDBStorage;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const downloadBlob = (blob, fileName, autoRevoke = true) => {
|
|
4
|
+
const url = URL.createObjectURL(blob);
|
|
5
|
+
const a = document.createElement("a");
|
|
6
|
+
a.href = url;
|
|
7
|
+
a.download = fileName;
|
|
8
|
+
a.click();
|
|
9
|
+
if (autoRevoke) {
|
|
10
|
+
URL.revokeObjectURL(url);
|
|
11
|
+
}
|
|
12
|
+
return url;
|
|
13
|
+
};
|
|
14
|
+
exports.downloadBlob = downloadBlob;
|
package/cjs/hooks/index.cjs
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const useCombineControlValue = require("./useCombineControlValue.cjs");
|
|
7
4
|
exports.useCombineControlValue = useCombineControlValue.useCombineControlValue;
|
|
@@ -1,43 +1,32 @@
|
|
|
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
|
-
}, [onChange, resolveFn]);
|
|
32
|
-
var finalValue = react.useMemo(function () {
|
|
33
|
-
if (hasValue)
|
|
34
|
-
return value;
|
|
35
|
-
return internalValue;
|
|
36
|
-
}, [hasValue, internalValue, value]);
|
|
37
|
-
return {
|
|
38
|
-
value: finalValue,
|
|
39
|
-
onChange: handleChange
|
|
40
|
-
};
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const react = require("react");
|
|
4
|
+
function useCombineControlValue({
|
|
5
|
+
props,
|
|
6
|
+
valueKey = "value",
|
|
7
|
+
defaultValue,
|
|
8
|
+
onChange
|
|
9
|
+
}, resolveFn) {
|
|
10
|
+
const { [valueKey]: value } = props;
|
|
11
|
+
const hasValue = Object.prototype.hasOwnProperty.call(props, valueKey);
|
|
12
|
+
const [internalValue, setInternalValue] = react.useState(value ?? defaultValue);
|
|
13
|
+
const handleChange = react.useCallback((...params) => {
|
|
14
|
+
let realNextVal;
|
|
15
|
+
if (typeof resolveFn === "function") {
|
|
16
|
+
realNextVal = resolveFn(...params);
|
|
17
|
+
} else {
|
|
18
|
+
realNextVal = params[0];
|
|
19
|
+
}
|
|
20
|
+
setInternalValue(realNextVal);
|
|
21
|
+
onChange?.(...params);
|
|
22
|
+
}, [onChange, resolveFn]);
|
|
23
|
+
const finalValue = react.useMemo(() => {
|
|
24
|
+
if (hasValue) return value;
|
|
25
|
+
return internalValue;
|
|
26
|
+
}, [hasValue, internalValue, value]);
|
|
27
|
+
return {
|
|
28
|
+
value: finalValue,
|
|
29
|
+
onChange: handleChange
|
|
30
|
+
};
|
|
41
31
|
}
|
|
42
|
-
|
|
43
32
|
exports.useCombineControlValue = useCombineControlValue;
|