@yoroi/common
Version:
The Common package of Yoroi SDK
102 lines (101 loc) • 3.52 kB
JavaScript
;
import AsyncStorage from '@react-native-async-storage/async-storage';
import { parseSafe } from '../../utils/parsers';
import { isFileKey } from '../helpers/is-file-key';
import { isFolderKey } from '../helpers/is-folder-key';
export const mountAsyncStorage = ({
path
}) => {
const withPath = key => `${path}${key}`;
const withoutPath = value => value.slice(path.length);
async function getItem(key, parse = parseSafe) {
const item = await AsyncStorage.getItem(withPath(key));
return parse(item);
}
async function multiGet(keys, parse = parseSafe) {
const absolutePaths = keys.map(key => withPath(key));
const items = await AsyncStorage.multiGet(absolutePaths);
return items.map(([key, value]) => [withoutPath(key), parse(value)]);
}
return {
join: folderName => mountAsyncStorage({
path: `${path}${folderName}`
}),
getItem,
multiGet,
setItem: async (key, value, stringify = JSON.stringify) => {
const item = stringify(value);
await AsyncStorage.setItem(withPath(key), item);
},
multiSet: async (tuples, stringify = JSON.stringify) => {
const items = tuples.map(([key, value]) => [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(([key]) => 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