rxtutils 1.1.4-beta.14 → 1.1.4-beta.16

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.
@@ -2,13 +2,12 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var tslib = require('tslib');
6
5
  var moment = require('moment');
7
6
  var indexDB = require('./indexDB.cjs');
8
7
  var defaultEquals = require('../_utils/defaultEquals.cjs');
9
8
 
10
9
  /** 存储类型映射表 */
11
- var StorageMap = {
10
+ const StorageMap = {
12
11
  localStorage: localStorage,
13
12
  sessionStorage: sessionStorage,
14
13
  };
@@ -17,7 +16,7 @@ var StorageMap = {
17
16
  * @template Param 缓存参数类型
18
17
  * @template Data 缓存数据类型
19
18
  */
20
- var Cache = /** @class */ (function () {
19
+ class Cache {
21
20
  /**
22
21
  * 构造函数
23
22
  * @param cacheType 存储类型
@@ -26,17 +25,15 @@ var Cache = /** @class */ (function () {
26
25
  * @param indexDBName IndexedDB 数据库名称,默认值为 '__apiCacheDatabase__'
27
26
  * @param cacheKeyEquals 缓存键比较函数,默认使用 defaultEquals
28
27
  */
29
- function Cache(cacheType, cacheKey, cacheTime, indexDBName, cacheKeyEquals) {
30
- if (indexDBName === void 0) { indexDBName = "__apiCacheDatabase__"; }
31
- if (cacheKeyEquals === void 0) { cacheKeyEquals = defaultEquals; }
28
+ constructor(cacheType, cacheKey, cacheTime, indexDBName = "__apiCacheDatabase__", cacheKeyEquals = defaultEquals) {
32
29
  /** 内存中的缓存数组 */
33
30
  this.cache = [];
34
31
  this.cacheOptions = {
35
32
  storageType: cacheType,
36
33
  cacheKey: cacheKey,
37
34
  cacheTime: cacheTime,
38
- indexDBName: indexDBName,
39
- cacheKeyEquals: cacheKeyEquals,
35
+ indexDBName,
36
+ cacheKeyEquals,
40
37
  };
41
38
  if (cacheType === "indexedDB") {
42
39
  this.storage = new indexDB.IndexedDBStorage(indexDBName, "cacheStore");
@@ -51,99 +48,85 @@ var Cache = /** @class */ (function () {
51
48
  * 从存储中加载已保存的缓存数据,并进行解析和过期处理
52
49
  * @private
53
50
  */
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*/];
51
+ async _init() {
52
+ const { storageType: cacheType, cacheKey: cacheKey } = this.cacheOptions;
53
+ if (this.storage instanceof indexDB.IndexedDBStorage) {
54
+ this.cache = JSON.parse((await this.storage.getItem(cacheKey)) || "[]");
55
+ }
56
+ else if (this.storage instanceof Storage) {
57
+ this.storage = StorageMap[cacheType];
58
+ if (this.storage) {
59
+ if (typeof cacheKey === "string") {
60
+ try {
61
+ this.cache = JSON.parse(this.storage.getItem(cacheKey) || "[]");
62
+ }
63
+ catch (e) {
64
+ this.cache = [];
65
+ console.error(`缓存数据解析失败,key:${cacheKey}`);
66
+ }
88
67
  }
89
- });
90
- });
91
- };
68
+ }
69
+ }
70
+ this._filterExpired();
71
+ this._saveToStorage();
72
+ }
92
73
  /**
93
74
  * 过滤掉已过期的缓存项
94
75
  * 通过比较当前时间和过期时间,移除过期的缓存项
95
76
  * @private
96
77
  */
97
- Cache.prototype._filterExpired = function () {
98
- var newCache = this.cache.filter(function (item) {
78
+ _filterExpired() {
79
+ const newCache = this.cache.filter((item) => {
99
80
  return moment(item.expireTime).isAfter(moment());
100
81
  });
101
82
  this.cache = newCache;
102
- };
83
+ }
103
84
  /**
104
85
  * 将当前缓存数据保存到存储中
105
86
  * 如果设置了缓存键名且存储实例存在,则将缓存数据序列化后保存
106
87
  * @private
107
88
  */
108
- Cache.prototype._saveToStorage = function () {
89
+ _saveToStorage() {
109
90
  if (this.storage) {
110
91
  if (typeof this.cacheOptions.cacheKey === "string") {
111
92
  this.storage.setItem(this.cacheOptions.cacheKey, JSON.stringify(this.cache));
112
93
  }
113
94
  }
114
- };
95
+ }
115
96
  /**
116
97
  * 设置缓存数据
117
98
  * @param params 缓存的参数
118
99
  * @param data 要缓存的数据
119
100
  * @param cacheOptions 可选的缓存配置,可以覆盖默认的缓存时间
120
101
  */
121
- Cache.prototype.setCache = function (params, data, cacheOptions) {
122
- var _a = tslib.__assign(tslib.__assign({}, this.cacheOptions), cacheOptions), cacheTime = _a.cacheTime, _b = _a.cacheKeyEquals, cacheKeyEquals = _b === void 0 ? defaultEquals : _b;
123
- var cacheItemIndex = this.cache.findIndex(function (item) {
102
+ setCache(params, data, cacheOptions) {
103
+ const { cacheTime, cacheKeyEquals = defaultEquals } = {
104
+ ...this.cacheOptions,
105
+ ...cacheOptions,
106
+ };
107
+ const cacheItemIndex = this.cache.findIndex((item) => {
124
108
  return cacheKeyEquals(item.params, params);
125
109
  });
126
110
  if (cacheItemIndex > -1) {
127
111
  this.cache.splice(cacheItemIndex, 1);
128
112
  }
129
113
  this.cache.push({
130
- params: params,
131
- data: data,
114
+ params,
115
+ data,
132
116
  expireTime: moment().add(cacheTime, "seconds").toJSON(),
133
117
  });
134
118
  this._saveToStorage();
135
- };
119
+ }
136
120
  /**
137
121
  * 获取缓存数据
138
122
  * @param params 查询参数
139
123
  * @returns 如果找到有效的缓存数据则返回数据,否则返回 null
140
124
  */
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);
125
+ getCache(params) {
126
+ const itemIndex = this.cache.findIndex((item) => {
127
+ return this.cacheOptions.cacheKeyEquals(item.params, params);
145
128
  });
146
- var item = this.cache[itemIndex];
129
+ const item = this.cache[itemIndex];
147
130
  if (item) {
148
131
  if (moment(item.expireTime).isAfter(moment())) {
149
132
  return item.data;
@@ -154,17 +137,16 @@ var Cache = /** @class */ (function () {
154
137
  }
155
138
  }
156
139
  return null;
157
- };
140
+ }
158
141
  /**
159
142
  * 清空所有缓存数据
160
143
  * 清空内存中的缓存数组并同步到存储中
161
144
  */
162
- Cache.prototype.clear = function () {
145
+ clear() {
163
146
  this.cache = [];
164
147
  this._saveToStorage();
165
- };
166
- return Cache;
167
- }());
148
+ }
149
+ }
168
150
 
169
151
  exports.StorageMap = StorageMap;
170
152
  exports.default = Cache;
@@ -129,7 +129,7 @@ declare class Cache<Param, Data> {
129
129
  * @param params 查询参数
130
130
  * @returns 如果找到有效的缓存数据则返回数据,否则返回 null
131
131
  */
132
- getCache(params: Param): Data;
132
+ getCache(params: Param): Data | null;
133
133
  /**
134
134
  * 清空所有缓存数据
135
135
  * 清空内存中的缓存数组并同步到存储中
@@ -4,13 +4,13 @@
4
4
  * IndexedDB 存储类
5
5
  * 提供了对 IndexedDB 数据库操作的简单封装
6
6
  */
7
- var IndexedDBStorage = /** @class */ (function () {
7
+ class IndexedDBStorage {
8
8
  /**
9
9
  * 构造函数
10
10
  * @param dbName 数据库名称
11
11
  * @param storeName 存储对象名称
12
12
  */
13
- function IndexedDBStorage(dbName, storeName) {
13
+ constructor(dbName, storeName) {
14
14
  /** 数据库连接实例 */
15
15
  this.db = null;
16
16
  this.dbName = dbName;
@@ -22,28 +22,26 @@ var IndexedDBStorage = /** @class */ (function () {
22
22
  * @private
23
23
  * @returns Promise<IDBDatabase> 数据库连接实例
24
24
  */
25
- IndexedDBStorage.prototype._open = function () {
26
- var _this = this;
27
- return new Promise(function (resolve, reject) {
28
- if (_this.db) {
29
- resolve(_this.db);
25
+ _open() {
26
+ return new Promise((resolve, reject) => {
27
+ if (this.db) {
28
+ resolve(this.db);
30
29
  return;
31
30
  }
32
- var request = indexedDB.open(_this.dbName);
33
- request.onupgradeneeded = function (event) {
34
- var db = event.target.result;
35
- db.createObjectStore(_this.storeName, { keyPath: 'key' });
31
+ const request = indexedDB.open(this.dbName);
32
+ request.onupgradeneeded = (event) => {
33
+ const db = event.target.result;
34
+ db.createObjectStore(this.storeName, { keyPath: 'key' });
36
35
  };
37
- request.onerror = function () {
38
- var _a;
39
- reject("IndexedDB open request error: ".concat((_a = request.error) === null || _a === void 0 ? void 0 : _a.message));
36
+ request.onerror = () => {
37
+ reject(`IndexedDB open request error: ${request.error?.message}`);
40
38
  };
41
- request.onsuccess = function (event) {
42
- _this.db = event.target.result;
43
- resolve(_this.db);
39
+ request.onsuccess = (event) => {
40
+ this.db = event.target.result;
41
+ resolve(this.db);
44
42
  };
45
43
  });
46
- };
44
+ }
47
45
  /**
48
46
  * 获取存储对象
49
47
  * @param mode 事务模式,默认为只读模式
@@ -52,14 +50,12 @@ var IndexedDBStorage = /** @class */ (function () {
52
50
  * @private
53
51
  * @returns Promise<IDBObjectStore> 存储对象实例
54
52
  */
55
- IndexedDBStorage.prototype._getStore = function (mode) {
56
- var _this = this;
57
- if (mode === void 0) { mode = 'readonly'; }
58
- return this._open().then(function (db) {
59
- var transaction = db.transaction(_this.storeName, mode);
60
- return transaction.objectStore(_this.storeName);
53
+ _getStore(mode = 'readonly') {
54
+ return this._open().then((db) => {
55
+ const transaction = db.transaction(this.storeName, mode);
56
+ return transaction.objectStore(this.storeName);
61
57
  });
62
- };
58
+ }
63
59
  /**
64
60
  * 设置键值对
65
61
  * @param key 键名
@@ -67,15 +63,15 @@ var IndexedDBStorage = /** @class */ (function () {
67
63
  * @returns Promise<void> 存储操作的结果
68
64
  * @throws 当存储操作失败时抛出错误
69
65
  */
70
- IndexedDBStorage.prototype.setItem = function (key, value) {
71
- return this._getStore('readwrite').then(function (store) {
72
- return new Promise(function (resolve, reject) {
73
- var request = store.put({ key: key, value: value });
74
- request.onsuccess = function () { return resolve(); };
75
- request.onerror = function () { var _a; return reject("Could not set the item: ".concat((_a = request.error) === null || _a === void 0 ? void 0 : _a.message)); };
66
+ setItem(key, value) {
67
+ return this._getStore('readwrite').then((store) => {
68
+ return new Promise((resolve, reject) => {
69
+ const request = store.put({ key, value });
70
+ request.onsuccess = () => resolve();
71
+ request.onerror = () => reject(`Could not set the item: ${request.error?.message}`);
76
72
  });
77
73
  });
78
- };
74
+ }
79
75
  /**
80
76
  * 获取键对应的值
81
77
  * @param key 要获取的键名
@@ -83,18 +79,15 @@ var IndexedDBStorage = /** @class */ (function () {
83
79
  * @throws 当获取操作失败时抛出错误
84
80
  * @template T 存储值的类型,默认为 any
85
81
  */
86
- IndexedDBStorage.prototype.getItem = function (key) {
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)); };
82
+ getItem(key) {
83
+ return this._getStore().then((store) => {
84
+ return new Promise((resolve, reject) => {
85
+ const request = store.get(key);
86
+ request.onsuccess = () => resolve(request.result ? request.result.value : undefined);
87
+ request.onerror = () => reject(`Could not get the item: ${request.error?.message}`);
94
88
  });
95
89
  });
96
- };
97
- return IndexedDBStorage;
98
- }());
90
+ }
91
+ }
99
92
 
100
93
  exports.IndexedDBStorage = IndexedDBStorage;
@@ -9,27 +9,22 @@ var react = require('react');
9
9
  * @param param onChange 值改变时的回调
10
10
  * @returns value: 组件应该采用的值,onChange:值改变时的回调,处理非受控值与向父组件传递值的逻辑
11
11
  */
12
- function useCombineControlValue(_a, resolveFn) {
13
- var props = _a.props, _b = _a.valueKey, valueKey = _b === void 0 ? 'value' : _b, defaultValue = _a.defaultValue, onChange = _a.onChange;
14
- var _c = props, _d = valueKey, value = _c[_d];
15
- var hasValue = Object.prototype.hasOwnProperty.call(props, valueKey);
16
- var _e = react.useState(value !== null && value !== void 0 ? value : defaultValue), internalValue = _e[0], setInternalValue = _e[1];
17
- var handleChange = react.useCallback(function () {
18
- var params = [];
19
- for (var _i = 0; _i < arguments.length; _i++) {
20
- params[_i] = arguments[_i];
21
- }
22
- var realNextVal;
12
+ function useCombineControlValue({ props, valueKey = 'value', defaultValue, onChange }, resolveFn) {
13
+ const { [valueKey]: value } = props;
14
+ const hasValue = Object.prototype.hasOwnProperty.call(props, valueKey);
15
+ const [internalValue, setInternalValue] = react.useState(value ?? defaultValue);
16
+ const handleChange = react.useCallback((...params) => {
17
+ let realNextVal;
23
18
  if (typeof resolveFn === 'function') {
24
- realNextVal = resolveFn.apply(void 0, params);
19
+ realNextVal = resolveFn(...params);
25
20
  }
26
21
  else {
27
22
  realNextVal = params[0];
28
23
  }
29
24
  setInternalValue(realNextVal);
30
- onChange === null || onChange === void 0 ? void 0 : onChange.apply(void 0, params);
25
+ onChange?.(...params);
31
26
  }, [onChange, resolveFn]);
32
- var finalValue = react.useMemo(function () {
27
+ const finalValue = react.useMemo(() => {
33
28
  if (hasValue)
34
29
  return value;
35
30
  return internalValue;
@@ -8,7 +8,7 @@
8
8
  * @param code 业务错误码
9
9
  */
10
10
  function _defaultErrorCodeHandler(defaultMessageShower, code) {
11
- defaultMessageShower("\u8BF7\u6C42\u51FA\u9519\uFF0C\u9519\u8BEF\u7801\uFF1A".concat(code, "\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5"));
11
+ defaultMessageShower(`请求出错,错误码:${code},请稍后再试`);
12
12
  }
13
13
  /**
14
14
  * 默认 HTTP 错误码处理函数
@@ -18,7 +18,7 @@ function _defaultErrorCodeHandler(defaultMessageShower, code) {
18
18
  * @param code HTTP 状态码
19
19
  */
20
20
  function _defaultHttpErrorCodeHandler(defaultMessageShower, code) {
21
- defaultMessageShower("\u670D\u52A1\u7AEF\u8BF7\u6C42\u51FA\u9519\uFF0CHttp\u9519\u8BEF\u7801\uFF1A".concat(String(code), "\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5"));
21
+ defaultMessageShower(`服务端请求出错,Http错误码:${String(code)},请稍后再试`);
22
22
  }
23
23
  /**
24
24
  * 默认其他错误处理函数
@@ -28,7 +28,7 @@ function _defaultHttpErrorCodeHandler(defaultMessageShower, code) {
28
28
  * @param error 错误对象
29
29
  */
30
30
  function _defaultOtherErrorCodeHandler(defaultMessageShower, error) {
31
- defaultMessageShower("\u672A\u77E5\u8BF7\u6C42\u51FA\u9519\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5");
31
+ defaultMessageShower(`未知请求出错,请稍后再试`);
32
32
  }
33
33
 
34
34
  exports._defaultErrorCodeHandler = _defaultErrorCodeHandler;
@@ -1,7 +1,5 @@
1
1
  'use strict';
2
2
 
3
- var tslib = require('tslib');
4
-
5
3
  /**
6
4
  * 请求错误类
7
5
  * 用于统一处理请求过程中的各种错误
@@ -9,22 +7,19 @@ var tslib = require('tslib');
9
7
  * @template Data 错误数据类型
10
8
  * @extends Error
11
9
  */
12
- var RequestError = /** @class */ (function (_super) {
13
- tslib.__extends(RequestError, _super);
10
+ class RequestError extends Error {
14
11
  /**
15
12
  * 构造函数
16
13
  * @param message 错误消息
17
14
  * @param type 错误类型
18
15
  * @param data 错误相关的数据
19
16
  */
20
- function RequestError(message, type, data) {
21
- var _this = _super.call(this, message) || this;
22
- _this.name = 'RequestError';
23
- _this.type = type;
24
- _this.data = data;
25
- return _this;
17
+ constructor(message, type, data) {
18
+ super(message);
19
+ this.name = 'RequestError';
20
+ this.type = type;
21
+ this.data = data;
26
22
  }
27
- return RequestError;
28
- }(Error));
23
+ }
29
24
 
30
25
  module.exports = RequestError;