27 行
785 B
TypeScript
27 行
785 B
TypeScript
|
|
import AsyncStorage from '@react-native-async-storage/async-storage'
|
||
|
|
|
||
|
|
export const K = {
|
||
|
|
DEMO_TOKEN: '@xuqm:demoToken',
|
||
|
|
IM_TOKEN: '@xuqm:imToken',
|
||
|
|
PROFILE: '@xuqm:profile',
|
||
|
|
CONTACTS: '@xuqm:contacts',
|
||
|
|
} as const
|
||
|
|
|
||
|
|
export async function save(key: string, value: unknown): Promise<void> {
|
||
|
|
await AsyncStorage.setItem(key, JSON.stringify(value))
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function load<T>(key: string): Promise<T | null> {
|
||
|
|
const raw = await AsyncStorage.getItem(key)
|
||
|
|
if (!raw) return null
|
||
|
|
try { return JSON.parse(raw) as T } catch { return null }
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function remove(key: string): Promise<void> {
|
||
|
|
await AsyncStorage.removeItem(key)
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function clearSession(): Promise<void> {
|
||
|
|
await AsyncStorage.multiRemove([K.DEMO_TOKEN, K.IM_TOKEN, K.PROFILE])
|
||
|
|
}
|