|
|
/** |
|
|
* Session Storage 实现 |
|
|
* |
|
|
* React Native 没有原生的 sessionStorage,这里提供一个内存实现 |
|
|
* 数据只在应用运行期间保存,应用关闭后会丢失 |
|
|
* |
|
|
* 特点: |
|
|
* - 数据存储在内存中 |
|
|
* - 应用重启后数据丢失 |
|
|
* - 适用于临时数据、会话数据 |
|
|
* - API 与 localStorage 类似 |
|
|
*/ |
|
|
|
|
|
/** |
|
|
* Session Storage 键名常量 |
|
|
*/ |
|
|
export enum SESSION_KEYS { |
|
|
TEMP_DATA = 'temp_data', |
|
|
FORM_DRAFT = 'form_draft', |
|
|
SEARCH_HISTORY = 'search_history', |
|
|
CURRENT_TAB = 'current_tab', |
|
|
SCROLL_POSITION = 'scroll_position', |
|
|
FILTER_STATE = 'filter_state', |
|
|
} |
|
|
|
|
|
/** |
|
|
* Session Storage 类 |
|
|
* |
|
|
* 使用 Map 实现内存存储 |
|
|
*/ |
|
|
class SessionStorage { |
|
|
private static storage: Map<string, string> = new Map(); |
|
|
|
|
|
/** |
|
|
* 存储字符串 |
|
|
*/ |
|
|
static setString(key: string, value: string): void { |
|
|
try { |
|
|
this.storage.set(key, value); |
|
|
if (__DEV__) { |
|
|
console.log(`💾 SessionStorage set: ${key}`); |
|
|
} |
|
|
} catch (error) { |
|
|
console.error(`SessionStorage setString error for key "${key}":`, error); |
|
|
throw error; |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* 获取字符串 |
|
|
*/ |
|
|
static getString(key: string): string | null { |
|
|
try { |
|
|
const value = this.storage.get(key) ?? null; |
|
|
if (__DEV__) { |
|
|
console.log(`📖 SessionStorage get: ${key}`, value ? '✓' : '✗'); |
|
|
} |
|
|
return value; |
|
|
} catch (error) { |
|
|
console.error(`SessionStorage getString error for key "${key}":`, error); |
|
|
return null; |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* 存储对象(自动序列化为 JSON) |
|
|
*/ |
|
|
static setObject<T>(key: string, value: T): void { |
|
|
try { |
|
|
const jsonValue = JSON.stringify(value); |
|
|
this.storage.set(key, jsonValue); |
|
|
if (__DEV__) { |
|
|
console.log(`💾 SessionStorage set object: ${key}`); |
|
|
} |
|
|
} catch (error) { |
|
|
console.error(`SessionStorage setObject error for key "${key}":`, error); |
|
|
throw error; |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* 获取对象(自动反序列化 JSON) |
|
|
*/ |
|
|
static getObject<T>(key: string): T | null { |
|
|
try { |
|
|
const jsonValue = this.storage.get(key); |
|
|
if (jsonValue === undefined) { |
|
|
return null; |
|
|
} |
|
|
const value = JSON.parse(jsonValue) as T; |
|
|
if (__DEV__) { |
|
|
console.log(`📖 SessionStorage get object: ${key} ✓`); |
|
|
} |
|
|
return value; |
|
|
} catch (error) { |
|
|
console.error(`SessionStorage getObject error for key "${key}":`, error); |
|
|
return null; |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* 删除指定键 |
|
|
*/ |
|
|
static remove(key: string): void { |
|
|
try { |
|
|
this.storage.delete(key); |
|
|
if (__DEV__) { |
|
|
console.log(`🗑️ SessionStorage remove: ${key}`); |
|
|
} |
|
|
} catch (error) { |
|
|
console.error(`SessionStorage remove error for key "${key}":`, error); |
|
|
throw error; |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* 清空所有存储 |
|
|
*/ |
|
|
static clear(): void { |
|
|
try { |
|
|
this.storage.clear(); |
|
|
if (__DEV__) { |
|
|
console.log('🗑️ SessionStorage cleared all'); |
|
|
} |
|
|
} catch (error) { |
|
|
console.error('SessionStorage clear error:', error); |
|
|
throw error; |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* 获取所有键名 |
|
|
*/ |
|
|
static getAllKeys(): string[] { |
|
|
try { |
|
|
const keys = Array.from(this.storage.keys()); |
|
|
if (__DEV__) { |
|
|
console.log('🔑 SessionStorage all keys:', keys); |
|
|
} |
|
|
return keys; |
|
|
} catch (error) { |
|
|
console.error('SessionStorage getAllKeys error:', error); |
|
|
return []; |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* 获取存储项数量 |
|
|
*/ |
|
|
static get length(): number { |
|
|
return this.storage.size; |
|
|
} |
|
|
|
|
|
/** |
|
|
* 检查键是否存在 |
|
|
*/ |
|
|
static has(key: string): boolean { |
|
|
return this.storage.has(key); |
|
|
} |
|
|
|
|
|
/** |
|
|
* 批量获取 |
|
|
*/ |
|
|
static multiGet(keys: string[]): [string, string | null][] { |
|
|
try { |
|
|
return keys.map((key) => [key, this.storage.get(key) ?? null]); |
|
|
} catch (error) { |
|
|
console.error('SessionStorage multiGet error:', error); |
|
|
return []; |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* 批量设置 |
|
|
*/ |
|
|
static multiSet(keyValuePairs: [string, string][]): void { |
|
|
try { |
|
|
keyValuePairs.forEach(([key, value]) => { |
|
|
this.storage.set(key, value); |
|
|
}); |
|
|
if (__DEV__) { |
|
|
console.log(`💾 SessionStorage multiSet: ${keyValuePairs.length} items`); |
|
|
} |
|
|
} catch (error) { |
|
|
console.error('SessionStorage multiSet error:', error); |
|
|
throw error; |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* 批量删除 |
|
|
*/ |
|
|
static multiRemove(keys: string[]): void { |
|
|
try { |
|
|
keys.forEach((key) => { |
|
|
this.storage.delete(key); |
|
|
}); |
|
|
if (__DEV__) { |
|
|
console.log(`🗑️ SessionStorage multiRemove: ${keys.length} items`); |
|
|
} |
|
|
} catch (error) { |
|
|
console.error('SessionStorage multiRemove error:', error); |
|
|
throw error; |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* 获取所有数据(调试用) |
|
|
*/ |
|
|
static getAll(): Record<string, string> { |
|
|
const result: Record<string, string> = {}; |
|
|
this.storage.forEach((value, key) => { |
|
|
result[key] = value; |
|
|
}); |
|
|
return result; |
|
|
} |
|
|
} |
|
|
|
|
|
export default SessionStorage; |
|
|
|
|
|
|