util-helpers 5.7.8 → 5.8.1
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/README.md +2 -2
- package/dist/util-helpers.js +140 -682
- 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/ConcurrencyController.js +54 -0
- package/esm/VERSION.js +1 -1
- package/esm/index.js +1 -0
- package/esm/isIdCard.js +3 -3
- package/esm/randomString.js +4 -1
- package/lib/ConcurrencyController.js +56 -0
- package/lib/VERSION.js +1 -1
- package/lib/index.js +2 -0
- package/lib/isIdCard.js +3 -3
- package/lib/randomString.js +4 -1
- package/package.json +4 -4
- package/types/ConcurrencyController.d.ts +45 -2
- package/types/index.d.ts +1 -0
- package/types/isIdCard.d.ts +1 -1
- package/types/randomString.d.ts +12 -2
package/dist/util-helpers.js
CHANGED
|
@@ -405,25 +405,25 @@
|
|
|
405
405
|
|
|
406
406
|
function noop() { }
|
|
407
407
|
|
|
408
|
-
var reg$
|
|
408
|
+
var reg$c = /^1[3456789]\d{9}$/;
|
|
409
409
|
function isMobile(value) {
|
|
410
410
|
var valueStr = toString(value);
|
|
411
|
-
return reg$
|
|
411
|
+
return reg$c.test(valueStr);
|
|
412
412
|
}
|
|
413
413
|
|
|
414
|
-
var reg$
|
|
414
|
+
var reg$b = /^(0\d{2,3}-)?([2-9]\d{6,7})(-\d{1,6})?$/;
|
|
415
415
|
function isTelephone(value) {
|
|
416
416
|
var valueStr = toString(value);
|
|
417
|
-
return reg$
|
|
417
|
+
return reg$b.test(valueStr);
|
|
418
418
|
}
|
|
419
419
|
|
|
420
|
-
var reg$
|
|
420
|
+
var reg$a = /^\d{6}$/;
|
|
421
421
|
function isPostcode(value) {
|
|
422
422
|
var valueStr = toString(value);
|
|
423
|
-
return reg$
|
|
423
|
+
return reg$a.test(valueStr);
|
|
424
424
|
}
|
|
425
425
|
|
|
426
|
-
var
|
|
426
|
+
var reg$9 = /^[1-9]\d{5}(19|20)?\d{2}((0[1-9])|(1[012]))(([0-2][1-9])|10|20|30|31)\d{3}(\d|X)?$/i;
|
|
427
427
|
function check(id) {
|
|
428
428
|
var index, sum;
|
|
429
429
|
for (sum = index = 0; index < 17; index++) {
|
|
@@ -442,9 +442,9 @@
|
|
|
442
442
|
var _a = options.loose, loose = _a === void 0 ? false : _a, _b = options.checkCode, checkCode = _b === void 0 ? true : _b;
|
|
443
443
|
var valueStr = toString(value);
|
|
444
444
|
if (valueStr.length === 15 && loose) {
|
|
445
|
-
return
|
|
445
|
+
return reg$9.test(valueStr);
|
|
446
446
|
}
|
|
447
|
-
if (valueStr.length === 18 &&
|
|
447
|
+
if (valueStr.length === 18 && reg$9.test(valueStr)) {
|
|
448
448
|
if (checkCode) {
|
|
449
449
|
return check(valueStr);
|
|
450
450
|
}
|
|
@@ -1902,10 +1902,13 @@
|
|
|
1902
1902
|
}
|
|
1903
1903
|
return prefix;
|
|
1904
1904
|
}
|
|
1905
|
-
var randomString
|
|
1905
|
+
var randomString = function (len, pool) {
|
|
1906
1906
|
if (len === void 0) { len = 0; }
|
|
1907
1907
|
var _pool;
|
|
1908
|
-
if (
|
|
1908
|
+
if (Array.isArray(pool)) {
|
|
1909
|
+
_pool = pool.map(function (p) { return chars[p]; }).join('');
|
|
1910
|
+
}
|
|
1911
|
+
else if (typeof pool !== 'string') {
|
|
1909
1912
|
_pool = allChars;
|
|
1910
1913
|
}
|
|
1911
1914
|
else if (chars[pool]) {
|
|
@@ -2202,111 +2205,26 @@
|
|
|
2202
2205
|
return internalFindTreeSelect(tree, predicate, childrenField);
|
|
2203
2206
|
}
|
|
2204
2207
|
|
|
2205
|
-
var VERSION = "5.
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
* 事件触发器,支持浏览器端和 node 端。
|
|
2209
|
-
*
|
|
2210
|
-
* @class
|
|
2211
|
-
* @example
|
|
2212
|
-
*
|
|
2213
|
-
* const emitter = new Emitter();
|
|
2214
|
-
*
|
|
2215
|
-
* // 注册监听方法
|
|
2216
|
-
* emitter.on('foo', () => console.log('foo 1'));
|
|
2217
|
-
* emitter.on('foo', () => console.log('foo 2'));
|
|
2218
|
-
*
|
|
2219
|
-
* // 触发方法
|
|
2220
|
-
* emitter.emit('foo');
|
|
2221
|
-
* // foo 1
|
|
2222
|
-
* // foo 2
|
|
2223
|
-
*
|
|
2224
|
-
* // 取消监听方法
|
|
2225
|
-
* emitter.off('foo');
|
|
2226
|
-
*
|
|
2227
|
-
* // 支持链式调用
|
|
2228
|
-
* emitter.on('foo', () => {})
|
|
2229
|
-
* .on('foo', () => {})
|
|
2230
|
-
* .off('foo');
|
|
2231
|
-
*/
|
|
2232
|
-
var EmitterPro = /** @class */ (function () {
|
|
2208
|
+
var VERSION = "5.8.1";
|
|
2209
|
+
|
|
2210
|
+
var EmitterPro = (function () {
|
|
2233
2211
|
function EmitterPro() {
|
|
2234
2212
|
this.handlers = {};
|
|
2235
2213
|
}
|
|
2236
|
-
/**
|
|
2237
|
-
* 获取全部事件名称。
|
|
2238
|
-
*
|
|
2239
|
-
* @returns 事件名称数组。
|
|
2240
|
-
* @example
|
|
2241
|
-
* emitter.on('foo', () => {});
|
|
2242
|
-
* emitter.on('bar', () => {});
|
|
2243
|
-
*
|
|
2244
|
-
* emitter.eventNames(); // ['foo', 'bar']
|
|
2245
|
-
*/
|
|
2246
2214
|
EmitterPro.prototype.eventNames = function () {
|
|
2247
2215
|
var _a;
|
|
2248
2216
|
var symbols = ((_a = Object.getOwnPropertySymbols) === null || _a === void 0 ? void 0 : _a.call(Object, this.handlers)) || [];
|
|
2249
2217
|
var keys = Object.keys(this.handlers);
|
|
2250
2218
|
return keys.concat(symbols);
|
|
2251
2219
|
};
|
|
2252
|
-
/**
|
|
2253
|
-
* 获取事件名称的全部监听方法(原始方法,未经过包装处理)。
|
|
2254
|
-
*
|
|
2255
|
-
* @param eventName 事件名称
|
|
2256
|
-
* @returns 对应事件名称的监听方法数组
|
|
2257
|
-
* @example
|
|
2258
|
-
* const fn1 = () => console.log('bar');
|
|
2259
|
-
* const fn2 = () => console.log('baz');
|
|
2260
|
-
*
|
|
2261
|
-
* emitter.on('test', fn1);
|
|
2262
|
-
* emitter.once('test', fn2);
|
|
2263
|
-
*
|
|
2264
|
-
* emitter.rawListeners('test'); // [fn1, fn2]
|
|
2265
|
-
*/
|
|
2266
2220
|
EmitterPro.prototype.rawListeners = function (eventName) {
|
|
2267
2221
|
var handler = this.handlers[eventName];
|
|
2268
2222
|
return handler ? handler.map(function (item) { return item.raw; }) : [];
|
|
2269
2223
|
};
|
|
2270
|
-
/**
|
|
2271
|
-
* 获取事件名称的全部监听方法(如通过 `once` 方法注册,返回的是包装方法)。
|
|
2272
|
-
*
|
|
2273
|
-
* @param eventName 事件名称
|
|
2274
|
-
* @returns 对应事件名称的监听方法数组
|
|
2275
|
-
* @example
|
|
2276
|
-
* const fn1 = () => console.log('bar');
|
|
2277
|
-
* const fn2 = () => console.log('baz');
|
|
2278
|
-
*
|
|
2279
|
-
* emitter.on('test', fn1);
|
|
2280
|
-
* emitter.once('test', fn2);
|
|
2281
|
-
*
|
|
2282
|
-
* emitter.rawListeners('test'); // [fn1, wrapFn2]
|
|
2283
|
-
*/
|
|
2284
2224
|
EmitterPro.prototype.listeners = function (eventName) {
|
|
2285
2225
|
var handler = this.handlers[eventName];
|
|
2286
2226
|
return handler ? handler.map(function (item) { return item.wrap; }) : [];
|
|
2287
2227
|
};
|
|
2288
|
-
/**
|
|
2289
|
-
* 判断事件名称对应的监听方法是否存在。
|
|
2290
|
-
*
|
|
2291
|
-
* @param eventName 事件名称
|
|
2292
|
-
* @param listener 监听方法
|
|
2293
|
-
* @returns 如果事件名称存在该事件方法返回 `true`,否则返回 `false`。
|
|
2294
|
-
* @example
|
|
2295
|
-
* const fn1 = () => console.log('bar');
|
|
2296
|
-
* const fn2 = () => console.log('baz');
|
|
2297
|
-
*
|
|
2298
|
-
* emitter.on('test', fn1);
|
|
2299
|
-
* emitter.once('test', fn2);
|
|
2300
|
-
*
|
|
2301
|
-
* emitter.hasListener('test', fn1); // true
|
|
2302
|
-
* emitter.hasListener('test', fn2); // true
|
|
2303
|
-
*
|
|
2304
|
-
* // fn2 是通过 once 方法注册,执行一次后自动解绑
|
|
2305
|
-
* emitter.emit('test');
|
|
2306
|
-
*
|
|
2307
|
-
* emitter.hasListener('test', fn1); // true
|
|
2308
|
-
* emitter.hasListener('test', fn2); // false
|
|
2309
|
-
*/
|
|
2310
2228
|
EmitterPro.prototype.hasListener = function (eventName, listener) {
|
|
2311
2229
|
return this.rawListeners(eventName).some(function (item) { return item === listener; });
|
|
2312
2230
|
};
|
|
@@ -2323,39 +2241,9 @@
|
|
|
2323
2241
|
}
|
|
2324
2242
|
return this;
|
|
2325
2243
|
};
|
|
2326
|
-
/**
|
|
2327
|
-
* 注册监听方法。同 `on` 方法,只是将监听方法添加到最前面(事件触发是按顺序执行)。
|
|
2328
|
-
*
|
|
2329
|
-
* @param eventName 事件名称
|
|
2330
|
-
* @param listener 监听方法
|
|
2331
|
-
* @param context 执行上下文
|
|
2332
|
-
* @returns 事件触发器实例。
|
|
2333
|
-
* @example
|
|
2334
|
-
* emitter.on('foo', () => console.log('bar'));
|
|
2335
|
-
* emitter.prependListener('foo', () => console.log(42));
|
|
2336
|
-
*
|
|
2337
|
-
* emitter.emit('foo');
|
|
2338
|
-
* // 42
|
|
2339
|
-
* // bar
|
|
2340
|
-
*/
|
|
2341
2244
|
EmitterPro.prototype.prependListener = function (eventName, listener, context) {
|
|
2342
2245
|
return this._on(eventName, listener, listener, context, 0);
|
|
2343
2246
|
};
|
|
2344
|
-
/**
|
|
2345
|
-
* 注册监听方法。允许多次添加同一引用的函数。
|
|
2346
|
-
*
|
|
2347
|
-
* @param eventName 事件名称
|
|
2348
|
-
* @param listener 监听方法
|
|
2349
|
-
* @param context 执行上下文
|
|
2350
|
-
* @returns 事件触发器实例。
|
|
2351
|
-
* @example
|
|
2352
|
-
* emitter.on('foo', () => console.log('bar'));
|
|
2353
|
-
* emitter.on('foo', () => console.log(42));
|
|
2354
|
-
*
|
|
2355
|
-
* emitter.emit('foo');
|
|
2356
|
-
* // bar
|
|
2357
|
-
* // 42
|
|
2358
|
-
*/
|
|
2359
2247
|
EmitterPro.prototype.on = function (eventName, listener, context) {
|
|
2360
2248
|
return this._on(eventName, listener, listener, context);
|
|
2361
2249
|
};
|
|
@@ -2372,76 +2260,14 @@
|
|
|
2372
2260
|
});
|
|
2373
2261
|
return wrap;
|
|
2374
2262
|
};
|
|
2375
|
-
/**
|
|
2376
|
-
* 仅触发一次的监听方法。使用方法同 `on` 。
|
|
2377
|
-
*
|
|
2378
|
-
* @param eventName 事件名称
|
|
2379
|
-
* @param listener 监听方法
|
|
2380
|
-
* @param context 执行上下文
|
|
2381
|
-
* @returns 事件触发器实例。
|
|
2382
|
-
* @example
|
|
2383
|
-
* emitter.on('foo', () => console.log('bar'));
|
|
2384
|
-
* emitter.once('foo', () => console.log(42));
|
|
2385
|
-
*
|
|
2386
|
-
* emitter.emit('foo');
|
|
2387
|
-
* // bar
|
|
2388
|
-
* // 42
|
|
2389
|
-
*
|
|
2390
|
-
* emitter.emit('foo');
|
|
2391
|
-
* // bar
|
|
2392
|
-
*/
|
|
2393
2263
|
EmitterPro.prototype.once = function (eventName, listener, context) {
|
|
2394
2264
|
var wrap = this._wrapOnce(eventName, listener, context);
|
|
2395
2265
|
return this._on(eventName, listener, wrap, context);
|
|
2396
2266
|
};
|
|
2397
|
-
/**
|
|
2398
|
-
* 仅触发一次的监听方法。同 `once` 方法,只是添加到最前面(事件触发是按顺序执行)。
|
|
2399
|
-
*
|
|
2400
|
-
* @param eventName 事件名称
|
|
2401
|
-
* @param listener 监听方法
|
|
2402
|
-
* @param context 执行上下文
|
|
2403
|
-
* @returns 事件触发器实例。
|
|
2404
|
-
* @example
|
|
2405
|
-
* emitter.on('foo', () => console.log('bar'));
|
|
2406
|
-
* emitter.prependOnceListener('foo', () => console.log(42));
|
|
2407
|
-
*
|
|
2408
|
-
* emitter.emit('foo');
|
|
2409
|
-
* // 42
|
|
2410
|
-
* // bar
|
|
2411
|
-
*
|
|
2412
|
-
* emitter.emit('foo');
|
|
2413
|
-
* // bar
|
|
2414
|
-
*/
|
|
2415
2267
|
EmitterPro.prototype.prependOnceListener = function (eventName, listener, context) {
|
|
2416
2268
|
var wrap = this._wrapOnce(eventName, listener, context);
|
|
2417
2269
|
return this._on(eventName, listener, wrap, context, 0);
|
|
2418
2270
|
};
|
|
2419
|
-
/**
|
|
2420
|
-
* 取消监听方法。如果不传第二个参数,将取消该事件名称的全部监听方法。如果多次添加同一引用的函数,需要多次删除。
|
|
2421
|
-
*
|
|
2422
|
-
* @param eventName 事件名称
|
|
2423
|
-
* @param listener 监听方法
|
|
2424
|
-
* @returns 事件触发器实例。
|
|
2425
|
-
* @example
|
|
2426
|
-
* const fn = () => console.log('bar');
|
|
2427
|
-
* emitter.on('foo', fn);
|
|
2428
|
-
* emitter.on('foo', () => console.log('baz'));
|
|
2429
|
-
* emitter.on('foo', () => console.log(42));
|
|
2430
|
-
*
|
|
2431
|
-
* emitter.emit('foo');
|
|
2432
|
-
* // bar
|
|
2433
|
-
* // baz
|
|
2434
|
-
* // 42
|
|
2435
|
-
*
|
|
2436
|
-
* emitter.off('foo', fn); // 取消 foo 的监听方法 fn
|
|
2437
|
-
*
|
|
2438
|
-
* emitter.emit('foo');
|
|
2439
|
-
* // bar
|
|
2440
|
-
* // 42
|
|
2441
|
-
*
|
|
2442
|
-
* emitter.off('foo'); // 取消 foo 的全部监听方法
|
|
2443
|
-
* emitter.emit('foo'); // 什么都没发生
|
|
2444
|
-
*/
|
|
2445
2271
|
EmitterPro.prototype.off = function (eventName, listener) {
|
|
2446
2272
|
var handler = this.handlers[eventName];
|
|
2447
2273
|
if (handler) {
|
|
@@ -2457,63 +2283,10 @@
|
|
|
2457
2283
|
}
|
|
2458
2284
|
return this;
|
|
2459
2285
|
};
|
|
2460
|
-
/**
|
|
2461
|
-
* 取消全部事件名称的监听方法。
|
|
2462
|
-
*
|
|
2463
|
-
* @returns 事件触发器实例。
|
|
2464
|
-
* @example
|
|
2465
|
-
* const fn = () => console.log('bar');
|
|
2466
|
-
* emitter.on('test', fn);
|
|
2467
|
-
* emitter.on('test', () => console.log('baz'));
|
|
2468
|
-
* emitter.on('test', () => console.log(42));
|
|
2469
|
-
*
|
|
2470
|
-
* emitter.on('other', fn);
|
|
2471
|
-
* emitter.on('other', () => console.log('baz'));
|
|
2472
|
-
*
|
|
2473
|
-
* emitter.emit('test');
|
|
2474
|
-
* // bar
|
|
2475
|
-
* // baz
|
|
2476
|
-
* // 42
|
|
2477
|
-
*
|
|
2478
|
-
* emitter.emit('other');
|
|
2479
|
-
* // bar
|
|
2480
|
-
* // baz
|
|
2481
|
-
*
|
|
2482
|
-
* emitter.offAll(); // 取消全部监听方法
|
|
2483
|
-
*
|
|
2484
|
-
* emitter.emit('test'); // 什么都没发生
|
|
2485
|
-
* emitter.emit('other'); // 什么都没发生
|
|
2486
|
-
*/
|
|
2487
2286
|
EmitterPro.prototype.offAll = function () {
|
|
2488
2287
|
this.handlers = {};
|
|
2489
2288
|
return this;
|
|
2490
2289
|
};
|
|
2491
|
-
/**
|
|
2492
|
-
* 触发监听方法。
|
|
2493
|
-
*
|
|
2494
|
-
* @param eventName 事件名称
|
|
2495
|
-
* @param args 触发监听方法的参数(从第二个参数开始都将传给监听方法)
|
|
2496
|
-
* @returns 如果触发成功返回 `true`,否则返回 `false`。
|
|
2497
|
-
* @example
|
|
2498
|
-
* emitter.on('foo', () => console.log('bar'));
|
|
2499
|
-
* emitter.on('foo', () => console.log(42));
|
|
2500
|
-
*
|
|
2501
|
-
* emitter.emit('foo');
|
|
2502
|
-
* // bar
|
|
2503
|
-
* // 42
|
|
2504
|
-
*
|
|
2505
|
-
* // 支持传入参数
|
|
2506
|
-
* emitter.on('test' (a, b) => console.log(a + b));
|
|
2507
|
-
* emitter.on('test' (a, b) => console.log(a * b));
|
|
2508
|
-
*
|
|
2509
|
-
* emitter.emit('other', 2, 5);
|
|
2510
|
-
* // 7
|
|
2511
|
-
* // 10
|
|
2512
|
-
*
|
|
2513
|
-
* emitter.emit('other', 5, 5);
|
|
2514
|
-
* // 10
|
|
2515
|
-
* // 25
|
|
2516
|
-
*/
|
|
2517
2290
|
EmitterPro.prototype.emit = function (eventName) {
|
|
2518
2291
|
var args = [];
|
|
2519
2292
|
for (var _i = 1; _i < arguments.length; _i++) {
|
|
@@ -2531,37 +2304,9 @@
|
|
|
2531
2304
|
return EmitterPro;
|
|
2532
2305
|
}());
|
|
2533
2306
|
|
|
2534
|
-
var
|
|
2535
|
-
var
|
|
2536
|
-
|
|
2537
|
-
if (scope === void 0) { scope = 'default'; }
|
|
2538
|
-
this.scope = scope;
|
|
2539
|
-
if (!cache[this.scope]) {
|
|
2540
|
-
cache[this.scope] = {};
|
|
2541
|
-
}
|
|
2542
|
-
this.data = cache[this.scope];
|
|
2543
|
-
}
|
|
2544
|
-
MemoryStorage.prototype.getItem = function (key) {
|
|
2545
|
-
return key in this.data ? this.data[key] : null;
|
|
2546
|
-
};
|
|
2547
|
-
MemoryStorage.prototype.setItem = function (key, value) {
|
|
2548
|
-
this.data[key] = value;
|
|
2549
|
-
};
|
|
2550
|
-
MemoryStorage.prototype.removeItem = function (key) {
|
|
2551
|
-
delete this.data[key];
|
|
2552
|
-
};
|
|
2553
|
-
MemoryStorage.prototype.clear = function () {
|
|
2554
|
-
cache[this.scope] = {};
|
|
2555
|
-
this.data = cache[this.scope];
|
|
2556
|
-
};
|
|
2557
|
-
return MemoryStorage;
|
|
2558
|
-
}());
|
|
2559
|
-
|
|
2560
|
-
// 随机字符串
|
|
2561
|
-
function randomString() {
|
|
2562
|
-
return Math.random().toString(16).substring(2, 8);
|
|
2563
|
-
}
|
|
2564
|
-
// 是否支持 storage
|
|
2307
|
+
var pkgName = 'cache2';
|
|
2308
|
+
var CACHE_DEFAULT_PREFIX = "".concat(pkgName, "_");
|
|
2309
|
+
var CAHCE_DEFAULT_NAMESPACE = 'default';
|
|
2565
2310
|
function isStorageSupported(storage) {
|
|
2566
2311
|
try {
|
|
2567
2312
|
var isSupport = typeof storage === 'object' &&
|
|
@@ -2570,149 +2315,85 @@
|
|
|
2570
2315
|
!!storage.getItem &&
|
|
2571
2316
|
!!storage.removeItem;
|
|
2572
2317
|
if (isSupport) {
|
|
2573
|
-
var
|
|
2318
|
+
var randomStr = Math.random().toString(16).substring(2, 8);
|
|
2319
|
+
var key = "___".concat(pkgName, "_test_").concat(randomStr, "_").concat(Date.now(), "___");
|
|
2574
2320
|
var value = '1';
|
|
2575
2321
|
storage.setItem(key, value);
|
|
2576
|
-
if (storage.getItem(key) !== value) {
|
|
2577
|
-
return false;
|
|
2578
|
-
}
|
|
2579
2322
|
storage.removeItem(key);
|
|
2580
2323
|
}
|
|
2581
2324
|
return isSupport;
|
|
2582
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2583
2325
|
}
|
|
2584
2326
|
catch (err) {
|
|
2585
|
-
console.error("[
|
|
2327
|
+
console.error("[".concat(pkgName, "] Storage operation failed:"), err instanceof Error ? err.message : err);
|
|
2328
|
+
console.warn("[".concat(pkgName, "] The default memory cache will be used."));
|
|
2586
2329
|
return false;
|
|
2587
2330
|
}
|
|
2588
2331
|
}
|
|
2589
2332
|
function parse(value, reviver) {
|
|
2590
2333
|
try {
|
|
2591
2334
|
return JSON.parse(value, reviver);
|
|
2592
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2593
2335
|
}
|
|
2594
2336
|
catch (err) {
|
|
2337
|
+
console.warn("[".concat(pkgName, "] JSON parse failed, returning original value:"), err instanceof Error ? err.message : err);
|
|
2595
2338
|
return value;
|
|
2596
2339
|
}
|
|
2597
2340
|
}
|
|
2598
2341
|
function stringify(value, replacer) {
|
|
2599
|
-
|
|
2600
|
-
|
|
2601
|
-
|
|
2602
|
-
|
|
2603
|
-
|
|
2604
|
-
|
|
2605
|
-
|
|
2606
|
-
|
|
2607
|
-
|
|
2608
|
-
|
|
2609
|
-
|
|
2610
|
-
|
|
2611
|
-
|
|
2612
|
-
|
|
2613
|
-
|
|
2614
|
-
|
|
2615
|
-
|
|
2616
|
-
|
|
2617
|
-
|
|
2618
|
-
|
|
2619
|
-
|
|
2620
|
-
|
|
2621
|
-
|
|
2622
|
-
|
|
2623
|
-
|
|
2624
|
-
|
|
2625
|
-
|
|
2626
|
-
|
|
2627
|
-
|
|
2628
|
-
|
|
2629
|
-
|
|
2630
|
-
|
|
2631
|
-
|
|
2632
|
-
|
|
2633
|
-
|
|
2634
|
-
|
|
2635
|
-
*/
|
|
2636
|
-
var Storage = /** @class */ (function () {
|
|
2342
|
+
try {
|
|
2343
|
+
return JSON.stringify(value, replacer);
|
|
2344
|
+
}
|
|
2345
|
+
catch (err) {
|
|
2346
|
+
console.error("[".concat(pkgName, "] JSON stringify failed:"), err instanceof Error ? err.message : err);
|
|
2347
|
+
throw err;
|
|
2348
|
+
}
|
|
2349
|
+
}
|
|
2350
|
+
|
|
2351
|
+
var cache = {};
|
|
2352
|
+
var MemoryStorage = (function () {
|
|
2353
|
+
function MemoryStorage(scope) {
|
|
2354
|
+
if (scope === void 0) { scope = CAHCE_DEFAULT_NAMESPACE; }
|
|
2355
|
+
this.scope = scope;
|
|
2356
|
+
if (!cache[this.scope]) {
|
|
2357
|
+
cache[this.scope] = {};
|
|
2358
|
+
}
|
|
2359
|
+
this.data = cache[this.scope];
|
|
2360
|
+
}
|
|
2361
|
+
MemoryStorage.prototype.getItem = function (key) {
|
|
2362
|
+
return key in this.data ? this.data[key] : null;
|
|
2363
|
+
};
|
|
2364
|
+
MemoryStorage.prototype.setItem = function (key, value) {
|
|
2365
|
+
this.data[key] = value;
|
|
2366
|
+
};
|
|
2367
|
+
MemoryStorage.prototype.removeItem = function (key) {
|
|
2368
|
+
delete this.data[key];
|
|
2369
|
+
};
|
|
2370
|
+
MemoryStorage.prototype.clear = function () {
|
|
2371
|
+
cache[this.scope] = {};
|
|
2372
|
+
this.data = cache[this.scope];
|
|
2373
|
+
};
|
|
2374
|
+
return MemoryStorage;
|
|
2375
|
+
}());
|
|
2376
|
+
|
|
2377
|
+
var Storage = (function () {
|
|
2637
2378
|
function Storage(storage, options) {
|
|
2638
2379
|
if (options === void 0) { options = {}; }
|
|
2639
2380
|
var isSupported = storage ? isStorageSupported(storage) : false;
|
|
2640
2381
|
this.options = __assign({ needParsed: isSupported, prefix: '' }, options);
|
|
2641
2382
|
this.storage = isSupported ? storage : new MemoryStorage(this.options.memoryScope);
|
|
2642
2383
|
}
|
|
2643
|
-
/**
|
|
2644
|
-
* 内部用于获取存储的键名称。
|
|
2645
|
-
*
|
|
2646
|
-
* 如果实例有设置 `prefix`,返回 `prefix + key`。
|
|
2647
|
-
*
|
|
2648
|
-
* @protected
|
|
2649
|
-
* @param key 原键名称
|
|
2650
|
-
* @returns 存储的键名称
|
|
2651
|
-
*/
|
|
2652
2384
|
Storage.prototype.getKey = function (key) {
|
|
2653
2385
|
return this.options.prefix + key;
|
|
2654
2386
|
};
|
|
2655
|
-
/**
|
|
2656
|
-
* 获取存储的数据。
|
|
2657
|
-
*
|
|
2658
|
-
* @param {string} key 键名称。
|
|
2659
|
-
* @returns 如果键值存在返回键值,否则返回 `null`。
|
|
2660
|
-
* @example
|
|
2661
|
-
* const local = new Storage(window.localStorage);
|
|
2662
|
-
* local.set('foo', { baz: 42 });
|
|
2663
|
-
* local.get('foo');
|
|
2664
|
-
* // { baz: 42 }
|
|
2665
|
-
*/
|
|
2666
2387
|
Storage.prototype.get = function (key) {
|
|
2667
2388
|
var value = this.storage.getItem(this.getKey(key));
|
|
2668
2389
|
return this.options.needParsed ? parse(value, this.options.reviver) : value;
|
|
2669
2390
|
};
|
|
2670
|
-
/**
|
|
2671
|
-
* 存储数据。
|
|
2672
|
-
*
|
|
2673
|
-
* @param key 键名称。
|
|
2674
|
-
* @param value 键值。
|
|
2675
|
-
* @example
|
|
2676
|
-
* const local = new Storage(window.localStorage);
|
|
2677
|
-
* local.set('foo', { baz: 42 });
|
|
2678
|
-
* local.get('foo');
|
|
2679
|
-
* // { baz: 42 }
|
|
2680
|
-
*/
|
|
2681
2391
|
Storage.prototype.set = function (key, value) {
|
|
2682
2392
|
this.storage.setItem(this.getKey(key), this.options.needParsed ? stringify(value, this.options.replacer) : value);
|
|
2683
2393
|
};
|
|
2684
|
-
/**
|
|
2685
|
-
* 删除存储的数据。
|
|
2686
|
-
*
|
|
2687
|
-
* @param key 键名称。
|
|
2688
|
-
* @example
|
|
2689
|
-
* const local = new Storage(window.localStorage);
|
|
2690
|
-
* local.set('foo', { baz: 42 });
|
|
2691
|
-
* local.get('foo');
|
|
2692
|
-
* // { baz: 42 }
|
|
2693
|
-
*
|
|
2694
|
-
* local.del('foo');
|
|
2695
|
-
* local.get('foo');
|
|
2696
|
-
* // null
|
|
2697
|
-
*/
|
|
2698
2394
|
Storage.prototype.del = function (key) {
|
|
2699
2395
|
this.storage.removeItem(this.getKey(key));
|
|
2700
2396
|
};
|
|
2701
|
-
/**
|
|
2702
|
-
* 清除存储的所有键。
|
|
2703
|
-
*
|
|
2704
|
-
* 注意:该方法调用 `storage.clear()`,可能会将同域下的不同实例的所有键都清除。如果要避免这种情况,建议使用 `import { Cache } 'cache2'`。
|
|
2705
|
-
*
|
|
2706
|
-
* @example
|
|
2707
|
-
* const local = new Storage(window.localStorage);
|
|
2708
|
-
* local.set('foo', { baz: 42 });
|
|
2709
|
-
* local.get('foo');
|
|
2710
|
-
* // { baz: 42 }
|
|
2711
|
-
*
|
|
2712
|
-
* local.clear();
|
|
2713
|
-
* local.get('foo');
|
|
2714
|
-
* // null
|
|
2715
|
-
*/
|
|
2716
2397
|
Storage.prototype.clear = function () {
|
|
2717
2398
|
if (typeof this.storage.clear === 'function') {
|
|
2718
2399
|
this.storage.clear();
|
|
@@ -2721,55 +2402,13 @@
|
|
|
2721
2402
|
return Storage;
|
|
2722
2403
|
}());
|
|
2723
2404
|
|
|
2724
|
-
|
|
2725
|
-
var defaultPrefix = 'cache2_';
|
|
2726
|
-
var defaultNamespace = 'default';
|
|
2727
|
-
/**
|
|
2728
|
-
* 功能丰富的数据存储管理,支持 `自定义缓存` `命名空间` `数据过期时间` `限制缓存数量` `自定义事件`。
|
|
2729
|
-
*
|
|
2730
|
-
* 注意:如果你需要的是简单的基本数据存储管理,例如浏览器存储,建议使用 `import { Storage } from 'cache2'`。
|
|
2731
|
-
*
|
|
2732
|
-
* @class
|
|
2733
|
-
* @param {string} [namespace] 命名空间。可选。
|
|
2734
|
-
* @param {Object} [options] 配置项。可选。
|
|
2735
|
-
* @param {Object} [options.storage] 自定义缓存对象要包含 `getItem` `setItem` `removeItem` 方法。默认使用内置的内存缓存。
|
|
2736
|
-
* @param {number} [options.max=-1] 最大缓存数据数量。`-1` 表示无限制。默认 `-1`。
|
|
2737
|
-
* @param {'limited' | 'replaced'} [options.maxStrategy='limited'] 当达到最大缓存数量限制时的缓存策略。`limited` 表示达到限制数量后不存入数据,保存时返回 `false`。`replaced` 表示优先替换快过期的数据,如果都是一样的过期时间(0),按照先入先出规则处理,保存时始终返回 `true`。默认 `limited`。
|
|
2738
|
-
* @param {number} [options.stdTTL=0] 相对当前时间的数据存活时间,应用于当前实例的所有缓存数据。单位为毫秒,`0` 表示无期限。默认 `0`。
|
|
2739
|
-
* @param {number} [options.checkperiod=0] 定时检查过期数据,单位毫秒。如果小于等于 `0` 表示不启动定时器检查。默认 `0`。
|
|
2740
|
-
* @param {boolean} [options.needParsed] 存取数据时是否需要序列化和解析数据。如果使用内置的内存缓存,默认 `false`,如果自定义 `storage` 默认 `true`。
|
|
2741
|
-
* @param {Function} [options.replacer] 数据存储时序列化的参数,透传给 [JSON.stringify](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) 的 `replacer` 参数。仅在 `needParsed=true` 时生效。
|
|
2742
|
-
* @param {Function} [options.reviver] 数据获取时转换的参数,透传给 [JSON.parse](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse) 的 `reviver` 参数。仅在 `needParsed=true` 时生效。
|
|
2743
|
-
* @param {string} [options.prefix] 缓存键前缀。
|
|
2744
|
-
* @example
|
|
2745
|
-
* // 自定义过期时间
|
|
2746
|
-
* const memoryCache = new Cache({ stdTTL: 60 * 1000 });
|
|
2747
|
-
* memoryCache.set('foo', { baz: 42 });
|
|
2748
|
-
* memoryCache.get('foo');
|
|
2749
|
-
* // { baz: 42 }
|
|
2750
|
-
*
|
|
2751
|
-
* // 60 seconds later
|
|
2752
|
-
*
|
|
2753
|
-
* memoryCache.get('foo');
|
|
2754
|
-
* // undefined
|
|
2755
|
-
*
|
|
2756
|
-
* // 命名空间、自定义缓存
|
|
2757
|
-
* const localCache = new Cache('namespace', { storage: window.localStorage });
|
|
2758
|
-
* localCache.set('foo', { baz: 42 });
|
|
2759
|
-
* localCache.get('foo');
|
|
2760
|
-
* // { baz: 42 }
|
|
2761
|
-
*
|
|
2762
|
-
* localCache.del('foo');
|
|
2763
|
-
* localCache.get('foo');
|
|
2764
|
-
* // undefined
|
|
2765
|
-
*/
|
|
2766
|
-
var Cache = /** @class */ (function (_super) {
|
|
2405
|
+
var Cache = (function (_super) {
|
|
2767
2406
|
__extends(Cache, _super);
|
|
2768
2407
|
function Cache(namespace, options) {
|
|
2769
2408
|
var _this = _super.call(this) || this;
|
|
2770
|
-
var ns =
|
|
2409
|
+
var ns = CAHCE_DEFAULT_NAMESPACE, opts;
|
|
2771
2410
|
if (typeof namespace === 'string') {
|
|
2772
|
-
ns = namespace ||
|
|
2411
|
+
ns = namespace || CAHCE_DEFAULT_NAMESPACE;
|
|
2773
2412
|
}
|
|
2774
2413
|
else if (typeof namespace === 'object') {
|
|
2775
2414
|
opts = namespace;
|
|
@@ -2777,19 +2416,12 @@
|
|
|
2777
2416
|
if (!opts && typeof options === 'object') {
|
|
2778
2417
|
opts = options;
|
|
2779
2418
|
}
|
|
2780
|
-
_this.options = __assign({ max: -1, stdTTL: 0, maxStrategy: 'limited', checkperiod: 0, prefix:
|
|
2419
|
+
_this.options = __assign({ max: -1, stdTTL: 0, maxStrategy: 'limited', checkperiod: 0, prefix: CACHE_DEFAULT_PREFIX }, opts);
|
|
2781
2420
|
_this.storage = new Storage(_this.options.storage, __assign({ memoryScope: ns }, _this.options));
|
|
2782
2421
|
_this.cacheKey = ns;
|
|
2783
2422
|
_this.startCheckperiod();
|
|
2784
2423
|
return _this;
|
|
2785
2424
|
}
|
|
2786
|
-
/**
|
|
2787
|
-
* 检查当前键值是否过期,如果过期将会自动删除。
|
|
2788
|
-
*
|
|
2789
|
-
* @param key 键名称。
|
|
2790
|
-
* @param data 缓存数据。
|
|
2791
|
-
* @returns 如果键值已过期返回 `false` ,否则返回 `true`。
|
|
2792
|
-
*/
|
|
2793
2425
|
Cache.prototype._check = function (key, data) {
|
|
2794
2426
|
var ret = true;
|
|
2795
2427
|
if (data.t !== 0 && data.t < Date.now()) {
|
|
@@ -2815,65 +2447,33 @@
|
|
|
2815
2447
|
Cache.prototype._getReplaceKey = function (keys, cacheValues) {
|
|
2816
2448
|
var retkey = keys[0];
|
|
2817
2449
|
keys.forEach(function (key) {
|
|
2818
|
-
if (cacheValues[
|
|
2819
|
-
(cacheValues[key].t === cacheValues[retkey].t && cacheValues[key].n < cacheValues[retkey].n)) {
|
|
2450
|
+
if (cacheValues[retkey].n > cacheValues[key].n) {
|
|
2820
2451
|
retkey = key;
|
|
2821
2452
|
}
|
|
2822
2453
|
});
|
|
2823
2454
|
return retkey;
|
|
2824
2455
|
};
|
|
2825
2456
|
Object.defineProperty(Cache.prototype, "cacheValues", {
|
|
2826
|
-
/**
|
|
2827
|
-
* 获取全部缓存数据,不处理过期数据和排序
|
|
2828
|
-
*/
|
|
2829
2457
|
get: function () {
|
|
2830
2458
|
return this.storage.get(this.cacheKey) || {};
|
|
2831
2459
|
},
|
|
2832
2460
|
enumerable: false,
|
|
2833
2461
|
configurable: true
|
|
2834
2462
|
});
|
|
2835
|
-
// 设置缓存数据
|
|
2836
2463
|
Cache.prototype.setCacheValues = function (values) {
|
|
2837
2464
|
this.storage.set(this.cacheKey, values);
|
|
2838
2465
|
};
|
|
2839
|
-
|
|
2840
|
-
|
|
2841
|
-
*
|
|
2842
|
-
* @param {string} key 键名称。
|
|
2843
|
-
* @returns {*} 如果找到该值,则返回该值。如果未找到或已过期,则返回 `undefined`。
|
|
2844
|
-
* @example
|
|
2845
|
-
* myCache.set('myKey', obj, 5 * 60 * 1000);
|
|
2846
|
-
* myCache.get('myKey');
|
|
2847
|
-
* // { foo: 'bar', baz: 42 }
|
|
2848
|
-
*
|
|
2849
|
-
* myCache.get('myKey2');
|
|
2850
|
-
* // undefined
|
|
2851
|
-
*/
|
|
2852
|
-
Cache.prototype.get = function (key) {
|
|
2853
|
-
var data = this.cacheValues[key];
|
|
2466
|
+
Cache.prototype._getData = function (key, cacheValues) {
|
|
2467
|
+
var data = cacheValues[key];
|
|
2854
2468
|
if (data && this._check(key, data)) {
|
|
2855
|
-
return data
|
|
2469
|
+
return data;
|
|
2856
2470
|
}
|
|
2857
2471
|
return;
|
|
2858
2472
|
};
|
|
2859
|
-
|
|
2860
|
-
|
|
2861
|
-
|
|
2862
|
-
|
|
2863
|
-
* @returns {Object} 如果找到对应键名的值,返回一个具有键值对的对象。如果未找到或已过期,则返回一个空对象 `{}`。
|
|
2864
|
-
* @example
|
|
2865
|
-
* myCache.mset([
|
|
2866
|
-
* { key: 'myKey', value: { foo: 'bar', baz: 42 }, ttl: 5 * 60 * 1000 },
|
|
2867
|
-
* { key: 'myKey2', value: { a: 1, b: 2 } },
|
|
2868
|
-
* { key: 'myKey3', value: 'abc' }
|
|
2869
|
-
* ]);
|
|
2870
|
-
*
|
|
2871
|
-
* myCache.mget(['myKey', 'myKey2']);
|
|
2872
|
-
* // {
|
|
2873
|
-
* // myKey: { foo: 'bar', baz: 42 },
|
|
2874
|
-
* // myKey2: { a: 1, b: 2 }
|
|
2875
|
-
* // }
|
|
2876
|
-
*/
|
|
2473
|
+
Cache.prototype.get = function (key) {
|
|
2474
|
+
var data = this._getData(key, this.cacheValues);
|
|
2475
|
+
return data === null || data === void 0 ? void 0 : data.v;
|
|
2476
|
+
};
|
|
2877
2477
|
Cache.prototype.mget = function (keys) {
|
|
2878
2478
|
var _this = this;
|
|
2879
2479
|
var ret = {};
|
|
@@ -2882,65 +2482,31 @@
|
|
|
2882
2482
|
}
|
|
2883
2483
|
var cacheValues = this.cacheValues;
|
|
2884
2484
|
keys.forEach(function (key) {
|
|
2885
|
-
var data = cacheValues
|
|
2886
|
-
if (data
|
|
2485
|
+
var data = _this._getData(key, cacheValues);
|
|
2486
|
+
if (data) {
|
|
2887
2487
|
ret[key] = data.v;
|
|
2888
2488
|
}
|
|
2889
2489
|
});
|
|
2890
2490
|
return ret;
|
|
2891
2491
|
};
|
|
2892
|
-
/**
|
|
2893
|
-
* 获取全部缓存值。
|
|
2894
|
-
*
|
|
2895
|
-
* @returns {Object} 返回一个具有键值对的对象。
|
|
2896
|
-
* @example
|
|
2897
|
-
* myCache.mset([
|
|
2898
|
-
* { key: 'myKey', value: { foo: 'bar', baz: 42 }, ttl: 5 * 60 * 1000 },
|
|
2899
|
-
* { key: 'myKey2', value: { a: 1, b: 2 } },
|
|
2900
|
-
* { key: 'myKey3', value: 'abc' }
|
|
2901
|
-
* ]);
|
|
2902
|
-
*
|
|
2903
|
-
* myCache.getAll();
|
|
2904
|
-
* // {
|
|
2905
|
-
* // myKey: { foo: 'bar', baz: 42 },
|
|
2906
|
-
* // myKey2: { a: 1, b: 2 }
|
|
2907
|
-
* // myKey3: 'abc'
|
|
2908
|
-
* // }
|
|
2909
|
-
*/
|
|
2910
2492
|
Cache.prototype.getAll = function () {
|
|
2911
2493
|
var keys = Object.keys(this.cacheValues);
|
|
2912
2494
|
return this.mget(keys);
|
|
2913
2495
|
};
|
|
2914
|
-
/**
|
|
2915
|
-
* 设置缓存数据。
|
|
2916
|
-
*
|
|
2917
|
-
* 如果超出缓存数量,可能会设置失败。
|
|
2918
|
-
*
|
|
2919
|
-
* @param {string} key 键名称。
|
|
2920
|
-
* @param {*} value 键值。
|
|
2921
|
-
* @param {number} [ttl] 数据存活时间。单位毫秒 `ms`。
|
|
2922
|
-
* @returns {boolean} 如果设置成功返回 `true`,否则返回 `false`。
|
|
2923
|
-
* @example
|
|
2924
|
-
* myCache.set('myKey', { foo: 'bar', baz: 42 }, 5 * 60 * 1000);
|
|
2925
|
-
* // true
|
|
2926
|
-
*/
|
|
2927
2496
|
Cache.prototype.set = function (key, value, ttl) {
|
|
2928
2497
|
if (this.options.max === 0) {
|
|
2929
2498
|
return false;
|
|
2930
2499
|
}
|
|
2931
2500
|
var cacheValues = this.cacheValues;
|
|
2932
2501
|
var keys = Object.keys(cacheValues);
|
|
2933
|
-
// 当前不存在该键值,并且数据量超过最大限制
|
|
2934
2502
|
if (!cacheValues[key] && this._isLimited(keys.length)) {
|
|
2935
2503
|
var validKeys = this.keys();
|
|
2936
2504
|
if (this._isLimited(validKeys.length)) {
|
|
2937
|
-
// 如果最大限制策略是替换,将优先替换快过期的数据,如果都是一样的过期时间(0),按照先入先出规则处理。
|
|
2938
2505
|
if (this.options.maxStrategy === 'replaced') {
|
|
2939
2506
|
var replaceKey = this._getReplaceKey(validKeys, cacheValues);
|
|
2940
2507
|
this.del(replaceKey);
|
|
2941
2508
|
}
|
|
2942
2509
|
else {
|
|
2943
|
-
// 如果是最大限制策略是不允许添加,返回 false 。
|
|
2944
2510
|
return false;
|
|
2945
2511
|
}
|
|
2946
2512
|
}
|
|
@@ -2950,22 +2516,8 @@
|
|
|
2950
2516
|
this.emit('set', key, cacheValues[key].v);
|
|
2951
2517
|
return true;
|
|
2952
2518
|
};
|
|
2953
|
-
/**
|
|
2954
|
-
* 设置多个缓存数据。
|
|
2955
|
-
*
|
|
2956
|
-
* @param {Object[]} keyValueSet 多个键值对数据。
|
|
2957
|
-
* @returns {boolean} 如果全部设置成功返回 `true`,否则返回 `false`。
|
|
2958
|
-
* @example
|
|
2959
|
-
* myCache.mset([
|
|
2960
|
-
* { key: 'myKey', value: { foo: 'bar', baz: 42 }, ttl: 5 * 60 * 1000 },
|
|
2961
|
-
* { key: 'myKey2', value: { a: 1, b: 2 } },
|
|
2962
|
-
* { key: 'myKey3', value: 'abc' }
|
|
2963
|
-
* ]);
|
|
2964
|
-
* // true
|
|
2965
|
-
*/
|
|
2966
2519
|
Cache.prototype.mset = function (keyValueSet) {
|
|
2967
2520
|
var _this = this;
|
|
2968
|
-
// 该处不使用数组 some 方法,是因为不能某个失败,而导致其他就不在更新。
|
|
2969
2521
|
var ret = true;
|
|
2970
2522
|
keyValueSet.forEach(function (item) {
|
|
2971
2523
|
var itemSetResult = _this.set(item.key, item.value, item.ttl);
|
|
@@ -2975,23 +2527,6 @@
|
|
|
2975
2527
|
});
|
|
2976
2528
|
return ret;
|
|
2977
2529
|
};
|
|
2978
|
-
/**
|
|
2979
|
-
* 删除一个或多个键。
|
|
2980
|
-
*
|
|
2981
|
-
* @param {string|string[]} key 要删除的键名。
|
|
2982
|
-
* @returns {number} 返回已删除的数量。
|
|
2983
|
-
* @example
|
|
2984
|
-
* myCache.set('myKey', { foo: 'bar', baz: 42 });
|
|
2985
|
-
* myCache.del('myKey'); // 1
|
|
2986
|
-
* myCache.del('not found'); // 0
|
|
2987
|
-
*
|
|
2988
|
-
* myCache.mset([
|
|
2989
|
-
* { key: 'myKey', value: { foo: 'bar', baz: 42 }, ttl: 5 * 60 * 1000 },
|
|
2990
|
-
* { key: 'myKey2', value: { a: 1, b: 2 } },
|
|
2991
|
-
* { key: 'myKey3', value: 'abc' }
|
|
2992
|
-
* ]);
|
|
2993
|
-
* myCache.del(['myKey', 'myKey2']); // 2
|
|
2994
|
-
*/
|
|
2995
2530
|
Cache.prototype.del = function (key) {
|
|
2996
2531
|
var _this = this;
|
|
2997
2532
|
var cacheValues = this.cacheValues;
|
|
@@ -3010,163 +2545,44 @@
|
|
|
3010
2545
|
}
|
|
3011
2546
|
return count;
|
|
3012
2547
|
};
|
|
3013
|
-
/**
|
|
3014
|
-
* 清除全部缓存的数据。
|
|
3015
|
-
*
|
|
3016
|
-
* @example
|
|
3017
|
-
* myCache.set('bar', 1);
|
|
3018
|
-
* myCache.set('foo', 2);
|
|
3019
|
-
* myCache.keys(); // ['bar', 'foo']
|
|
3020
|
-
*
|
|
3021
|
-
* myCache.clear();
|
|
3022
|
-
* myCache.keys(); // []
|
|
3023
|
-
*/
|
|
3024
2548
|
Cache.prototype.clear = function () {
|
|
3025
2549
|
this.storage.del(this.cacheKey);
|
|
3026
2550
|
};
|
|
3027
|
-
/**
|
|
3028
|
-
* 获取全部键名的数组。
|
|
3029
|
-
*
|
|
3030
|
-
* @returns {string[]} 返回全部键名的数组。
|
|
3031
|
-
* @example
|
|
3032
|
-
* myCache.set('bar', 1);
|
|
3033
|
-
* myCache.set('foo', 2);
|
|
3034
|
-
*
|
|
3035
|
-
* myCache.keys(); // ['bar', 'foo']
|
|
3036
|
-
*/
|
|
3037
2551
|
Cache.prototype.keys = function () {
|
|
3038
2552
|
var _this = this;
|
|
3039
2553
|
var cacheValues = this.cacheValues;
|
|
3040
2554
|
var keys = Object.keys(cacheValues);
|
|
3041
2555
|
return keys.filter(function (key) { return _this._check(key, cacheValues[key]); });
|
|
3042
2556
|
};
|
|
3043
|
-
/**
|
|
3044
|
-
* 判断是否存在某个键。
|
|
3045
|
-
*
|
|
3046
|
-
* @param {string} key 键名称。
|
|
3047
|
-
* @returns {boolean} 如果包含该键返回 `true`,否则返回 `false`。
|
|
3048
|
-
* @example
|
|
3049
|
-
* myCache.has('foo'); // false
|
|
3050
|
-
*
|
|
3051
|
-
* myCache.set('foo', 1);
|
|
3052
|
-
* myCache.has('foo'); // true
|
|
3053
|
-
*/
|
|
3054
2557
|
Cache.prototype.has = function (key) {
|
|
3055
|
-
|
|
3056
|
-
return !!(data && this._check(key, data));
|
|
2558
|
+
return !!this._getData(key, this.cacheValues);
|
|
3057
2559
|
};
|
|
3058
|
-
/**
|
|
3059
|
-
* 获取缓存值并从缓存中删除键。
|
|
3060
|
-
*
|
|
3061
|
-
* @param {string} key 键名称。
|
|
3062
|
-
* @returns {*} 如果找到该值,则返回该值,并从缓存中删除该键。如果未找到或已过期,则返回 `undefined`。
|
|
3063
|
-
* @example
|
|
3064
|
-
* myCache.set('myKey', 'myValue');
|
|
3065
|
-
* myCache.has('myKey'); // true
|
|
3066
|
-
*
|
|
3067
|
-
* myCache.take('myKey'); // 'myValue'
|
|
3068
|
-
* myCache.has('myKey'); // false
|
|
3069
|
-
*/
|
|
3070
2560
|
Cache.prototype.take = function (key) {
|
|
3071
2561
|
var ret;
|
|
3072
|
-
var data = this.cacheValues
|
|
3073
|
-
if (data
|
|
2562
|
+
var data = this._getData(key, this.cacheValues);
|
|
2563
|
+
if (data) {
|
|
3074
2564
|
ret = data.v;
|
|
3075
2565
|
this.del(key);
|
|
3076
2566
|
}
|
|
3077
2567
|
return ret;
|
|
3078
2568
|
};
|
|
3079
|
-
/**
|
|
3080
|
-
* 更新缓存键值的数据存活时间。
|
|
3081
|
-
*
|
|
3082
|
-
* @param {string} key 键名称。
|
|
3083
|
-
* @param {number} ttl 数据存活时间。
|
|
3084
|
-
* @returns {boolean} 如果找到并更新成功,则返回 `true`,否则返回 `false`。
|
|
3085
|
-
* @example
|
|
3086
|
-
* myCache.set('myKey', { foo: 'bar', baz: 42 }, 5 * 60 * 1000);
|
|
3087
|
-
* myCache.ttl('myKey', 60 * 1000);
|
|
3088
|
-
* // true
|
|
3089
|
-
*
|
|
3090
|
-
* myCache.ttl('not found', 1000);
|
|
3091
|
-
* // false
|
|
3092
|
-
*/
|
|
3093
2569
|
Cache.prototype.ttl = function (key, ttl) {
|
|
3094
|
-
var
|
|
3095
|
-
|
|
3096
|
-
|
|
3097
|
-
cacheValues[key] = this._wrap(data.v, ttl);
|
|
3098
|
-
return true;
|
|
2570
|
+
var data = this._getData(key, this.cacheValues);
|
|
2571
|
+
if (data) {
|
|
2572
|
+
return this.set(key, data.v, ttl);
|
|
3099
2573
|
}
|
|
3100
2574
|
return false;
|
|
3101
2575
|
};
|
|
3102
|
-
/**
|
|
3103
|
-
* 获取某个键的过期时间戳。
|
|
3104
|
-
*
|
|
3105
|
-
* @param {string} key 键名称。
|
|
3106
|
-
* @returns {number | undefined} 如果未找到键或已过期,返回 `undefined`。如果 `ttl` 为 `0`,返回 `0`,否则返回一个以毫秒为单位的时间戳,表示键值将过期的时间。
|
|
3107
|
-
* @example
|
|
3108
|
-
* const myCache = new Cache({ stdTTL: 5 * 1000 });
|
|
3109
|
-
*
|
|
3110
|
-
* // 假如 Date.now() = 1673330000000
|
|
3111
|
-
* myCache.set('ttlKey', 'expireData');
|
|
3112
|
-
* myCache.set('noTtlKey', 'nonExpireData', 0);
|
|
3113
|
-
*
|
|
3114
|
-
* myCache.getTtl('ttlKey'); // 1673330005000
|
|
3115
|
-
* myCache.getTtl('noTtlKey'); // 0
|
|
3116
|
-
* myCache.getTtl('unknownKey'); // undefined
|
|
3117
|
-
*/
|
|
3118
2576
|
Cache.prototype.getTtl = function (key) {
|
|
3119
|
-
var
|
|
3120
|
-
|
|
3121
|
-
if (data && this._check(key, data)) {
|
|
3122
|
-
return cacheValues[key].t;
|
|
3123
|
-
}
|
|
3124
|
-
return;
|
|
2577
|
+
var data = this._getData(key, this.cacheValues);
|
|
2578
|
+
return data === null || data === void 0 ? void 0 : data.t;
|
|
3125
2579
|
};
|
|
3126
|
-
/**
|
|
3127
|
-
* 获取某个键值的最后修改时间。
|
|
3128
|
-
*
|
|
3129
|
-
* @param {string} key 键名称。
|
|
3130
|
-
* @returns {number | undefined} 如果未找到键或已过期,返回 `undefined`,否则返回一个以毫秒时间戳,表示键值最后修改时间。
|
|
3131
|
-
* @example
|
|
3132
|
-
* const myCache = new Cache();
|
|
3133
|
-
*
|
|
3134
|
-
* // 假如 Date.now() = 1673330000000
|
|
3135
|
-
* myCache.set('myKey', 'foo');
|
|
3136
|
-
* myCache.getLastModified('myKey'); // 1673330000000
|
|
3137
|
-
*
|
|
3138
|
-
* // 5000ms later
|
|
3139
|
-
* myCache.set('myKey', 'bar');
|
|
3140
|
-
* myCache.getLastModified('myKey'); // 1673330005000
|
|
3141
|
-
*/
|
|
3142
2580
|
Cache.prototype.getLastModified = function (key) {
|
|
3143
|
-
var
|
|
3144
|
-
|
|
3145
|
-
if (data && this._check(key, data)) {
|
|
3146
|
-
return cacheValues[key].n;
|
|
3147
|
-
}
|
|
3148
|
-
return;
|
|
2581
|
+
var data = this._getData(key, this.cacheValues);
|
|
2582
|
+
return data === null || data === void 0 ? void 0 : data.n;
|
|
3149
2583
|
};
|
|
3150
|
-
/**
|
|
3151
|
-
* 启动定时校验过期数据。
|
|
3152
|
-
*
|
|
3153
|
-
* 注意,如果没有设置 `checkperiod` 将不会触发定时器。
|
|
3154
|
-
*
|
|
3155
|
-
* @example
|
|
3156
|
-
* // 设置 checkperiod 之后自动生效
|
|
3157
|
-
* const myCache = new Cache({
|
|
3158
|
-
* checkperiod: 10 * 60 * 1000 // 10分钟检查一次数据是否过期
|
|
3159
|
-
* });
|
|
3160
|
-
*
|
|
3161
|
-
* // 停止定时校验过期数据
|
|
3162
|
-
* myCache.stopCheckperiod();
|
|
3163
|
-
*
|
|
3164
|
-
* // 启动定时校验过期数据
|
|
3165
|
-
* myCache.startCheckperiod();
|
|
3166
|
-
*/
|
|
3167
2584
|
Cache.prototype.startCheckperiod = function () {
|
|
3168
2585
|
var _this = this;
|
|
3169
|
-
// 触发全部缓存数据是否过期校验
|
|
3170
2586
|
this.keys();
|
|
3171
2587
|
if (this.options.checkperiod > 0) {
|
|
3172
2588
|
clearTimeout(this._checkTimeout);
|
|
@@ -3175,18 +2591,6 @@
|
|
|
3175
2591
|
}, this.options.checkperiod);
|
|
3176
2592
|
}
|
|
3177
2593
|
};
|
|
3178
|
-
/**
|
|
3179
|
-
* 停止定时校验过期数据。
|
|
3180
|
-
*
|
|
3181
|
-
* @example
|
|
3182
|
-
* // 设置 checkperiod 之后自动生效
|
|
3183
|
-
* const myCache = new Cache({
|
|
3184
|
-
* checkperiod: 10 * 60 * 1000 // 10分钟检查一次数据是否过期
|
|
3185
|
-
* });
|
|
3186
|
-
*
|
|
3187
|
-
* // 停止定时校验过期数据
|
|
3188
|
-
* myCache.stopCheckperiod();
|
|
3189
|
-
*/
|
|
3190
2594
|
Cache.prototype.stopCheckperiod = function () {
|
|
3191
2595
|
clearTimeout(this._checkTimeout);
|
|
3192
2596
|
};
|
|
@@ -3228,7 +2632,61 @@
|
|
|
3228
2632
|
return AsyncMemo;
|
|
3229
2633
|
}());
|
|
3230
2634
|
|
|
2635
|
+
var ConcurrencyController = (function () {
|
|
2636
|
+
function ConcurrencyController(maxConcurrency) {
|
|
2637
|
+
if (maxConcurrency === void 0) { maxConcurrency = 2; }
|
|
2638
|
+
this.maxConcurrency = Math.max(maxConcurrency, 1);
|
|
2639
|
+
this.runningCount = 0;
|
|
2640
|
+
this.queue = [];
|
|
2641
|
+
this.isPaused = false;
|
|
2642
|
+
}
|
|
2643
|
+
ConcurrencyController.prototype.add = function (task) {
|
|
2644
|
+
var _this = this;
|
|
2645
|
+
return new Promise(function (resolve, reject) {
|
|
2646
|
+
_this.queue.push({ task: task, resolve: resolve, reject: reject });
|
|
2647
|
+
_this._run();
|
|
2648
|
+
});
|
|
2649
|
+
};
|
|
2650
|
+
ConcurrencyController.prototype._run = function () {
|
|
2651
|
+
var _this = this;
|
|
2652
|
+
if (this.isPaused)
|
|
2653
|
+
return;
|
|
2654
|
+
while (this.runningCount < this.maxConcurrency && this.queue.length > 0) {
|
|
2655
|
+
var _a = this.queue.shift(), task = _a.task, resolve = _a.resolve, reject = _a.reject;
|
|
2656
|
+
this.runningCount++;
|
|
2657
|
+
task()
|
|
2658
|
+
.then(resolve)
|
|
2659
|
+
.catch(reject)
|
|
2660
|
+
.finally(function () {
|
|
2661
|
+
_this.runningCount--;
|
|
2662
|
+
_this._run();
|
|
2663
|
+
});
|
|
2664
|
+
}
|
|
2665
|
+
};
|
|
2666
|
+
ConcurrencyController.prototype.getStatus = function () {
|
|
2667
|
+
return {
|
|
2668
|
+
running: this.runningCount,
|
|
2669
|
+
waiting: this.queue.length,
|
|
2670
|
+
maxConcurrency: this.maxConcurrency,
|
|
2671
|
+
paused: this.isPaused
|
|
2672
|
+
};
|
|
2673
|
+
};
|
|
2674
|
+
ConcurrencyController.prototype.setMaxConcurrency = function (maxConcurrency) {
|
|
2675
|
+
this.maxConcurrency = Math.max(maxConcurrency, 1);
|
|
2676
|
+
this._run();
|
|
2677
|
+
};
|
|
2678
|
+
ConcurrencyController.prototype.pause = function () {
|
|
2679
|
+
this.isPaused = true;
|
|
2680
|
+
};
|
|
2681
|
+
ConcurrencyController.prototype.resume = function () {
|
|
2682
|
+
this.isPaused = false;
|
|
2683
|
+
this._run();
|
|
2684
|
+
};
|
|
2685
|
+
return ConcurrencyController;
|
|
2686
|
+
}());
|
|
2687
|
+
|
|
3231
2688
|
exports.AsyncMemo = AsyncMemo;
|
|
2689
|
+
exports.ConcurrencyController = ConcurrencyController;
|
|
3232
2690
|
exports.VERSION = VERSION;
|
|
3233
2691
|
exports.ajax = ajax;
|
|
3234
2692
|
exports.bytesToSize = bytesToSize;
|
|
@@ -3286,7 +2744,7 @@
|
|
|
3286
2744
|
exports.padZero = padZero;
|
|
3287
2745
|
exports.parseIdCard = parseIdCard;
|
|
3288
2746
|
exports.plus = plus;
|
|
3289
|
-
exports.randomString = randomString
|
|
2747
|
+
exports.randomString = randomString;
|
|
3290
2748
|
exports.replaceChar = replaceChar;
|
|
3291
2749
|
exports.round = _round;
|
|
3292
2750
|
exports.safeDate = safeDate;
|