Skip to content

Commit

Permalink
Refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
bLopata committed Dec 11, 2024
1 parent 4e8c8ff commit 5ae182c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 66 deletions.
47 changes: 21 additions & 26 deletions www/app/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -150,30 +150,20 @@ export default function Chat({
return getConversations();
};

const { data: conversations, mutate: mutateConversations } = useSWR(
userId,
conversationsFetcher,
{
fallbackData: initialConversations,
onSuccess: async (conversations) => {
if (conversations.length) {
if (
!conversationId ||
!conversations.find((c) => c.conversationId === conversationId)
) {
setConversationId(conversations[0].conversationId);
}
setCanSend(true);
} else {
const newConvo = await createConversation();
setConversationId(newConvo?.conversationId);
await mutateConversations();
}
},
provider: cacheProvider,
revalidateOnFocus: false,
}
);
const conversationsKey = useMemo(() => userId, [userId]);

const { data: conversations, mutate: mutateConversations } = useSWR(
conversationsKey,
conversationsFetcher,
{
fallbackData: initialConversations,
provider: cacheProvider,
revalidateOnFocus: false,
dedupingInterval: 60000,
revalidateIfStale: false,
revalidateOnMount: false,
}
);

const messagesFetcher = async (conversationId: string) => {
if (!userId) return Promise.resolve([]);
Expand All @@ -183,12 +173,17 @@ export default function Chat({
return getMessages(conversationId);
};

const messagesKey = useMemo(() =>
conversationId ? ['messages', conversationId] : null,
[conversationId]
);

const {
data: messages,
mutate: mutateMessages,
isLoading: messagesLoading,
} = useSWR(
conversationId ? ['messages', conversationId] : null,
messagesKey,
() => messagesFetcher(conversationId!),
{
fallbackData: initialMessages,
Expand All @@ -200,7 +195,7 @@ export default function Chat({
if (conversationId?.startsWith('temp-')) {
mutateMessages([], false);
}
},
}
}
);

Expand Down
47 changes: 7 additions & 40 deletions www/utils/swrCache.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,12 @@
import { createStore, get, set, del, clear, } from 'idb-keyval';
import type { Cache } from 'swr';

const store = typeof window !== 'undefined'
? createStore('bloom-db', 'messages')
: null;

// In-memory cache for synchronous operations
const memoryCache = new Map<string, any>();

export const clearSWRCache = async () => {
memoryCache.clear();
if (store && typeof window !== 'undefined') {
try {
await clear(store);
} catch (error) {
console.error('Error clearing cache:', error);
}
}
};
export const cacheProvider = (cache: Readonly<Cache<any>>) => ({
get: (key: string) => memoryCache.get(key),
set: (key: string, value: any) => memoryCache.set(key, value),
delete: (key: string) => memoryCache.delete(key),
keys: () => Array.from(memoryCache.keys())[Symbol.iterator]()
});

export const cacheProvider = (cache: Readonly<Cache<any>>) => {
return {
get: (key: string) => {
return memoryCache.get(key);
},
set: (key: string, value: any) => {
memoryCache.set(key, value);
// Persist to IndexedDB in background
if (store) {
set(key, value, store).catch(console.error);
}
},
delete: (key: string) => {
memoryCache.delete(key);
// Delete from IndexedDB in background
if (store) {
del(key, store).catch(console.error);
}
},
keys: () => {
return memoryCache.keys();
}
}
}
export const clearSWRCache = () => memoryCache.clear();

0 comments on commit 5ae182c

Please sign in to comment.