Fixed cache loading promise when persistence storage is off
Some checks failed
Build / Build NPM Project (push) Successful in 54s
Build / Publish Documentation (push) Failing after 4s
Build / Tag Version (push) Successful in 8s

This commit is contained in:
2025-07-26 11:24:04 -04:00
parent fb077775b6
commit 55b871f4c1
2 changed files with 8 additions and 7 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@ztimson/utils",
"version": "0.26.19",
"version": "0.26.20",
"description": "Utility library",
"author": "Zak Timson",
"license": "MIT",

View File

@ -22,8 +22,10 @@ export class Cache<K extends string | number | symbol, T> {
[key: string | number | symbol]: CachedValue<T> | any;
/** Whether cache is complete */
complete = false;
private _loading!: Function;
/** Await initial loading */
loading!: Promise<void>;
loading = new Promise<void>(r => this._loading = r);
/**
* Create new cache
@ -31,9 +33,6 @@ export class Cache<K extends string | number | symbol, T> {
* @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<K extends string | number | symbol, T> {
const table: Table<any, any> = 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((<any>this.options.persistentStorage?.storage)?.getItem != undefined) {
const stored = (<Storage>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