UNPKG

@convo-lang/convo-lang

Version:
41 lines 1.71 kB
import { currentBaseUser } from "@iyio/common"; import { ConvoHashCacheBase } from "./ConvoHashCacheBase.js"; import { ConvoLocalStorageCache } from "./ConvoLocalStorageCache.js"; import { ConvoVfsCache } from "./ConvoVfsCache.js"; import { commonConvoCacheTypes } from "./convo-lib.js"; /** * Uses a combination of local storage and the virtual file system to cache. Read attempts will * first try the vfs then fallback to local storage. Writing to the vfs will only occur if a user * is signed in. This cache type is good for applications that want user to be able to always read * from the vfs cache but don't want unauthorized user to write to the cache. */ export class ConvoUserVfsCache extends ConvoHashCacheBase { constructor(options = {}) { super(commonConvoCacheTypes.userVfs); this.localStorageCache = new ConvoLocalStorageCache(options.localStorageOptions); this.vfsCache = new ConvoVfsCache(options.vfsOptions); } async getMessagesByKey(key) { const user = currentBaseUser.get(); if (user) { return await this.vfsCache.getMessagesByKey(key); } else { const [fs, ls] = await Promise.all([ this.vfsCache.getMessagesByKey(key), this.localStorageCache.getMessagesByKey(key) ]); return fs ?? ls; } } async cacheMessagesByKey(key, messages) { const user = currentBaseUser.get(); if (user) { await this.vfsCache.cacheMessagesByKey(key, messages); } else { this.localStorageCache.cacheMessagesByKey(key, messages); } } } //# sourceMappingURL=ConvoUserVfsCache.js.map