You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
5.0 KiB

1 month ago
/**
*
*
* 使 localStorage (AsyncStorage) sessionStorage
*
* 使
* - localStorage: 持久化数据
* - sessionStorage: 临时数据
*
*
* ```typescript
* // 使用 localStorage(默认)
* await StorageManager.set('user', userData);
*
* // 使用 sessionStorage
* await StorageManager.set('temp', tempData, { type: 'session' });
*
* // 获取数据(自动从正确的存储中读取)
* const user = await StorageManager.get('user');
* const temp = await StorageManager.get('temp', { type: 'session' });
* ```
*/
import Storage from './storage';
import SessionStorage from './sessionStorage';
/**
*
*/
export type StorageType = 'local' | 'session';
/**
*
*/
export interface StorageOptions {
/**
*
* - 'local': AsyncStorage
* - 'session':
*/
type?: StorageType;
}
/**
*
*/
class StorageManager {
/**
*
*/
static async setString(
key: string,
value: string,
options: StorageOptions = {}
): Promise<void> {
const { type = 'local' } = options;
if (type === 'session') {
SessionStorage.setString(key, value);
} else {
await Storage.setString(key, value);
}
}
/**
*
*/
static async getString(
key: string,
options: StorageOptions = {}
): Promise<string | null> {
const { type = 'local' } = options;
if (type === 'session') {
return SessionStorage.getString(key);
} else {
return await Storage.getString(key);
}
}
/**
*
*/
static async setObject<T>(
key: string,
value: T,
options: StorageOptions = {}
): Promise<void> {
const { type = 'local' } = options;
if (type === 'session') {
SessionStorage.setObject(key, value);
} else {
await Storage.setObject(key, value);
}
}
/**
*
*/
static async getObject<T>(
key: string,
options: StorageOptions = {}
): Promise<T | null> {
const { type = 'local' } = options;
if (type === 'session') {
return SessionStorage.getObject<T>(key);
} else {
return await Storage.getObject<T>(key);
}
}
/**
*
*/
static async remove(key: string, options: StorageOptions = {}): Promise<void> {
const { type = 'local' } = options;
if (type === 'session') {
SessionStorage.remove(key);
} else {
await Storage.remove(key);
}
}
/**
*
*/
static async clear(options: StorageOptions = {}): Promise<void> {
const { type = 'local' } = options;
if (type === 'session') {
SessionStorage.clear();
} else {
await Storage.clear();
}
}
/**
*
*/
static async getAllKeys(options: StorageOptions = {}): Promise<string[]> {
const { type = 'local' } = options;
if (type === 'session') {
return SessionStorage.getAllKeys();
} else {
return await Storage.getAllKeys();
}
}
/**
*
*/
static async has(key: string, options: StorageOptions = {}): Promise<boolean> {
const { type = 'local' } = options;
if (type === 'session') {
return SessionStorage.has(key);
} else {
const value = await Storage.getString(key);
return value !== null;
}
}
/**
*
*/
static async multiGet(
keys: string[],
options: StorageOptions = {}
): Promise<[string, string | null][]> {
const { type = 'local' } = options;
if (type === 'session') {
return SessionStorage.multiGet(keys);
} else {
return await Storage.multiGet(keys);
}
}
/**
*
*/
static async multiSet(
keyValuePairs: [string, string][],
options: StorageOptions = {}
): Promise<void> {
const { type = 'local' } = options;
if (type === 'session') {
SessionStorage.multiSet(keyValuePairs);
} else {
await Storage.multiSet(keyValuePairs);
}
}
/**
*
*/
static async multiRemove(
keys: string[],
options: StorageOptions = {}
): Promise<void> {
const { type = 'local' } = options;
if (type === 'session') {
SessionStorage.multiRemove(keys);
} else {
await Storage.multiRemove(keys);
}
}
/**
* session storage
*/
static getSize(options: StorageOptions = {}): number {
const { type = 'local' } = options;
if (type === 'session') {
return SessionStorage.length;
} else {
// AsyncStorage 不支持直接获取大小
return -1;
}
}
/**
* local + session
*/
static async clearAll(): Promise<void> {
await Storage.clear();
SessionStorage.clear();
if (__DEV__) {
console.log('🗑 All storage cleared (local + session)');
}
}
}
export default StorageManager;