@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 CHANGED
@@ -23,6 +23,8 @@ export declare class Cache<K extends string | number | symbol, T> {
23
23
  [key: string | number | symbol]: CachedValue<T> | any;
24
24
  /** Whether cache is complete */
25
25
  complete: boolean;
26
+ /** Await initial loading */
27
+ loading: Promise<void>;
26
28
  /**
27
29
  * Create new cache
28
30
  * @param {keyof T} key Default property to use as primary key
package/dist/index.cjs CHANGED
@@ -534,6 +534,8 @@ ${opts.message || this.desc}`;
534
534
  __publicField(this, "store", {});
535
535
  /** Whether cache is complete */
536
536
  __publicField(this, "complete", false);
537
+ /** Await initial loading */
538
+ __publicField(this, "loading");
537
539
  /**
538
540
  * Get all cached items
539
541
  * @return {T[]} Array of items
@@ -542,12 +544,15 @@ ${opts.message || this.desc}`;
542
544
  var _a;
543
545
  this.key = key;
544
546
  this.options = options;
547
+ let resolve;
548
+ this.loading = new Promise((r) => resolve = r);
545
549
  if (options.storageKey && !options.storage && typeof Storage !== "undefined") options.storage = localStorage;
546
550
  if (options.storage) {
547
551
  if (options.storage instanceof Table) {
548
552
  (async () => {
549
553
  var _a2;
550
- return this.addAll(await ((_a2 = options.storage) == null ? void 0 : _a2.getAll()), false);
554
+ this.addAll(await ((_a2 = options.storage) == null ? void 0 : _a2.getAll()), false);
555
+ resolve();
551
556
  })();
552
557
  } else if (options.storageKey) {
553
558
  const stored = (_a = options.storage) == null ? void 0 : _a.getItem(options.storageKey);
@@ -555,8 +560,9 @@ ${opts.message || this.desc}`;
555
560
  Object.assign(this.store, JSON.parse(stored));
556
561
  } catch {
557
562
  }
563
+ resolve();
558
564
  }
559
- }
565
+ } else resolve();
560
566
  return new Proxy(this, {
561
567
  get: (target, prop) => {
562
568
  if (prop in target) return target[prop];
@@ -577,7 +583,20 @@ ${opts.message || this.desc}`;
577
583
  save(key) {
578
584
  if (this.options.storage) {
579
585
  if (this.options.storage instanceof Table) {
580
- this.options.storage.put(key, this.store[key]);
586
+ if (key == null) {
587
+ const rows = this.entries();
588
+ rows.forEach(([k, v]) => {
589
+ var _a;
590
+ return (_a = this.options.storage) == null ? void 0 : _a.put(k, v);
591
+ });
592
+ this.options.storage.getAllKeys().then((keys) => {
593
+ rows.map(([k]) => k).filter((k) => !keys.includes(k)).forEach((k) => {
594
+ var _a;
595
+ return (_a = this.options.storage) == null ? void 0 : _a.delete(k);
596
+ });
597
+ });
598
+ } else if (this.store[key] === void 0) this.options.storage.delete(key);
599
+ else this.options.storage.put(key, this.store[key]);
581
600
  } else if (this.options.storageKey) {
582
601
  this.options.storage.setItem(this.options.storageKey, JSONSanitize(this.store));
583
602
  }
@@ -619,6 +638,7 @@ ${opts.message || this.desc}`;
619
638
  clear() {
620
639
  this.complete = false;
621
640
  this.store = {};
641
+ this.save();
622
642
  return this;
623
643
  }
624
644
  /**