UNPKG

@yoroi/common

Version:

The Common package of Yoroi SDK

114 lines 3.99 kB
import AsyncStorage from '@react-native-async-storage/async-storage'; import { parseSafe } from '../../utils/parsers'; import { isFolderKey } from '../helpers/is-folder-key'; import { isFileKey } from '../helpers/is-file-key'; export const mountAsyncStorage = _ref => { let { path } = _ref; const withPath = key => `${path}${key}`; const withoutPath = value => value.slice(path.length); async function getItem(key) { let parse = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : parseSafe; const item = await AsyncStorage.getItem(withPath(key)); return parse(item); } async function multiGet(keys) { let parse = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : parseSafe; const absolutePaths = keys.map(key => withPath(key)); const items = await AsyncStorage.multiGet(absolutePaths); return items.map(_ref2 => { let [key, value] = _ref2; return [withoutPath(key), parse(value)]; }); } return { join: folderName => mountAsyncStorage({ path: `${path}${folderName}` }), getItem, multiGet, setItem: async function (key, value) { let stringify = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : JSON.stringify; const item = stringify(value); await AsyncStorage.setItem(withPath(key), item); }, multiSet: async function (tuples) { let stringify = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : JSON.stringify; const items = tuples.map(_ref3 => { let [key, value] = _ref3; return [withPath(key), stringify(value)]; }); await AsyncStorage.multiSet(items); }, removeItem: async key => { await AsyncStorage.removeItem(withPath(key)); }, removeFolder: async folderName => { const keys = await AsyncStorage.getAllKeys(); const filteredKeys = keys.filter(key => key.startsWith(path) && withoutPath(key).startsWith(folderName) && isFolderKey({ key, path })); await AsyncStorage.multiRemove(filteredKeys); }, multiRemove: async keys => { await AsyncStorage.multiRemove(keys.map(key => withPath(key))); }, getAllKeys: () => { return AsyncStorage.getAllKeys().then(keys => keys.filter(key => key.startsWith(path) && isFileKey({ key, path }))).then( // temporary any until async interface is migrated to receive keys for multi storage filteredKeys => filteredKeys.map(withoutPath)); }, clear: async () => { const keys = await AsyncStorage.getAllKeys(); const filteredKeys = keys.filter(key => key.startsWith(path)); await AsyncStorage.multiRemove(filteredKeys); } }; }; export const mountAsyncMultiStorage = options => { const { storage, dataFolder, keyExtractor, serializer = JSON.stringify, deserializer = parseSafe } = options; const dataStorage = storage.join(dataFolder); const { getAllKeys: getAllKeysStorage, multiSet, multiGet } = dataStorage; const clear = () => storage.removeFolder(dataFolder); const saveMany = items => { const entries = items.map(item => { if (typeof keyExtractor === 'function') { return [keyExtractor(item), item]; } return [String(item[keyExtractor]), item]; }); const entriesWithKeys = entries.filter(_ref4 => { let [key] = _ref4; return key != null && key !== ''; }); return multiSet(entriesWithKeys, serializer); }; const readAll = () => getAllKeys().then(keysToRead => multiGet(keysToRead, deserializer)); const readMany = keysToRead => dataStorage.multiGet(keysToRead, deserializer); const removeMany = keysToRead => dataStorage.multiRemove(keysToRead); const getAllKeys = () => getAllKeysStorage().then(keys => keys); return { getAllKeys, clear, readAll, saveMany, readMany, removeMany }; }; //# sourceMappingURL=async-storage.js.map