@ztimson/utils 0.24.7 → 0.24.9
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/cache.d.ts +18 -7
- package/dist/index.cjs +44 -21
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +44 -21
- package/dist/index.mjs.map +1 -1
- package/dist/path-events.d.ts +10 -7
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -422,7 +422,7 @@ class Cache {
|
|
|
422
422
|
return new Proxy(this, {
|
|
423
423
|
get: (target, prop) => {
|
|
424
424
|
if (prop in target) return target[prop];
|
|
425
|
-
return
|
|
425
|
+
return this.get(prop, true);
|
|
426
426
|
},
|
|
427
427
|
set: (target, prop, value) => {
|
|
428
428
|
if (prop in target) target[prop] = value;
|
|
@@ -435,12 +435,16 @@ class Cache {
|
|
|
435
435
|
if (!this.key) throw new Error("No key defined");
|
|
436
436
|
return value[this.key];
|
|
437
437
|
}
|
|
438
|
+
save() {
|
|
439
|
+
if (this.options.storageKey && this.options.storage)
|
|
440
|
+
this.options.storage.setItem(this.options.storageKey, JSON.stringify(this.store));
|
|
441
|
+
}
|
|
438
442
|
/**
|
|
439
443
|
* Get all cached items
|
|
440
444
|
* @return {T[]} Array of items
|
|
441
445
|
*/
|
|
442
|
-
all() {
|
|
443
|
-
return deepCopy(Object.values(this.store));
|
|
446
|
+
all(expired) {
|
|
447
|
+
return deepCopy(Object.values(this.store).filter((v) => expired || !v._expired));
|
|
444
448
|
}
|
|
445
449
|
/**
|
|
446
450
|
* Add a new item to the cache. Like set, but finds key automatically
|
|
@@ -469,6 +473,7 @@ class Cache {
|
|
|
469
473
|
* Remove all keys from cache
|
|
470
474
|
*/
|
|
471
475
|
clear() {
|
|
476
|
+
this.complete = false;
|
|
472
477
|
this.store = {};
|
|
473
478
|
}
|
|
474
479
|
/**
|
|
@@ -477,37 +482,51 @@ class Cache {
|
|
|
477
482
|
*/
|
|
478
483
|
delete(key) {
|
|
479
484
|
delete this.store[key];
|
|
480
|
-
|
|
481
|
-
this.options.storage.setItem(this.options.storageKey, JSON.stringify(this.store));
|
|
485
|
+
this.save();
|
|
482
486
|
}
|
|
483
487
|
/**
|
|
484
488
|
* Return cache as an array of key-value pairs
|
|
485
489
|
* @return {[K, T][]} Key-value pairs array
|
|
486
490
|
*/
|
|
487
|
-
entries() {
|
|
488
|
-
return Object.entries(this.store);
|
|
491
|
+
entries(expired) {
|
|
492
|
+
return deepCopy(Object.entries(this.store).filter((v) => expired || !v._expired));
|
|
493
|
+
}
|
|
494
|
+
/**
|
|
495
|
+
* Manually expire a cached item
|
|
496
|
+
* @param {K} key Key to expire
|
|
497
|
+
*/
|
|
498
|
+
expire(key) {
|
|
499
|
+
this.complete = false;
|
|
500
|
+
if (this.options.expiryPolicy == "keep") this.store[key]._expired = true;
|
|
501
|
+
else this.delete(key);
|
|
489
502
|
}
|
|
490
503
|
/**
|
|
491
504
|
* Get item from the cache
|
|
492
505
|
* @param {K} key Key to lookup
|
|
493
506
|
* @return {T} Cached item
|
|
494
507
|
*/
|
|
495
|
-
get(key) {
|
|
496
|
-
|
|
508
|
+
get(key, expired) {
|
|
509
|
+
const cached = deepCopy(this.store[key] ?? null);
|
|
510
|
+
if (expired || !cached._expired) return cached;
|
|
511
|
+
return null;
|
|
497
512
|
}
|
|
498
513
|
/**
|
|
499
514
|
* Get a list of cached keys
|
|
500
515
|
* @return {K[]} Array of keys
|
|
501
516
|
*/
|
|
502
|
-
keys() {
|
|
503
|
-
return Object.keys(this.store);
|
|
517
|
+
keys(expired) {
|
|
518
|
+
return Object.keys(this.store).filter((k) => expired || !this.store[k]._expired);
|
|
504
519
|
}
|
|
505
520
|
/**
|
|
506
521
|
* Get map of cached items
|
|
507
522
|
* @return {Record<K, T>}
|
|
508
523
|
*/
|
|
509
|
-
map() {
|
|
510
|
-
|
|
524
|
+
map(expired) {
|
|
525
|
+
const copy = deepCopy(this.store);
|
|
526
|
+
if (!expired) Object.keys(copy).forEach((k) => {
|
|
527
|
+
if (copy[k]._expired) delete copy[k];
|
|
528
|
+
});
|
|
529
|
+
return copy;
|
|
511
530
|
}
|
|
512
531
|
/**
|
|
513
532
|
* Add an item to the cache manually specifying the key
|
|
@@ -517,12 +536,12 @@ class Cache {
|
|
|
517
536
|
* @return {this}
|
|
518
537
|
*/
|
|
519
538
|
set(key, value, ttl = this.options.ttl) {
|
|
539
|
+
if (this.options.expiryPolicy == "keep") delete this.store[key]._expired;
|
|
520
540
|
this.store[key] = value;
|
|
521
|
-
|
|
522
|
-
this.options.storage.setItem(this.options.storageKey, JSON.stringify(this.store));
|
|
541
|
+
this.save();
|
|
523
542
|
if (ttl) setTimeout(() => {
|
|
524
|
-
this.
|
|
525
|
-
this.
|
|
543
|
+
this.expire(key);
|
|
544
|
+
this.save();
|
|
526
545
|
}, ttl * 1e3);
|
|
527
546
|
return this;
|
|
528
547
|
}
|
|
@@ -1634,7 +1653,7 @@ class PathEvent {
|
|
|
1634
1653
|
return PathEvent.filter(target, this);
|
|
1635
1654
|
}
|
|
1636
1655
|
/**
|
|
1637
|
-
* Create
|
|
1656
|
+
* Create event string from its components
|
|
1638
1657
|
*
|
|
1639
1658
|
* @return {string} String representation of Event
|
|
1640
1659
|
*/
|
|
@@ -1643,18 +1662,22 @@ class PathEvent {
|
|
|
1643
1662
|
}
|
|
1644
1663
|
}
|
|
1645
1664
|
class PathEventEmitter {
|
|
1646
|
-
constructor() {
|
|
1665
|
+
constructor(prefix = "") {
|
|
1647
1666
|
__publicField(this, "listeners", []);
|
|
1667
|
+
this.prefix = prefix;
|
|
1648
1668
|
}
|
|
1649
1669
|
emit(event, ...args) {
|
|
1650
|
-
const parsed = new PathEvent(event);
|
|
1670
|
+
const parsed = new PathEvent(`${this.prefix}/${typeof event == "string" ? event : event.toString()}`);
|
|
1651
1671
|
this.listeners.filter((l) => PathEvent.has(l[0], event)).forEach(async (l) => l[1](parsed, ...args));
|
|
1652
1672
|
}
|
|
1653
1673
|
off(listener) {
|
|
1654
1674
|
this.listeners = this.listeners.filter((l) => l[1] != listener);
|
|
1655
1675
|
}
|
|
1656
1676
|
on(event, listener) {
|
|
1657
|
-
makeArray(event).forEach((e) => this.listeners.push([
|
|
1677
|
+
makeArray(event).forEach((e) => this.listeners.push([
|
|
1678
|
+
new PathEvent(`${this.prefix}/${typeof e == "string" ? event : event.toString()}`),
|
|
1679
|
+
listener
|
|
1680
|
+
]));
|
|
1658
1681
|
return () => this.off(listener);
|
|
1659
1682
|
}
|
|
1660
1683
|
once(event, listener) {
|