@ztimson/utils 0.25.21 → 0.25.23
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 +2 -0
- package/dist/index.cjs +23 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +23 -3
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -530,6 +530,8 @@ class Cache {
|
|
|
530
530
|
__publicField(this, "store", {});
|
|
531
531
|
/** Whether cache is complete */
|
|
532
532
|
__publicField(this, "complete", false);
|
|
533
|
+
/** Await initial loading */
|
|
534
|
+
__publicField(this, "loading");
|
|
533
535
|
/**
|
|
534
536
|
* Get all cached items
|
|
535
537
|
* @return {T[]} Array of items
|
|
@@ -538,12 +540,15 @@ class Cache {
|
|
|
538
540
|
var _a;
|
|
539
541
|
this.key = key;
|
|
540
542
|
this.options = options;
|
|
543
|
+
let resolve;
|
|
544
|
+
this.loading = new Promise((r) => resolve = r);
|
|
541
545
|
if (options.storageKey && !options.storage && typeof Storage !== "undefined") options.storage = localStorage;
|
|
542
546
|
if (options.storage) {
|
|
543
547
|
if (options.storage instanceof Table) {
|
|
544
548
|
(async () => {
|
|
545
549
|
var _a2;
|
|
546
|
-
|
|
550
|
+
this.addAll(await ((_a2 = options.storage) == null ? void 0 : _a2.getAll()), false);
|
|
551
|
+
resolve();
|
|
547
552
|
})();
|
|
548
553
|
} else if (options.storageKey) {
|
|
549
554
|
const stored = (_a = options.storage) == null ? void 0 : _a.getItem(options.storageKey);
|
|
@@ -551,8 +556,9 @@ class Cache {
|
|
|
551
556
|
Object.assign(this.store, JSON.parse(stored));
|
|
552
557
|
} catch {
|
|
553
558
|
}
|
|
559
|
+
resolve();
|
|
554
560
|
}
|
|
555
|
-
}
|
|
561
|
+
} else resolve();
|
|
556
562
|
return new Proxy(this, {
|
|
557
563
|
get: (target, prop) => {
|
|
558
564
|
if (prop in target) return target[prop];
|
|
@@ -573,7 +579,20 @@ class Cache {
|
|
|
573
579
|
save(key) {
|
|
574
580
|
if (this.options.storage) {
|
|
575
581
|
if (this.options.storage instanceof Table) {
|
|
576
|
-
|
|
582
|
+
if (key == null) {
|
|
583
|
+
const rows = this.entries();
|
|
584
|
+
rows.forEach(([k, v]) => {
|
|
585
|
+
var _a;
|
|
586
|
+
return (_a = this.options.storage) == null ? void 0 : _a.put(k, v);
|
|
587
|
+
});
|
|
588
|
+
this.options.storage.getAllKeys().then((keys) => {
|
|
589
|
+
rows.map(([k]) => k).filter((k) => !keys.includes(k)).forEach((k) => {
|
|
590
|
+
var _a;
|
|
591
|
+
return (_a = this.options.storage) == null ? void 0 : _a.delete(k);
|
|
592
|
+
});
|
|
593
|
+
});
|
|
594
|
+
} else if (this.store[key] === void 0) this.options.storage.delete(key);
|
|
595
|
+
else this.options.storage.put(key, this.store[key]);
|
|
577
596
|
} else if (this.options.storageKey) {
|
|
578
597
|
this.options.storage.setItem(this.options.storageKey, JSONSanitize(this.store));
|
|
579
598
|
}
|
|
@@ -615,6 +634,7 @@ class Cache {
|
|
|
615
634
|
clear() {
|
|
616
635
|
this.complete = false;
|
|
617
636
|
this.store = {};
|
|
637
|
+
this.save();
|
|
618
638
|
return this;
|
|
619
639
|
}
|
|
620
640
|
/**
|