RnMultibundler/src/common/StorageHelper.ts

46 行
1.2 KiB
TypeScript

import Storage, { LoadParams } from 'react-native-storage';
import AsyncStorage from '@react-native-async-storage/async-storage';
export const storageApp = new Storage({
size: 1000,
storageBackend: AsyncStorage,
// defaultExpires: 1000 * 3600 * 24 * 60,
enableCache: true,
});
/**
*
* @param key
* @param value
* @param expires
*/
export const saveItem = (key: string, value: any, expires?: number | null) =>
storageApp.save({ key, data: value, expires });
/**
*
* @param key
*/
export const getItem = (key: string) =>
storageApp.load({ key, autoSync: true, syncInBackground: true });
/**
*
* @param key
*/
export const removeItem = (key: string) => storageApp.remove({ key });
/**
*
* @param params
*/
export const getAllItems = (params: LoadParams[]) =>
storageApp.getBatchData(params);
export const saveAllItems = (
params: {
key: string;
id?: string;
data: any;
expires?: number | null;
}[],
) => Promise.all([params.map(item => storageApp.save(item))]);
export const removeAllItems = (params: string[]) =>
Promise.all([params.map(item => storageApp.remove({ key: item }))]);