4 changed files with 94 additions and 19 deletions
@ -0,0 +1,37 @@ |
|||
import AsyncStorage from '@react-native-async-storage/async-storage'; |
|||
|
|||
/* ===================== 轻量持久化封装 ===================== |
|||
* 统一封装 @react-native-async-storage/async-storage, |
|||
* 业务侧只调用 get/set/remove,不感知底层实现; |
|||
* 所有读写包 try/catch,存储不可用(如 web 未链接原生模块)时静默降级, |
|||
* 不影响内存态功能。键统一加 'mydreamjob:' 前缀避免与其他 App 冲突。 |
|||
*/ |
|||
|
|||
const KEY_PREFIX = 'mydreamjob:'; |
|||
|
|||
/** 读取字符串值,缺失/异常时返回 null */ |
|||
export async function storageGet(key: string): Promise<string | null> { |
|||
try { |
|||
return await AsyncStorage.getItem(KEY_PREFIX + key); |
|||
} catch { |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
/** 写入字符串值,异常时静默忽略 */ |
|||
export async function storageSet(key: string, value: string): Promise<void> { |
|||
try { |
|||
await AsyncStorage.setItem(KEY_PREFIX + key, value); |
|||
} catch { |
|||
/* 存储不可用时降级为内存态,不抛出 */ |
|||
} |
|||
} |
|||
|
|||
/** 删除键,异常时静默忽略 */ |
|||
export async function storageRemove(key: string): Promise<void> { |
|||
try { |
|||
await AsyncStorage.removeItem(KEY_PREFIX + key); |
|||
} catch { |
|||
/* 同上 */ |
|||
} |
|||
} |
|||
Loading…
Reference in new issue