forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import {create as createArchiveDB} from '#/storage/archive/db'
2
3/**
4 * Interface for async storage compatible with @tanstack/query-async-storage-persister
5 */
6export interface PersistedQueryStorage {
7 getItem: (key: string) => Promise<string | null>
8 setItem: (key: string, value: string) => Promise<void>
9 removeItem: (key: string) => Promise<void>
10}
11
12function createId(id: string) {
13 return `react-query-cache-${id}`
14}
15
16/**
17 * Creates an MMKV-based storage adapter for persisting react-query cache on native platforms.
18 * Each storage instance uses a separate MMKV store identified by the provided id.
19 * MMKV provides synchronous access but we wrap it in Promises for API compatibility.
20 *
21 * @param id - Unique identifier for this storage instance (used as MMKV store id)
22 */
23export function createPersistedQueryStorage(id: string): PersistedQueryStorage {
24 const store = createArchiveDB({id: createId(id)})
25 return {
26 getItem: async (key: string): Promise<string | null> => {
27 return (await store.get(key)) ?? null
28 },
29 setItem: async (key: string, value: string): Promise<void> => {
30 await store.set(key, value)
31 },
32 removeItem: async (key: string): Promise<void> => {
33 await store.delete(key)
34 },
35 }
36}
37
38export async function clearPersistedQueryStorage(id: string) {
39 const store = createArchiveDB({id: createId(id)})
40 await store.clear()
41}