mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import AsyncStorage from '@react-native-async-storage/async-storage'
2
3import {logger} from '#/logger'
4import {Schema, schema} from '#/state/persisted/schema'
5
6const BSKY_STORAGE = 'BSKY_STORAGE'
7
8export async function write(value: Schema) {
9 schema.parse(value)
10 await AsyncStorage.setItem(BSKY_STORAGE, JSON.stringify(value))
11}
12
13export async function read(): Promise<Schema | undefined> {
14 const rawData = await AsyncStorage.getItem(BSKY_STORAGE)
15 const objData = rawData ? JSON.parse(rawData) : undefined
16 const parsed = schema.safeParse(objData)
17 if (parsed.success) {
18 return objData
19 } else {
20 const errors =
21 parsed.error?.errors?.map(e => ({
22 code: e.code,
23 // @ts-ignore exists on some types
24 expected: e?.expected,
25 path: e.path,
26 })) || []
27 logger.error(`persisted store: data failed validation on read`, {errors})
28 return undefined
29 }
30}
31
32export async function clear() {
33 try {
34 await AsyncStorage.removeItem(BSKY_STORAGE)
35 } catch (e: any) {
36 logger.error(`persisted store: failed to clear`, {message: e.toString()})
37 }
38}