From 55b871f4c1788ed8eac5a88e99f4fe768a17a0b0 Mon Sep 17 00:00:00 2001 From: ztimson Date: Sat, 26 Jul 2025 11:24:04 -0400 Subject: [PATCH] Fixed cache loading promise when persistence storage is off --- package.json | 2 +- src/cache.ts | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 2a86b63..7517a72 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/utils", - "version": "0.26.19", + "version": "0.26.20", "description": "Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/cache.ts b/src/cache.ts index 29aa7d1..0c828a7 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -22,8 +22,10 @@ export class Cache { [key: string | number | symbol]: CachedValue | any; /** Whether cache is complete */ complete = false; + + private _loading!: Function; /** Await initial loading */ - loading!: Promise; + loading = new Promise(r => this._loading = r); /** * Create new cache @@ -31,9 +33,6 @@ export class Cache { * @param options */ constructor(public readonly key?: keyof T, public readonly options: CacheOptions = {}) { - let done!: Function; - this.loading = new Promise(r => done = r); - // Persistent storage if(this.options.persistentStorage != null) { if(typeof this.options.persistentStorage == 'string') @@ -45,13 +44,15 @@ export class Cache { const table: Table = await persists.storage.createTable({name: persists.key, key: this.key}); const rows = await table.getAll(); Object.assign(this.store, rows.reduce((acc, row) => ({...acc, [this.getKey(row)]: row}), {})); - done(); + this._loading(); })(); } else if((this.options.persistentStorage?.storage)?.getItem != undefined) { const stored = (this.options.persistentStorage.storage).getItem(this.options.persistentStorage.key); if(stored != null) try { Object.assign(this.store, JSON.parse(stored)); } catch { } - done(); + this._loading(); } + } else { + this._loading(); } // Handle index lookups