about summary refs log tree commit diff
path: root/src/state/persisted/store.ts
blob: bb7fbed89089036782680ffef432bdc1a6535596 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import AsyncStorage from '@react-native-async-storage/async-storage'

import {Schema, schema} from '#/state/persisted/schema'
import {logger} from '#/logger'

const BSKY_STORAGE = 'BSKY_STORAGE'

export async function write(value: Schema) {
  schema.parse(value)
  await AsyncStorage.setItem(BSKY_STORAGE, JSON.stringify(value))
}

export async function read(): Promise<Schema | undefined> {
  const rawData = await AsyncStorage.getItem(BSKY_STORAGE)
  const objData = rawData ? JSON.parse(rawData) : undefined
  if (schema.safeParse(objData).success) {
    return objData
  }
}

export async function clear() {
  try {
    await AsyncStorage.removeItem(BSKY_STORAGE)
  } catch (e: any) {
    logger.error(`persisted store: failed to clear`, {message: e.toString()})
  }
}