diff --git a/package.json b/package.json index db2e8fa..2e2fd1d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@ztimson/utils", - "version": "0.25.21", + "version": "0.25.22", "description": "Utility library", "author": "Zak Timson", "license": "MIT", diff --git a/src/cache.ts b/src/cache.ts index 8da3471..0eea5e3 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -24,6 +24,8 @@ export class Cache { [key: string | number | symbol]: CachedValue | any; /** Whether cache is complete */ complete = false; + /** Await initial loading */ + loading!: Promise; /** * Create new cache @@ -31,15 +33,21 @@ export class Cache { * @param options */ constructor(public readonly key?: keyof T, public readonly options: CacheOptions = {}) { + let resolve: any; + this.loading = new Promise(r => resolve = r); if(options.storageKey && !options.storage && typeof(Storage) !== 'undefined') options.storage = localStorage; if(options.storage) { if(options.storage instanceof Table) { - (async () => this.addAll(await options.storage?.getAll(), false))() + (async () => { + this.addAll(await options.storage?.getAll(), false); + resolve(); + })() } else if(options.storageKey) { const stored = options.storage?.getItem(options.storageKey); if(stored != null) try { Object.assign(this.store, JSON.parse(stored)); } catch { } + resolve(); } - } + } else resolve(); return new Proxy(this, { get: (target: this, prop: string | symbol) => { if(prop in target) return (target as any)[prop];