util-helpers 4.23.0 → 4.23.2
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/util-helpers.js +453 -504
- package/dist/util-helpers.js.map +1 -1
- package/dist/util-helpers.min.js +1 -1
- package/dist/util-helpers.min.js.map +1 -1
- package/esm/VERSION.js +1 -1
- package/esm/getImageInfo.js +34 -40
- package/esm/index.js +1 -1
- package/esm/loadImage.js +32 -34
- package/esm/loadImageWithBlob.js +23 -24
- package/lib/VERSION.js +1 -1
- package/lib/getImageInfo.js +33 -39
- package/lib/index.js +1 -1
- package/lib/loadImage.js +31 -33
- package/lib/loadImageWithBlob.js +23 -24
- package/package.json +14 -13
- package/types/AsyncMemo.d.ts +2 -2
- package/types/ajax.d.ts +2 -2
- package/types/blobToDataURL.d.ts +1 -1
- package/types/calculateCursorPosition.d.ts +2 -2
- package/types/compressImage.d.ts +1 -1
- package/types/dataURLToBlob.d.ts +1 -1
- package/types/download.d.ts +3 -3
- package/types/fileReader.d.ts +1 -1
- package/types/gcd.d.ts +1 -1
- package/types/getImageInfo.d.ts +11 -10
- package/types/index.d.ts +2 -2
- package/types/isBankCard.d.ts +1 -1
- package/types/isBusinessLicense.d.ts +1 -1
- package/types/isChinese.d.ts +3 -3
- package/types/isHMCard.d.ts +1 -1
- package/types/isIdCard.d.ts +2 -2
- package/types/isPassport.d.ts +1 -1
- package/types/isPassword.d.ts +1 -1
- package/types/isSocialCreditCode.d.ts +1 -1
- package/types/isSwiftCode.d.ts +1 -1
- package/types/isTWCard.d.ts +1 -1
- package/types/isUrl.d.ts +1 -1
- package/types/isVehicle.d.ts +1 -1
- package/types/lcm.d.ts +1 -1
- package/types/loadImage.d.ts +2 -0
- package/types/loadImageWithBlob.d.ts +6 -4
- package/types/normalizeString.d.ts +1 -1
- package/types/numberToChinese.d.ts +1 -1
- package/types/parseIdCard.d.ts +1 -1
- package/types/safeDate.d.ts +1 -1
- package/types/setDataURLPrefix.d.ts +2 -2
- package/types/validatePassword.d.ts +1 -1
- package/esm/utils/Cache.js +0 -47
- package/lib/utils/Cache.js +0 -49
package/dist/util-helpers.js
CHANGED
|
@@ -200,6 +200,11 @@
|
|
|
200
200
|
return isNil(value) ? '' : baseToString(value);
|
|
201
201
|
}
|
|
202
202
|
|
|
203
|
+
var defaultTo = function (value, defaultValue) {
|
|
204
|
+
return value == null || value !== value ? defaultValue : value;
|
|
205
|
+
};
|
|
206
|
+
var defaultTo$1 = defaultTo;
|
|
207
|
+
|
|
203
208
|
var blobExisted = typeof Blob !== stringUndefined;
|
|
204
209
|
function isBlob(value) {
|
|
205
210
|
if (blobExisted && value instanceof Blob) {
|
|
@@ -1637,81 +1642,401 @@
|
|
|
1637
1642
|
return EmitterPro;
|
|
1638
1643
|
}());
|
|
1639
1644
|
|
|
1640
|
-
|
|
1645
|
+
// 随机字符串
|
|
1646
|
+
function randomString$1() {
|
|
1647
|
+
return Math.random().toString(16).substring(2, 8);
|
|
1648
|
+
}
|
|
1649
|
+
// 内部自增id
|
|
1650
|
+
var uid = 1;
|
|
1651
|
+
// 返回唯一标识
|
|
1652
|
+
function getUniqueId() {
|
|
1653
|
+
return "".concat(randomString$1(), "_").concat(uid++);
|
|
1654
|
+
}
|
|
1655
|
+
// 是否支持 storage
|
|
1656
|
+
function isStorageSupported(storage) {
|
|
1657
|
+
try {
|
|
1658
|
+
var isSupport = typeof storage === 'object' &&
|
|
1659
|
+
storage !== null &&
|
|
1660
|
+
!!storage.setItem &&
|
|
1661
|
+
!!storage.getItem &&
|
|
1662
|
+
!!storage.removeItem;
|
|
1663
|
+
if (isSupport) {
|
|
1664
|
+
var key = getUniqueId();
|
|
1665
|
+
var value = '1';
|
|
1666
|
+
storage.setItem(key, value);
|
|
1667
|
+
if (storage.getItem(key) !== value) {
|
|
1668
|
+
return false;
|
|
1669
|
+
}
|
|
1670
|
+
storage.removeItem(key);
|
|
1671
|
+
}
|
|
1672
|
+
return isSupport;
|
|
1673
|
+
}
|
|
1674
|
+
catch (e) {
|
|
1675
|
+
console.error("[cache2] ".concat(storage, " is not supported. The default memory cache will be used."));
|
|
1676
|
+
return false;
|
|
1677
|
+
}
|
|
1678
|
+
}
|
|
1679
|
+
function parse(value, reviver) {
|
|
1680
|
+
try {
|
|
1681
|
+
return JSON.parse(value, reviver);
|
|
1682
|
+
}
|
|
1683
|
+
catch (e) {
|
|
1684
|
+
return value;
|
|
1685
|
+
}
|
|
1686
|
+
}
|
|
1687
|
+
function stringify(value, replacer) {
|
|
1688
|
+
return JSON.stringify(value, replacer);
|
|
1689
|
+
}
|
|
1690
|
+
|
|
1691
|
+
var cache = {};
|
|
1692
|
+
var MemoryStorage = /** @class */ (function () {
|
|
1693
|
+
function MemoryStorage(scope) {
|
|
1694
|
+
if (scope === void 0) { scope = 'default'; }
|
|
1695
|
+
this.scope = scope;
|
|
1696
|
+
if (!cache[this.scope]) {
|
|
1697
|
+
cache[this.scope] = {};
|
|
1698
|
+
}
|
|
1699
|
+
this.data = cache[this.scope];
|
|
1700
|
+
}
|
|
1701
|
+
MemoryStorage.prototype.getItem = function (key) {
|
|
1702
|
+
return key in this.data ? this.data[key] : null;
|
|
1703
|
+
};
|
|
1704
|
+
MemoryStorage.prototype.setItem = function (key, value) {
|
|
1705
|
+
this.data[key] = value;
|
|
1706
|
+
};
|
|
1707
|
+
MemoryStorage.prototype.removeItem = function (key) {
|
|
1708
|
+
delete this.data[key];
|
|
1709
|
+
};
|
|
1710
|
+
MemoryStorage.prototype.clear = function () {
|
|
1711
|
+
cache[this.scope] = {};
|
|
1712
|
+
this.data = cache[this.scope];
|
|
1713
|
+
};
|
|
1714
|
+
return MemoryStorage;
|
|
1715
|
+
}());
|
|
1716
|
+
|
|
1717
|
+
var Storage = /** @class */ (function () {
|
|
1718
|
+
function Storage(storage, options) {
|
|
1719
|
+
if (options === void 0) { options = {}; }
|
|
1720
|
+
var isSupported = storage ? isStorageSupported(storage) : false;
|
|
1721
|
+
this.options = __assign({ needParsed: isSupported }, options);
|
|
1722
|
+
this.storage = isSupported ? storage : new MemoryStorage(this.options.memoryScope);
|
|
1723
|
+
}
|
|
1724
|
+
Storage.prototype.get = function (key) {
|
|
1725
|
+
var data = this.storage.getItem(key);
|
|
1726
|
+
return this.options.needParsed ? parse(data, this.options.reviver) : data;
|
|
1727
|
+
};
|
|
1728
|
+
Storage.prototype.set = function (key, data) {
|
|
1729
|
+
this.storage.setItem(key, this.options.needParsed ? stringify(data, this.options.replacer) : data);
|
|
1730
|
+
};
|
|
1731
|
+
Storage.prototype.del = function (key) {
|
|
1732
|
+
this.storage.removeItem(key);
|
|
1733
|
+
};
|
|
1734
|
+
Storage.prototype.clear = function () {
|
|
1735
|
+
if (typeof this.storage.clear === 'function') {
|
|
1736
|
+
this.storage.clear();
|
|
1737
|
+
}
|
|
1738
|
+
};
|
|
1739
|
+
return Storage;
|
|
1740
|
+
}());
|
|
1741
|
+
|
|
1742
|
+
var defaultPrefix = 'cache2_'; // 命名空间缓存键前缀,默认 cache2_ 。
|
|
1743
|
+
var defaultNamespace = 'default';
|
|
1744
|
+
var Cache = /** @class */ (function (_super) {
|
|
1641
1745
|
__extends(Cache, _super);
|
|
1642
|
-
function Cache(options) {
|
|
1746
|
+
function Cache(namespace, options) {
|
|
1643
1747
|
var _this = _super.call(this) || this;
|
|
1644
|
-
|
|
1645
|
-
|
|
1748
|
+
var ns = defaultNamespace, opts;
|
|
1749
|
+
if (typeof namespace === 'string') {
|
|
1750
|
+
ns = namespace;
|
|
1751
|
+
}
|
|
1752
|
+
else if (typeof namespace === 'object') {
|
|
1753
|
+
opts = namespace;
|
|
1754
|
+
}
|
|
1755
|
+
if (!opts && typeof options === 'object') {
|
|
1756
|
+
opts = options;
|
|
1757
|
+
}
|
|
1758
|
+
_this.options = __assign({ max: -1, stdTTL: 0, maxStrategy: 'limited', checkperiod: 0, prefix: defaultPrefix }, opts);
|
|
1759
|
+
_this.storage = new Storage(_this.options.storage, __assign({ memoryScope: ns }, opts));
|
|
1760
|
+
_this.cacheKey = (_this.options.prefix || '') + (ns || '') || getUniqueId();
|
|
1761
|
+
_this.startCheckperiod();
|
|
1646
1762
|
return _this;
|
|
1647
1763
|
}
|
|
1648
|
-
|
|
1649
|
-
|
|
1764
|
+
// 检查当前键值是否过期,如果过期将会自动删除
|
|
1765
|
+
Cache.prototype._check = function (key, data) {
|
|
1766
|
+
var ret = true;
|
|
1767
|
+
if (data.t !== 0 && data.t < Date.now()) {
|
|
1768
|
+
ret = false;
|
|
1769
|
+
this.del(key);
|
|
1770
|
+
this.emit('expired', key, data.v);
|
|
1771
|
+
}
|
|
1772
|
+
return ret;
|
|
1650
1773
|
};
|
|
1651
|
-
Cache.prototype.
|
|
1652
|
-
var
|
|
1653
|
-
|
|
1774
|
+
Cache.prototype._wrap = function (value, ttl) {
|
|
1775
|
+
var now = Date.now();
|
|
1776
|
+
var currentTtl = typeof ttl === 'number' ? ttl : this.options.stdTTL;
|
|
1777
|
+
var livetime = currentTtl > 0 ? now + currentTtl : 0;
|
|
1778
|
+
return {
|
|
1779
|
+
v: value,
|
|
1780
|
+
t: livetime,
|
|
1781
|
+
n: now
|
|
1782
|
+
};
|
|
1654
1783
|
};
|
|
1655
|
-
Cache.prototype.
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1784
|
+
Cache.prototype._isLimited = function (len) {
|
|
1785
|
+
return this.options.max > -1 && len >= this.options.max;
|
|
1786
|
+
};
|
|
1787
|
+
Cache.prototype._getReplaceKey = function (keys, cacheValues) {
|
|
1788
|
+
var retkey = keys[0];
|
|
1789
|
+
keys.forEach(function (key) {
|
|
1790
|
+
if (cacheValues[key].t < cacheValues[retkey].t ||
|
|
1791
|
+
(cacheValues[key].t === cacheValues[retkey].t && cacheValues[key].n < cacheValues[retkey].n)) {
|
|
1792
|
+
retkey = key;
|
|
1664
1793
|
}
|
|
1665
|
-
}
|
|
1794
|
+
});
|
|
1795
|
+
return retkey;
|
|
1796
|
+
};
|
|
1797
|
+
Object.defineProperty(Cache.prototype, "cacheValues", {
|
|
1798
|
+
// 获取全部缓存数据,不处理过期数据和排序
|
|
1799
|
+
get: function () {
|
|
1800
|
+
return this.storage.get(this.cacheKey) || {};
|
|
1801
|
+
},
|
|
1802
|
+
enumerable: false,
|
|
1803
|
+
configurable: true
|
|
1804
|
+
});
|
|
1805
|
+
// 设置缓存数据
|
|
1806
|
+
Cache.prototype.setCacheValues = function (values) {
|
|
1807
|
+
this.storage.set(this.cacheKey, values);
|
|
1666
1808
|
};
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1809
|
+
// 从缓存中获取保存的值。如果未找到或已过期,则返回 undefined 。如果找到该值,则返回该值。
|
|
1810
|
+
Cache.prototype.get = function (key) {
|
|
1811
|
+
var data = this.cacheValues[key];
|
|
1812
|
+
if (data && this._check(key, data)) {
|
|
1813
|
+
return data.v;
|
|
1672
1814
|
}
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1815
|
+
return;
|
|
1816
|
+
};
|
|
1817
|
+
// 从缓存中获取多个保存的值。如果未找到或已过期,则返回一个空对象。如果找到该值,它会返回一个具有键值对的对象。
|
|
1818
|
+
Cache.prototype.mget = function (keys) {
|
|
1819
|
+
var _this = this;
|
|
1820
|
+
var ret = {};
|
|
1821
|
+
if (!Array.isArray(keys)) {
|
|
1822
|
+
return ret;
|
|
1676
1823
|
}
|
|
1824
|
+
var cacheValues = this.cacheValues;
|
|
1825
|
+
keys.forEach(function (key) {
|
|
1826
|
+
var data = cacheValues[key];
|
|
1827
|
+
if (data && _this._check(key, data)) {
|
|
1828
|
+
ret[key] = data.v;
|
|
1829
|
+
}
|
|
1830
|
+
});
|
|
1831
|
+
return ret;
|
|
1677
1832
|
};
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1833
|
+
// 从缓存中获取全部保存的值。返回一个具有键值对的对象。
|
|
1834
|
+
Cache.prototype.getAll = function () {
|
|
1835
|
+
var keys = Object.keys(this.cacheValues);
|
|
1836
|
+
return this.mget(keys);
|
|
1837
|
+
};
|
|
1838
|
+
// 设置键值对。设置成功返回 true 。
|
|
1839
|
+
Cache.prototype.set = function (key, value, ttl) {
|
|
1840
|
+
if (this.options.max === 0) {
|
|
1841
|
+
return false;
|
|
1686
1842
|
}
|
|
1687
|
-
|
|
1843
|
+
var cacheValues = this.cacheValues;
|
|
1844
|
+
var keys = Object.keys(cacheValues);
|
|
1845
|
+
// 当前不存在该键值,并且数据量超过最大限制
|
|
1846
|
+
if (!cacheValues[key] && this._isLimited(keys.length)) {
|
|
1847
|
+
var validKeys = this.keys();
|
|
1848
|
+
if (this._isLimited(validKeys.length)) {
|
|
1849
|
+
// 如果最大限制策略是替换,将优先替换快过期的数据,如果都是一样的过期时间(0),按照先入先出规则处理。
|
|
1850
|
+
if (this.options.maxStrategy === 'replaced') {
|
|
1851
|
+
var replaceKey = this._getReplaceKey(validKeys, cacheValues);
|
|
1852
|
+
this.del(replaceKey);
|
|
1853
|
+
}
|
|
1854
|
+
else {
|
|
1855
|
+
// 如果是最大限制策略是不允许添加,返回 false 。
|
|
1856
|
+
return false;
|
|
1857
|
+
}
|
|
1858
|
+
}
|
|
1688
1859
|
}
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
var _cacheOptions = {
|
|
1694
|
-
useCache: typeof cacheOptions === 'object' ? cacheOptions.useCache !== false : cacheOptions !== false,
|
|
1695
|
-
autoRevokeOnDel: typeof cacheOptions === 'object' ? cacheOptions.autoRevokeOnDel !== false : !!cacheOptions
|
|
1860
|
+
cacheValues[key] = this._wrap(value, ttl);
|
|
1861
|
+
this.setCacheValues(cacheValues);
|
|
1862
|
+
this.emit('set', key, cacheValues[key].v);
|
|
1863
|
+
return true;
|
|
1696
1864
|
};
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1865
|
+
// 设置多个键值对。全部设置成功返回 true 。
|
|
1866
|
+
Cache.prototype.mset = function (keyValueSet) {
|
|
1867
|
+
var _this = this;
|
|
1868
|
+
// 该处不使用数组 some 方法,是因为不能某个失败,而导致其他就不在更新。
|
|
1869
|
+
var ret = true;
|
|
1870
|
+
keyValueSet.forEach(function (item) {
|
|
1871
|
+
var itemSetResult = _this.set(item.key, item.value, item.ttl);
|
|
1872
|
+
if (ret && !itemSetResult) {
|
|
1873
|
+
ret = false;
|
|
1874
|
+
}
|
|
1875
|
+
});
|
|
1876
|
+
return ret;
|
|
1877
|
+
};
|
|
1878
|
+
// 删除一个或多个键。返回已删除条目的数量。删除永远不会失败。
|
|
1879
|
+
Cache.prototype.del = function (key) {
|
|
1880
|
+
var _this = this;
|
|
1881
|
+
var cacheValues = this.cacheValues;
|
|
1882
|
+
var count = 0;
|
|
1883
|
+
var keys = Array.isArray(key) ? key : [key];
|
|
1884
|
+
keys.forEach(function (key) {
|
|
1885
|
+
if (cacheValues[key]) {
|
|
1886
|
+
count++;
|
|
1887
|
+
var oldData = cacheValues[key];
|
|
1888
|
+
delete cacheValues[key];
|
|
1889
|
+
_this.emit('del', key, oldData.v);
|
|
1890
|
+
}
|
|
1891
|
+
});
|
|
1892
|
+
if (count > 0) {
|
|
1893
|
+
this.setCacheValues(cacheValues);
|
|
1700
1894
|
}
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
1895
|
+
return count;
|
|
1896
|
+
};
|
|
1897
|
+
// 删除当前所有缓存。
|
|
1898
|
+
Cache.prototype.clear = function () {
|
|
1899
|
+
this.storage.del(this.cacheKey);
|
|
1900
|
+
};
|
|
1901
|
+
// 返回所有现有键的数组。
|
|
1902
|
+
Cache.prototype.keys = function () {
|
|
1903
|
+
var _this = this;
|
|
1904
|
+
var cacheValues = this.cacheValues;
|
|
1905
|
+
var keys = Object.keys(cacheValues);
|
|
1906
|
+
return keys.filter(function (key) { return _this._check(key, cacheValues[key]); });
|
|
1907
|
+
};
|
|
1908
|
+
// 当前缓存是否包含某个键。
|
|
1909
|
+
Cache.prototype.has = function (key) {
|
|
1910
|
+
var data = this.cacheValues[key];
|
|
1911
|
+
return !!(data && this._check(key, data));
|
|
1912
|
+
};
|
|
1913
|
+
// 获取缓存值并从缓存中删除键。
|
|
1914
|
+
Cache.prototype.take = function (key) {
|
|
1915
|
+
var ret;
|
|
1916
|
+
var data = this.cacheValues[key];
|
|
1917
|
+
if (data && this._check(key, data)) {
|
|
1918
|
+
ret = data.v;
|
|
1919
|
+
this.del(key);
|
|
1920
|
+
}
|
|
1921
|
+
return ret;
|
|
1922
|
+
};
|
|
1923
|
+
// 重新定义一个键的 ttl 。如果找到并更新成功,则返回 true 。
|
|
1924
|
+
Cache.prototype.ttl = function (key, ttl) {
|
|
1925
|
+
var cacheValues = this.cacheValues;
|
|
1926
|
+
var data = cacheValues[key];
|
|
1927
|
+
if (data && this._check(key, data)) {
|
|
1928
|
+
cacheValues[key] = this._wrap(data.v, ttl);
|
|
1929
|
+
return true;
|
|
1930
|
+
}
|
|
1931
|
+
return false;
|
|
1932
|
+
};
|
|
1933
|
+
// 获取某个键的 ttl 。
|
|
1934
|
+
// 如果未找到键或已过期,返回 undefined 。
|
|
1935
|
+
// 如果 ttl 为 0 ,返回 0 。
|
|
1936
|
+
// 否则返回一个以毫秒为单位的时间戳,表示键值将过期的时间。
|
|
1937
|
+
Cache.prototype.getTtl = function (key) {
|
|
1938
|
+
var cacheValues = this.cacheValues;
|
|
1939
|
+
var data = cacheValues[key];
|
|
1940
|
+
if (data && this._check(key, data)) {
|
|
1941
|
+
return cacheValues[key].t;
|
|
1942
|
+
}
|
|
1943
|
+
return;
|
|
1944
|
+
};
|
|
1945
|
+
// 获取某个键值的最后修改时间
|
|
1946
|
+
// 如果未找到键或已过期,返回 undefined 。
|
|
1947
|
+
// 否则返回一个以毫秒为单位的时间戳,表示键值将过期的时间。
|
|
1948
|
+
Cache.prototype.getLastModified = function (key) {
|
|
1949
|
+
var cacheValues = this.cacheValues;
|
|
1950
|
+
var data = cacheValues[key];
|
|
1951
|
+
if (data && this._check(key, data)) {
|
|
1952
|
+
return cacheValues[key].n;
|
|
1953
|
+
}
|
|
1954
|
+
return;
|
|
1955
|
+
};
|
|
1956
|
+
// 启动定时校验过期数据
|
|
1957
|
+
Cache.prototype.startCheckperiod = function () {
|
|
1958
|
+
var _this = this;
|
|
1959
|
+
// 触发全部缓存数据是否过期校验
|
|
1960
|
+
this.keys();
|
|
1961
|
+
if (this.options.checkperiod > 0) {
|
|
1962
|
+
clearTimeout(this._checkTimeout);
|
|
1963
|
+
this._checkTimeout = setTimeout(function () {
|
|
1964
|
+
_this.startCheckperiod();
|
|
1965
|
+
}, this.options.checkperiod);
|
|
1966
|
+
}
|
|
1967
|
+
};
|
|
1968
|
+
// 停止定时校验过期数据
|
|
1969
|
+
Cache.prototype.stopCheckperiod = function () {
|
|
1970
|
+
clearTimeout(this._checkTimeout);
|
|
1971
|
+
};
|
|
1972
|
+
return Cache;
|
|
1973
|
+
}(EmitterPro));
|
|
1974
|
+
|
|
1975
|
+
var AsyncMemo = (function () {
|
|
1976
|
+
function AsyncMemo(options) {
|
|
1977
|
+
this.promiseCache = {};
|
|
1978
|
+
this.cache = new Cache(uniqueId('uh_async_memo'), options);
|
|
1979
|
+
}
|
|
1980
|
+
AsyncMemo.prototype.run = function (asyncFn, key, options) {
|
|
1981
|
+
var _this = this;
|
|
1982
|
+
if (!key || !isString(key)) {
|
|
1983
|
+
return asyncFn();
|
|
1984
|
+
}
|
|
1985
|
+
var opts = __assign({ persisted: true }, options);
|
|
1986
|
+
if (opts.persisted) {
|
|
1987
|
+
var data = this.cache.get(key);
|
|
1988
|
+
if (data) {
|
|
1989
|
+
return Promise.resolve(data);
|
|
1990
|
+
}
|
|
1991
|
+
}
|
|
1992
|
+
if (!this.promiseCache[key]) {
|
|
1993
|
+
this.promiseCache[key] = asyncFn()
|
|
1994
|
+
.then(function (res) {
|
|
1995
|
+
delete _this.promiseCache[key];
|
|
1996
|
+
_this.cache.set(key, res, opts.ttl);
|
|
1997
|
+
return res;
|
|
1998
|
+
})
|
|
1999
|
+
.catch(function (err) {
|
|
2000
|
+
delete _this.promiseCache[key];
|
|
2001
|
+
return Promise.reject(err);
|
|
2002
|
+
});
|
|
2003
|
+
}
|
|
2004
|
+
return this.promiseCache[key];
|
|
2005
|
+
};
|
|
2006
|
+
return AsyncMemo;
|
|
2007
|
+
}());
|
|
2008
|
+
|
|
2009
|
+
var asyncMemo$2 = new AsyncMemo({ max: 1, maxStrategy: 'replaced' });
|
|
2010
|
+
asyncMemo$2.cache.on('del', function (k, v) {
|
|
2011
|
+
try {
|
|
2012
|
+
if (v.r) {
|
|
2013
|
+
revokeObjectURL(v.data.image.src);
|
|
2014
|
+
}
|
|
2015
|
+
}
|
|
2016
|
+
catch (_a) {
|
|
2017
|
+
}
|
|
2018
|
+
});
|
|
2019
|
+
function loadImageWithBlob(img, cacheOptions, ajaxOptions) {
|
|
2020
|
+
if (cacheOptions === void 0) { cacheOptions = true; }
|
|
2021
|
+
var cacheOptionsIsObject = typeof cacheOptions === 'object';
|
|
2022
|
+
var _cacheOptions = {
|
|
2023
|
+
useCache: cacheOptionsIsObject ? cacheOptions.useCache !== false : cacheOptions !== false,
|
|
2024
|
+
autoRevokeOnDel: cacheOptionsIsObject ? cacheOptions.autoRevokeOnDel !== false : !!cacheOptions,
|
|
2025
|
+
cacheKey: defaultTo$1(cacheOptionsIsObject ? cacheOptions.cacheKey : undefined, typeof img === 'string' ? img : undefined)
|
|
2026
|
+
};
|
|
2027
|
+
return asyncMemo$2
|
|
2028
|
+
.run(function () {
|
|
2029
|
+
return new Promise(function (resolve, reject) {
|
|
2030
|
+
getFileBlob(img, ajaxOptions)
|
|
2031
|
+
.then(function (blob) {
|
|
2032
|
+
var url = createObjectURL(blob);
|
|
2033
|
+
var image = new Image();
|
|
1706
2034
|
image.onload = function () {
|
|
1707
|
-
var
|
|
1708
|
-
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
});
|
|
1713
|
-
}
|
|
1714
|
-
resolve(result);
|
|
2035
|
+
var data = { blob: blob, image: image };
|
|
2036
|
+
resolve({
|
|
2037
|
+
data: data,
|
|
2038
|
+
r: _cacheOptions.autoRevokeOnDel
|
|
2039
|
+
});
|
|
1715
2040
|
};
|
|
1716
2041
|
image.onerror = function (err) {
|
|
1717
2042
|
revokeObjectURL(url);
|
|
@@ -1721,8 +2046,9 @@
|
|
|
1721
2046
|
image.src = url;
|
|
1722
2047
|
})
|
|
1723
2048
|
.catch(reject);
|
|
1724
|
-
}
|
|
1725
|
-
})
|
|
2049
|
+
});
|
|
2050
|
+
}, _cacheOptions.useCache && _cacheOptions.cacheKey ? _cacheOptions.cacheKey : undefined)
|
|
2051
|
+
.then(function (res) { return res.data; });
|
|
1726
2052
|
}
|
|
1727
2053
|
|
|
1728
2054
|
function canvasToBlob(canvas, type, quality) {
|
|
@@ -1873,14 +2199,14 @@
|
|
|
1873
2199
|
});
|
|
1874
2200
|
}
|
|
1875
2201
|
|
|
1876
|
-
var
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
2202
|
+
var asyncMemo$1 = new AsyncMemo({ max: 1, maxStrategy: 'replaced' });
|
|
2203
|
+
asyncMemo$1.cache.on('del', function (k, v) {
|
|
2204
|
+
try {
|
|
2205
|
+
if (v.r) {
|
|
1880
2206
|
revokeObjectURL(v.data.image.src);
|
|
1881
2207
|
}
|
|
1882
|
-
|
|
1883
|
-
|
|
2208
|
+
}
|
|
2209
|
+
catch (_a) {
|
|
1884
2210
|
}
|
|
1885
2211
|
});
|
|
1886
2212
|
function calcContrast(w, h) {
|
|
@@ -1889,88 +2215,80 @@
|
|
|
1889
2215
|
}
|
|
1890
2216
|
function getImageInfo(img, cacheOptions, ajaxOptions) {
|
|
1891
2217
|
if (cacheOptions === void 0) { cacheOptions = true; }
|
|
2218
|
+
var cacheOptionsIsObject = typeof cacheOptions === 'object';
|
|
1892
2219
|
var _cacheOptions = {
|
|
1893
|
-
useCache:
|
|
1894
|
-
autoRevokeOnDel:
|
|
2220
|
+
useCache: cacheOptionsIsObject ? cacheOptions.useCache !== false : cacheOptions !== false,
|
|
2221
|
+
autoRevokeOnDel: cacheOptionsIsObject ? cacheOptions.autoRevokeOnDel !== false : !!cacheOptions,
|
|
2222
|
+
cacheKey: defaultTo$1(cacheOptionsIsObject ? cacheOptions.cacheKey : undefined, typeof img === 'string' ? img : undefined)
|
|
1895
2223
|
};
|
|
1896
|
-
return
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
r: _cacheOptions.autoRevokeOnDel
|
|
1919
|
-
});
|
|
1920
|
-
}
|
|
1921
|
-
resolve(result);
|
|
1922
|
-
})
|
|
1923
|
-
.catch(reject);
|
|
1924
|
-
}
|
|
1925
|
-
});
|
|
2224
|
+
return asyncMemo$1
|
|
2225
|
+
.run(function () {
|
|
2226
|
+
return loadImageWithBlob(img, false, ajaxOptions).then(function (_a) {
|
|
2227
|
+
var image = _a.image, blob = _a.blob;
|
|
2228
|
+
var width = image.width, height = image.height;
|
|
2229
|
+
var data = {
|
|
2230
|
+
width: width,
|
|
2231
|
+
height: height,
|
|
2232
|
+
contrast: calcContrast(width, height),
|
|
2233
|
+
measure: "".concat(width, " \u00D7 ").concat(height, " px"),
|
|
2234
|
+
size: bytesToSize(blob.size),
|
|
2235
|
+
bytes: blob.size,
|
|
2236
|
+
image: image,
|
|
2237
|
+
blob: blob
|
|
2238
|
+
};
|
|
2239
|
+
return {
|
|
2240
|
+
data: data,
|
|
2241
|
+
r: _cacheOptions.autoRevokeOnDel
|
|
2242
|
+
};
|
|
2243
|
+
});
|
|
2244
|
+
}, _cacheOptions.useCache && _cacheOptions.cacheKey ? _cacheOptions.cacheKey : undefined)
|
|
2245
|
+
.then(function (res) { return res.data; });
|
|
1926
2246
|
}
|
|
1927
2247
|
|
|
1928
|
-
var
|
|
1929
|
-
cache
|
|
1930
|
-
|
|
1931
|
-
|
|
2248
|
+
var asyncMemo = new AsyncMemo({ max: 1, maxStrategy: 'replaced' });
|
|
2249
|
+
asyncMemo.cache.on('del', function (k, v) {
|
|
2250
|
+
try {
|
|
2251
|
+
if (v.r) {
|
|
1932
2252
|
revokeObjectURL(v.data.src);
|
|
1933
2253
|
}
|
|
1934
|
-
|
|
1935
|
-
|
|
2254
|
+
}
|
|
2255
|
+
catch (_a) {
|
|
1936
2256
|
}
|
|
1937
2257
|
});
|
|
1938
2258
|
function loadImage(img, cacheOptions) {
|
|
1939
2259
|
if (cacheOptions === void 0) { cacheOptions = true; }
|
|
2260
|
+
var cacheOptionsIsObject = typeof cacheOptions === 'object';
|
|
1940
2261
|
var _cacheOptions = {
|
|
1941
|
-
useCache:
|
|
1942
|
-
autoRevokeOnDel:
|
|
2262
|
+
useCache: cacheOptionsIsObject ? cacheOptions.useCache !== false : cacheOptions !== false,
|
|
2263
|
+
autoRevokeOnDel: cacheOptionsIsObject ? cacheOptions.autoRevokeOnDel !== false : !!cacheOptions,
|
|
2264
|
+
cacheKey: defaultTo$1(cacheOptionsIsObject ? cacheOptions.cacheKey : undefined, typeof img === 'string' ? img : undefined)
|
|
1943
2265
|
};
|
|
1944
|
-
return
|
|
1945
|
-
|
|
1946
|
-
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
var
|
|
1950
|
-
|
|
1951
|
-
|
|
1952
|
-
if (!imgIsBlob_1) {
|
|
1953
|
-
image_1.crossOrigin = 'anonymous';
|
|
2266
|
+
return asyncMemo
|
|
2267
|
+
.run(function () {
|
|
2268
|
+
return new Promise(function (resolve, reject) {
|
|
2269
|
+
var imgIsBlob = isBlob(img);
|
|
2270
|
+
var url = imgIsBlob ? createObjectURL(img) : img;
|
|
2271
|
+
var image = new Image();
|
|
2272
|
+
if (!imgIsBlob) {
|
|
2273
|
+
image.crossOrigin = 'anonymous';
|
|
1954
2274
|
}
|
|
1955
|
-
|
|
1956
|
-
|
|
1957
|
-
|
|
1958
|
-
|
|
1959
|
-
|
|
1960
|
-
});
|
|
1961
|
-
}
|
|
1962
|
-
resolve(image_1);
|
|
2275
|
+
image.onload = function () {
|
|
2276
|
+
resolve({
|
|
2277
|
+
data: image,
|
|
2278
|
+
r: _cacheOptions.autoRevokeOnDel
|
|
2279
|
+
});
|
|
1963
2280
|
};
|
|
1964
|
-
|
|
1965
|
-
if (
|
|
1966
|
-
revokeObjectURL(
|
|
2281
|
+
image.onerror = function (err) {
|
|
2282
|
+
if (imgIsBlob) {
|
|
2283
|
+
revokeObjectURL(url);
|
|
1967
2284
|
}
|
|
1968
2285
|
console.error("[loadImage] The image load failed, '".concat(img, "'."));
|
|
1969
2286
|
reject(err);
|
|
1970
2287
|
};
|
|
1971
|
-
|
|
1972
|
-
}
|
|
1973
|
-
})
|
|
2288
|
+
image.src = url;
|
|
2289
|
+
});
|
|
2290
|
+
}, _cacheOptions.useCache && _cacheOptions.cacheKey ? _cacheOptions.cacheKey : undefined)
|
|
2291
|
+
.then(function (res) { return res.data; });
|
|
1974
2292
|
}
|
|
1975
2293
|
|
|
1976
2294
|
function loadScript(src, options) {
|
|
@@ -2024,7 +2342,7 @@
|
|
|
2024
2342
|
}
|
|
2025
2343
|
return prefix;
|
|
2026
2344
|
}
|
|
2027
|
-
var randomString
|
|
2345
|
+
var randomString = function (len, pool) {
|
|
2028
2346
|
if (len === void 0) { len = 0; }
|
|
2029
2347
|
var _pool;
|
|
2030
2348
|
if (typeof pool !== 'string') {
|
|
@@ -2322,378 +2640,9 @@
|
|
|
2322
2640
|
return internalFindTreeSelect(tree, predicate, childrenField);
|
|
2323
2641
|
}
|
|
2324
2642
|
|
|
2325
|
-
var VERSION = "4.23.
|
|
2326
|
-
|
|
2327
|
-
// 随机字符串
|
|
2328
|
-
function randomString() {
|
|
2329
|
-
return Math.random().toString(16).substring(2, 8);
|
|
2330
|
-
}
|
|
2331
|
-
// 内部自增id
|
|
2332
|
-
var uid = 1;
|
|
2333
|
-
// 返回唯一标识
|
|
2334
|
-
function getUniqueId() {
|
|
2335
|
-
return "".concat(randomString(), "_").concat(uid++);
|
|
2336
|
-
}
|
|
2337
|
-
// 是否支持 storage
|
|
2338
|
-
function isStorageSupported(storage) {
|
|
2339
|
-
try {
|
|
2340
|
-
var isSupport = typeof storage === 'object' &&
|
|
2341
|
-
storage !== null &&
|
|
2342
|
-
!!storage.setItem &&
|
|
2343
|
-
!!storage.getItem &&
|
|
2344
|
-
!!storage.removeItem;
|
|
2345
|
-
if (isSupport) {
|
|
2346
|
-
var key = getUniqueId();
|
|
2347
|
-
var value = '1';
|
|
2348
|
-
storage.setItem(key, value);
|
|
2349
|
-
if (storage.getItem(key) !== value) {
|
|
2350
|
-
return false;
|
|
2351
|
-
}
|
|
2352
|
-
storage.removeItem(key);
|
|
2353
|
-
}
|
|
2354
|
-
return isSupport;
|
|
2355
|
-
}
|
|
2356
|
-
catch (e) {
|
|
2357
|
-
console.error("[cache2] ".concat(storage, " is not supported. The default memory cache will be used."));
|
|
2358
|
-
return false;
|
|
2359
|
-
}
|
|
2360
|
-
}
|
|
2361
|
-
function parse(value, reviver) {
|
|
2362
|
-
try {
|
|
2363
|
-
return JSON.parse(value, reviver);
|
|
2364
|
-
}
|
|
2365
|
-
catch (e) {
|
|
2366
|
-
return value;
|
|
2367
|
-
}
|
|
2368
|
-
}
|
|
2369
|
-
function stringify(value, replacer) {
|
|
2370
|
-
return JSON.stringify(value, replacer);
|
|
2371
|
-
}
|
|
2372
|
-
var inWindow = typeof window !== 'undefined' && typeof window === 'object' && window.window === window;
|
|
2373
|
-
|
|
2374
|
-
var cache = {};
|
|
2375
|
-
var MemoryStorage = /** @class */ (function () {
|
|
2376
|
-
function MemoryStorage(scope) {
|
|
2377
|
-
if (scope === void 0) { scope = 'default'; }
|
|
2378
|
-
this.scope = scope;
|
|
2379
|
-
if (!cache[this.scope]) {
|
|
2380
|
-
cache[this.scope] = {};
|
|
2381
|
-
}
|
|
2382
|
-
this.data = cache[this.scope];
|
|
2383
|
-
}
|
|
2384
|
-
MemoryStorage.prototype.getItem = function (key) {
|
|
2385
|
-
return key in this.data ? this.data[key] : null;
|
|
2386
|
-
};
|
|
2387
|
-
MemoryStorage.prototype.setItem = function (key, value) {
|
|
2388
|
-
this.data[key] = value;
|
|
2389
|
-
};
|
|
2390
|
-
MemoryStorage.prototype.removeItem = function (key) {
|
|
2391
|
-
delete this.data[key];
|
|
2392
|
-
};
|
|
2393
|
-
MemoryStorage.prototype.clear = function () {
|
|
2394
|
-
cache[this.scope] = {};
|
|
2395
|
-
this.data = cache[this.scope];
|
|
2396
|
-
};
|
|
2397
|
-
return MemoryStorage;
|
|
2398
|
-
}());
|
|
2399
|
-
|
|
2400
|
-
var Storage = /** @class */ (function () {
|
|
2401
|
-
function Storage(storage, options) {
|
|
2402
|
-
if (options === void 0) { options = {}; }
|
|
2403
|
-
var isSupported = storage ? isStorageSupported(storage) : false;
|
|
2404
|
-
this.options = __assign({ needParsed: isSupported }, options);
|
|
2405
|
-
this.storage = isSupported ? storage : new MemoryStorage(this.options.memoryScope);
|
|
2406
|
-
}
|
|
2407
|
-
Storage.prototype.get = function (key) {
|
|
2408
|
-
var data = this.storage.getItem(key);
|
|
2409
|
-
return this.options.needParsed ? parse(data, this.options.reviver) : data;
|
|
2410
|
-
};
|
|
2411
|
-
Storage.prototype.set = function (key, data) {
|
|
2412
|
-
this.storage.setItem(key, this.options.needParsed ? stringify(data, this.options.replacer) : data);
|
|
2413
|
-
};
|
|
2414
|
-
Storage.prototype.del = function (key) {
|
|
2415
|
-
this.storage.removeItem(key);
|
|
2416
|
-
};
|
|
2417
|
-
Storage.prototype.clear = function () {
|
|
2418
|
-
if (typeof this.storage.clear === 'function') {
|
|
2419
|
-
this.storage.clear();
|
|
2420
|
-
}
|
|
2421
|
-
};
|
|
2422
|
-
return Storage;
|
|
2423
|
-
}());
|
|
2424
|
-
|
|
2425
|
-
var defaultPrefix = 'cache2_'; // 命名空间缓存键前缀,默认 cache2_ 。
|
|
2426
|
-
var defaultNamespace = 'default';
|
|
2427
|
-
var Cache = /** @class */ (function (_super) {
|
|
2428
|
-
__extends(Cache, _super);
|
|
2429
|
-
function Cache(namespace, options) {
|
|
2430
|
-
var _this = _super.call(this) || this;
|
|
2431
|
-
var ns = defaultNamespace, opts;
|
|
2432
|
-
if (typeof namespace === 'string') {
|
|
2433
|
-
ns = namespace;
|
|
2434
|
-
}
|
|
2435
|
-
else if (typeof namespace === 'object') {
|
|
2436
|
-
opts = namespace;
|
|
2437
|
-
}
|
|
2438
|
-
if (!opts && typeof options === 'object') {
|
|
2439
|
-
opts = options;
|
|
2440
|
-
}
|
|
2441
|
-
_this.options = __assign({ max: -1, stdTTL: 0, maxStrategy: 'limited', checkperiod: 0, prefix: defaultPrefix }, opts);
|
|
2442
|
-
_this.storage = new Storage(_this.options.storage, __assign({ memoryScope: ns }, opts));
|
|
2443
|
-
_this.cacheKey = (_this.options.prefix || '') + (ns || '') || getUniqueId();
|
|
2444
|
-
_this.startCheckperiod();
|
|
2445
|
-
return _this;
|
|
2446
|
-
}
|
|
2447
|
-
// 检查当前键值是否过期,如果过期将会自动删除
|
|
2448
|
-
Cache.prototype._check = function (key, data) {
|
|
2449
|
-
var ret = true;
|
|
2450
|
-
if (data.t !== 0 && data.t < Date.now()) {
|
|
2451
|
-
ret = false;
|
|
2452
|
-
this.del(key);
|
|
2453
|
-
this.emit('expired', key, data.v);
|
|
2454
|
-
}
|
|
2455
|
-
return ret;
|
|
2456
|
-
};
|
|
2457
|
-
Cache.prototype._wrap = function (value, ttl) {
|
|
2458
|
-
var now = Date.now();
|
|
2459
|
-
var currentTtl = typeof ttl === 'number' ? ttl : this.options.stdTTL;
|
|
2460
|
-
var livetime = currentTtl > 0 ? now + currentTtl : 0;
|
|
2461
|
-
return {
|
|
2462
|
-
v: value,
|
|
2463
|
-
t: livetime,
|
|
2464
|
-
n: now
|
|
2465
|
-
};
|
|
2466
|
-
};
|
|
2467
|
-
Cache.prototype._isLimited = function (len) {
|
|
2468
|
-
return this.options.max > -1 && len >= this.options.max;
|
|
2469
|
-
};
|
|
2470
|
-
Cache.prototype._getReplaceKey = function (keys, cacheValues) {
|
|
2471
|
-
var retkey = keys[0];
|
|
2472
|
-
keys.forEach(function (key) {
|
|
2473
|
-
if (cacheValues[key].t < cacheValues[retkey].t ||
|
|
2474
|
-
(cacheValues[key].t === cacheValues[retkey].t && cacheValues[key].n < cacheValues[retkey].n)) {
|
|
2475
|
-
retkey = key;
|
|
2476
|
-
}
|
|
2477
|
-
});
|
|
2478
|
-
return retkey;
|
|
2479
|
-
};
|
|
2480
|
-
Object.defineProperty(Cache.prototype, "cacheValues", {
|
|
2481
|
-
// 获取全部缓存数据,不处理过期数据和排序
|
|
2482
|
-
get: function () {
|
|
2483
|
-
return this.storage.get(this.cacheKey) || {};
|
|
2484
|
-
},
|
|
2485
|
-
enumerable: false,
|
|
2486
|
-
configurable: true
|
|
2487
|
-
});
|
|
2488
|
-
// 设置缓存数据
|
|
2489
|
-
Cache.prototype.setCacheValues = function (values) {
|
|
2490
|
-
this.storage.set(this.cacheKey, values);
|
|
2491
|
-
};
|
|
2492
|
-
// 从缓存中获取保存的值。如果未找到或已过期,则返回 undefined 。如果找到该值,则返回该值。
|
|
2493
|
-
Cache.prototype.get = function (key) {
|
|
2494
|
-
var data = this.cacheValues[key];
|
|
2495
|
-
if (data && this._check(key, data)) {
|
|
2496
|
-
return data.v;
|
|
2497
|
-
}
|
|
2498
|
-
return;
|
|
2499
|
-
};
|
|
2500
|
-
// 从缓存中获取多个保存的值。如果未找到或已过期,则返回一个空对象。如果找到该值,它会返回一个具有键值对的对象。
|
|
2501
|
-
Cache.prototype.mget = function (keys) {
|
|
2502
|
-
var _this = this;
|
|
2503
|
-
var ret = {};
|
|
2504
|
-
if (!Array.isArray(keys)) {
|
|
2505
|
-
return ret;
|
|
2506
|
-
}
|
|
2507
|
-
var cacheValues = this.cacheValues;
|
|
2508
|
-
keys.forEach(function (key) {
|
|
2509
|
-
var data = cacheValues[key];
|
|
2510
|
-
if (data && _this._check(key, data)) {
|
|
2511
|
-
ret[key] = data.v;
|
|
2512
|
-
}
|
|
2513
|
-
});
|
|
2514
|
-
return ret;
|
|
2515
|
-
};
|
|
2516
|
-
// 从缓存中获取全部保存的值。返回一个具有键值对的对象。
|
|
2517
|
-
Cache.prototype.getAll = function () {
|
|
2518
|
-
var keys = Object.keys(this.cacheValues);
|
|
2519
|
-
return this.mget(keys);
|
|
2520
|
-
};
|
|
2521
|
-
// 设置键值对。设置成功返回 true 。
|
|
2522
|
-
Cache.prototype.set = function (key, value, ttl) {
|
|
2523
|
-
if (this.options.max === 0) {
|
|
2524
|
-
return false;
|
|
2525
|
-
}
|
|
2526
|
-
var cacheValues = this.cacheValues;
|
|
2527
|
-
var keys = Object.keys(cacheValues);
|
|
2528
|
-
// 当前不存在该键值,并且数据量超过最大限制
|
|
2529
|
-
if (!cacheValues[key] && this._isLimited(keys.length)) {
|
|
2530
|
-
var validKeys = this.keys();
|
|
2531
|
-
if (this._isLimited(validKeys.length)) {
|
|
2532
|
-
// 如果最大限制策略是替换,将优先替换快过期的数据,如果都是一样的过期时间(0),按照先入先出规则处理。
|
|
2533
|
-
if (this.options.maxStrategy === 'replaced') {
|
|
2534
|
-
var replaceKey = this._getReplaceKey(validKeys, cacheValues);
|
|
2535
|
-
this.del(replaceKey);
|
|
2536
|
-
}
|
|
2537
|
-
else {
|
|
2538
|
-
// 如果是最大限制策略是不允许添加,返回 false 。
|
|
2539
|
-
return false;
|
|
2540
|
-
}
|
|
2541
|
-
}
|
|
2542
|
-
}
|
|
2543
|
-
cacheValues[key] = this._wrap(value, ttl);
|
|
2544
|
-
this.setCacheValues(cacheValues);
|
|
2545
|
-
this.emit('set', key, cacheValues[key].v);
|
|
2546
|
-
return true;
|
|
2547
|
-
};
|
|
2548
|
-
// 设置多个键值对。全部设置成功返回 true 。
|
|
2549
|
-
Cache.prototype.mset = function (keyValueSet) {
|
|
2550
|
-
var _this = this;
|
|
2551
|
-
// 该处不使用数组 some 方法,是因为不能某个失败,而导致其他就不在更新。
|
|
2552
|
-
var ret = true;
|
|
2553
|
-
keyValueSet.forEach(function (item) {
|
|
2554
|
-
var itemSetResult = _this.set(item.key, item.value, item.ttl);
|
|
2555
|
-
if (ret && !itemSetResult) {
|
|
2556
|
-
ret = false;
|
|
2557
|
-
}
|
|
2558
|
-
});
|
|
2559
|
-
return ret;
|
|
2560
|
-
};
|
|
2561
|
-
// 删除一个或多个键。返回已删除条目的数量。删除永远不会失败。
|
|
2562
|
-
Cache.prototype.del = function (key) {
|
|
2563
|
-
var _this = this;
|
|
2564
|
-
var cacheValues = this.cacheValues;
|
|
2565
|
-
var count = 0;
|
|
2566
|
-
var keys = Array.isArray(key) ? key : [key];
|
|
2567
|
-
keys.forEach(function (key) {
|
|
2568
|
-
if (cacheValues[key]) {
|
|
2569
|
-
count++;
|
|
2570
|
-
var oldData = cacheValues[key];
|
|
2571
|
-
delete cacheValues[key];
|
|
2572
|
-
_this.emit('del', key, oldData.v);
|
|
2573
|
-
}
|
|
2574
|
-
});
|
|
2575
|
-
if (count > 0) {
|
|
2576
|
-
this.setCacheValues(cacheValues);
|
|
2577
|
-
}
|
|
2578
|
-
return count;
|
|
2579
|
-
};
|
|
2580
|
-
// 删除当前所有缓存。
|
|
2581
|
-
Cache.prototype.clear = function () {
|
|
2582
|
-
this.storage.del(this.cacheKey);
|
|
2583
|
-
};
|
|
2584
|
-
// 返回所有现有键的数组。
|
|
2585
|
-
Cache.prototype.keys = function () {
|
|
2586
|
-
var _this = this;
|
|
2587
|
-
var cacheValues = this.cacheValues;
|
|
2588
|
-
var keys = Object.keys(cacheValues);
|
|
2589
|
-
return keys.filter(function (key) { return _this._check(key, cacheValues[key]); });
|
|
2590
|
-
};
|
|
2591
|
-
// 当前缓存是否包含某个键。
|
|
2592
|
-
Cache.prototype.has = function (key) {
|
|
2593
|
-
var data = this.cacheValues[key];
|
|
2594
|
-
return !!(data && this._check(key, data));
|
|
2595
|
-
};
|
|
2596
|
-
// 获取缓存值并从缓存中删除键。
|
|
2597
|
-
Cache.prototype.take = function (key) {
|
|
2598
|
-
var ret;
|
|
2599
|
-
var data = this.cacheValues[key];
|
|
2600
|
-
if (data && this._check(key, data)) {
|
|
2601
|
-
ret = data.v;
|
|
2602
|
-
this.del(key);
|
|
2603
|
-
}
|
|
2604
|
-
return ret;
|
|
2605
|
-
};
|
|
2606
|
-
// 重新定义一个键的 ttl 。如果找到并更新成功,则返回 true 。
|
|
2607
|
-
Cache.prototype.ttl = function (key, ttl) {
|
|
2608
|
-
var cacheValues = this.cacheValues;
|
|
2609
|
-
var data = cacheValues[key];
|
|
2610
|
-
if (data && this._check(key, data)) {
|
|
2611
|
-
cacheValues[key] = this._wrap(data.v, ttl);
|
|
2612
|
-
return true;
|
|
2613
|
-
}
|
|
2614
|
-
return false;
|
|
2615
|
-
};
|
|
2616
|
-
// 获取某个键的 ttl 。
|
|
2617
|
-
// 如果未找到键或已过期,返回 undefined 。
|
|
2618
|
-
// 如果 ttl 为 0 ,返回 0 。
|
|
2619
|
-
// 否则返回一个以毫秒为单位的时间戳,表示键值将过期的时间。
|
|
2620
|
-
Cache.prototype.getTtl = function (key) {
|
|
2621
|
-
var cacheValues = this.cacheValues;
|
|
2622
|
-
var data = cacheValues[key];
|
|
2623
|
-
if (data && this._check(key, data)) {
|
|
2624
|
-
return cacheValues[key].t;
|
|
2625
|
-
}
|
|
2626
|
-
return;
|
|
2627
|
-
};
|
|
2628
|
-
// 获取某个键值的最后修改时间
|
|
2629
|
-
// 如果未找到键或已过期,返回 undefined 。
|
|
2630
|
-
// 否则返回一个以毫秒为单位的时间戳,表示键值将过期的时间。
|
|
2631
|
-
Cache.prototype.getLastModified = function (key) {
|
|
2632
|
-
var cacheValues = this.cacheValues;
|
|
2633
|
-
var data = cacheValues[key];
|
|
2634
|
-
if (data && this._check(key, data)) {
|
|
2635
|
-
return cacheValues[key].n;
|
|
2636
|
-
}
|
|
2637
|
-
return;
|
|
2638
|
-
};
|
|
2639
|
-
// 启动定时校验过期数据
|
|
2640
|
-
Cache.prototype.startCheckperiod = function () {
|
|
2641
|
-
var _this = this;
|
|
2642
|
-
// 触发全部缓存数据是否过期校验
|
|
2643
|
-
this.keys();
|
|
2644
|
-
if (this.options.checkperiod > 0) {
|
|
2645
|
-
clearTimeout(this._checkTimeout);
|
|
2646
|
-
this._checkTimeout = setTimeout(function () {
|
|
2647
|
-
_this.startCheckperiod();
|
|
2648
|
-
}, this.options.checkperiod);
|
|
2649
|
-
}
|
|
2650
|
-
};
|
|
2651
|
-
// 停止定时校验过期数据
|
|
2652
|
-
Cache.prototype.stopCheckperiod = function () {
|
|
2653
|
-
clearTimeout(this._checkTimeout);
|
|
2654
|
-
};
|
|
2655
|
-
return Cache;
|
|
2656
|
-
}(EmitterPro));
|
|
2657
|
-
|
|
2658
|
-
new Storage(inWindow ? window.localStorage : undefined);
|
|
2659
|
-
|
|
2660
|
-
new Storage(inWindow ? window.sessionStorage : undefined);
|
|
2661
|
-
|
|
2662
|
-
var AsyncMemo = (function () {
|
|
2663
|
-
function AsyncMemo(options) {
|
|
2664
|
-
this.promiseCache = {};
|
|
2665
|
-
this.cache = new Cache(uniqueId('uh_async_memo'), options);
|
|
2666
|
-
}
|
|
2667
|
-
AsyncMemo.prototype.run = function (asyncFn, key, options) {
|
|
2668
|
-
var _this = this;
|
|
2669
|
-
if (!key || !isString(key)) {
|
|
2670
|
-
return asyncFn();
|
|
2671
|
-
}
|
|
2672
|
-
var opts = __assign({ persisted: true }, options);
|
|
2673
|
-
if (opts.persisted) {
|
|
2674
|
-
var data = this.cache.get(key);
|
|
2675
|
-
if (data) {
|
|
2676
|
-
return Promise.resolve(data);
|
|
2677
|
-
}
|
|
2678
|
-
}
|
|
2679
|
-
if (!this.promiseCache[key]) {
|
|
2680
|
-
this.promiseCache[key] = asyncFn()
|
|
2681
|
-
.then(function (res) {
|
|
2682
|
-
delete _this.promiseCache[key];
|
|
2683
|
-
_this.cache.set(key, res, opts.ttl);
|
|
2684
|
-
return res;
|
|
2685
|
-
})
|
|
2686
|
-
.catch(function (err) {
|
|
2687
|
-
delete _this.promiseCache[key];
|
|
2688
|
-
return Promise.reject(err);
|
|
2689
|
-
});
|
|
2690
|
-
}
|
|
2691
|
-
return this.promiseCache[key];
|
|
2692
|
-
};
|
|
2693
|
-
return AsyncMemo;
|
|
2694
|
-
}());
|
|
2643
|
+
var VERSION = "4.23.2";
|
|
2695
2644
|
|
|
2696
|
-
var version = "4.23.
|
|
2645
|
+
var version = "4.23.2";
|
|
2697
2646
|
|
|
2698
2647
|
exports.AsyncMemo = AsyncMemo;
|
|
2699
2648
|
exports.VERSION = VERSION;
|
|
@@ -2749,7 +2698,7 @@
|
|
|
2749
2698
|
exports.padZero = padZero;
|
|
2750
2699
|
exports.parseIdCard = parseIdCard;
|
|
2751
2700
|
exports.plus = plus;
|
|
2752
|
-
exports.randomString = randomString
|
|
2701
|
+
exports.randomString = randomString;
|
|
2753
2702
|
exports.replaceChar = replaceChar;
|
|
2754
2703
|
exports.round = _round;
|
|
2755
2704
|
exports.safeDate = safeDate;
|