fix: return correct promise from db-cache

This commit is contained in:
Samuel 2024-12-31 16:52:26 +01:00
parent 03aa2e99cf
commit 576697c5d0
2 changed files with 22 additions and 12 deletions

View file

@ -47,12 +47,12 @@ class LocalStorageCacheAdapter {
return `${this.prefix}-${cacheName}-${key}`; return `${this.prefix}-${cacheName}-${key}`;
} }
set(cacheName: string, key: string, value: unknown) { set(cacheName: string, key: string, value: unknown, isPromise = false) {
const fullKey = this.#createKey(cacheName, key); const fullKey = this.#createKey(cacheName, key);
this.keys.add(fullKey); this.keys.add(fullKey);
try { try {
localStorage.setItem(fullKey, serialize(value)); localStorage.setItem(fullKey, serialize({ isPromise, value }));
} catch (error: unknown) { } catch (error: unknown) {
if ( if (
error instanceof DOMException && error instanceof DOMException &&
@ -75,12 +75,23 @@ class LocalStorageCacheAdapter {
return false; return false;
} }
get<R>(cacheName: string, key: string): R | undefined { get<R>(
cacheName: string,
key: string,
):
| {
isPromise: boolean;
value: R;
}
| undefined {
if (this.#dbLoaded()) { if (this.#dbLoaded()) {
const item = localStorage.getItem(this.#createKey(cacheName, key)); const item = localStorage.getItem(this.#createKey(cacheName, key));
if (item) { if (item) {
return deserialize(item) as R; return deserialize(item) as {
isPromise: boolean;
value: R;
};
} }
} else { } else {
console.info("No database loaded"); console.info("No database loaded");
@ -123,16 +134,17 @@ export const cached = <T extends unknown[], R, TT>(
): ((...args: T) => R) => { ): ((...args: T) => R) => {
const cacheName = hashString(fn.toString()).toString(); const cacheName = hashString(fn.toString()).toString();
// important to return a promise on follow-up calls even if the data is immediately available
let isPromise: boolean;
return (...args: T) => { return (...args: T) => {
const cacheKey = createHashKey(...args).toString(); const cacheKey = createHashKey(...args).toString();
const cachedValue = cache.get<R>(cacheName, cacheKey); const cachedValue = cache.get<R>(cacheName, cacheKey);
if (cachedValue) { if (cachedValue) {
return (isPromise ? Promise.resolve(cachedValue) : cachedValue) as R; return (
cachedValue.isPromise
? Promise.resolve(cachedValue.value)
: cachedValue.value
) as R;
} }
let newValue: R; let newValue: R;
@ -145,11 +157,11 @@ export const cached = <T extends unknown[], R, TT>(
const promisified = Promise.resolve(newValue); const promisified = Promise.resolve(newValue);
isPromise = promisified == newValue; const isPromise = promisified == newValue;
void promisified.then((result) => { void promisified.then((result) => {
if (result !== undefined) { if (result !== undefined) {
cache.set(cacheName, cacheKey, result); cache.set(cacheName, cacheKey, result, isPromise);
} }
}); });

View file

@ -12,8 +12,6 @@ import { getNameFromRecipient } from "~/lib/get-name-from-recipient";
import { OverviewTable, type RoomOverview } from "./overview-table"; import { OverviewTable, type RoomOverview } from "./overview-table";
export const Overview: Component<RouteSectionProps> = () => { export const Overview: Component<RouteSectionProps> = () => {
console.log(overallSentMessagesQuery(SELF_ID));
const [allSelfSentMessagesCount] = createResource(() => const [allSelfSentMessagesCount] = createResource(() =>
overallSentMessagesQuery(SELF_ID), overallSentMessagesQuery(SELF_ID),
); );