UNPKG

@applicaster/zapp-react-native-utils

Version:

Applicaster Zapp React Native utilities package

231 lines (193 loc) • 5.84 kB
import { localStorage } from "@applicaster/zapp-react-native-bridge/ZappStorage/LocalStorage"; import { isNilOrEmpty } from "../reactUtils/helpers"; import { parseJsonIfNeeded } from "@applicaster/zapp-react-native-utils/functionUtils"; const defaultOwnedKey = "owned_keys"; const DEFAULT_NAMESPACE = "applicaster.v2"; // Structure // { // namespace1 : [key1, key2], // namespace2 : [key1, key2], // } async function getOwnedStorageKeys( ownershipKey: string, ownershipNamespace: string ): Promise<StorageOwnedValues> { if (isNilOrEmpty(ownershipNamespace)) { return {}; } const loginKeys = await localStorage.getItem( ownershipKey, ownershipNamespace ); if (isNilOrEmpty(loginKeys)) { return {}; } return parseJsonIfNeeded(loginKeys); } function mapOwnedKeysToAdd( storageValues: StorageValuesToAdd ): StorageOwnedValues { const mappedData = {}; Object.keys(storageValues).forEach((namespace: string) => { const data = storageValues[namespace]; mappedData[namespace] = Object.keys(data); }); return mappedData; } export async function batchSaveToLocalStorage( storageValues: StorageValuesToAdd ) { if (isNilOrEmpty(storageValues)) { return false; } for (const namespace of Object.keys(storageValues)) { const namespaceData = storageValues[namespace]; for (const key of Object.keys(namespaceData)) { const value = namespaceData[key]; if (!isNilOrEmpty(value)) { await localStorage.setItem(key, value, namespace); } } } } export async function batchRemoveFromLocalStorage( storageValues: StorageValuesToRemove ) { for (const namespace of Object.keys(storageValues)) { const namespaceData = storageValues[namespace]; for (const key of namespaceData) { await localStorage.removeItem(key, namespace); } } } async function addOwnedKeys({ newKeys, ownershipKey, ownershipNamespace, }: { newKeys: StorageOwnedValues; ownershipKey: string; ownershipNamespace: string; }) { const allStoragedOwnedKeys: StorageOwnedValues = await getOwnedStorageKeys( ownershipKey, ownershipNamespace ); Object.keys(newKeys).forEach(async (namespace: string) => { const newOwnedKeys: string[] = newKeys[namespace]; const currentKeys = allStoragedOwnedKeys[namespace] || []; const combinedSet = new Set([...currentKeys, ...newOwnedKeys]); allStoragedOwnedKeys[namespace] = Array.from(combinedSet); }); const data = JSON.stringify(allStoragedOwnedKeys); await localStorage.setItem(ownershipKey, data, ownershipNamespace); } async function removeOwnedKeys({ toRemoveKeys, ownershipKey, ownershipNamespace, }: { toRemoveKeys: StorageOwnedValues; ownershipKey: string; ownershipNamespace: string; }) { const currentKeysList: StorageOwnedValues = await getOwnedStorageKeys( ownershipKey, ownershipNamespace ); Object.keys(toRemoveKeys).forEach(async (namespace: string) => { const keysToRemove: string[] = toRemoveKeys[namespace] || []; const currentKeys: string[] = currentKeysList[namespace] || []; const storageOwnedSet = new Set(currentKeys); const keysToRemoveSet = new Set(keysToRemove); function removeAll(originalSet, toBeRemovedSet) { toBeRemovedSet.forEach(Set.prototype.delete, originalSet); } removeAll(storageOwnedSet, keysToRemoveSet); const newLoginKeys = Array.from(storageOwnedSet); if (isNilOrEmpty(newLoginKeys)) { delete currentKeysList[namespace]; } else { currentKeysList[namespace] = newLoginKeys; } }); if (isNilOrEmpty(currentKeysList)) { await localStorage.removeItem(ownershipKey, ownershipNamespace); } else { const data = JSON.stringify(currentKeysList); await localStorage.setItem(ownershipKey, data, ownershipNamespace); } } export async function batchSaveOwnedValues({ storageValues, ownershipKey = defaultOwnedKey, ownershipNamespace, }: { storageValues: StorageValuesToAdd; ownershipKey?: any; ownershipNamespace; }) { await batchSaveToLocalStorage(storageValues); const ownedKeys: StorageOwnedValues = mapOwnedKeysToAdd(storageValues); await addOwnedKeys({ newKeys: ownedKeys, ownershipKey, ownershipNamespace }); } export async function batchSaveOwnedNamespaceValues({ dataForNamespaces, ownershipKey = defaultOwnedKey, ownershipNamespace = DEFAULT_NAMESPACE, }: { dataForNamespaces: NamespaceValues; ownershipKey?: string; ownershipNamespace?: string; }) { const data: StorageValuesToAdd = { [ownershipNamespace]: dataForNamespaces }; return batchSaveOwnedValues({ storageValues: data, ownershipKey, ownershipNamespace, }); } export async function batchRemoveOwnedValues({ storageValues, ownershipKey = defaultOwnedKey, ownershipNamespace, }: { storageValues: StorageValuesToRemove; ownershipKey: string; ownershipNamespace: string; }) { await batchRemoveFromLocalStorage(storageValues); await removeOwnedKeys({ toRemoveKeys: storageValues, ownershipKey, ownershipNamespace, }); } export async function batchRemoveOwnedNamespaceKeys({ ownershipKey = defaultOwnedKey, ownershipNamespace, }: { ownershipKey?: string; ownershipNamespace: string; }) { const ownedStorageKeys = await getOwnedStorageKeys( ownershipKey, ownershipNamespace ); if (isNilOrEmpty(ownedStorageKeys)) { return; } return batchRemoveOwnedValues({ storageValues: ownedStorageKeys, ownershipKey, ownershipNamespace, }); } export async function batchRemoveAllFromNamespace(namespace) { const allDataInNamespace = await localStorage.getAllItems(namespace); const keysToRemove = Object.keys(allDataInNamespace); const dataToRemove = { [namespace]: keysToRemove, }; await batchRemoveFromLocalStorage(dataToRemove); }