@theoplayer/react-native-engage
Version:
Engage connector for @theoplayer/react-native
48 lines (47 loc) • 1.41 kB
JavaScript
;
import { MMKV } from 'react-native-mmkv';
import { DefaultCluster } from '../cluster/DefaultCluster';
const TAG = 'EngageStorage';
const storage = new MMKV({
id: 'engage-storage'
});
export async function readCluster(client, clusterType) {
try {
return createCluster(client, clusterType, storage.getString(storageKeyForCluster(clusterType)));
} catch (e) {
console.warn(TAG, `Failed to retrieve cluster data from storage: ${e}`);
}
return createCluster(client, clusterType);
}
function createCluster(client, clusterType, json) {
if (json) {
try {
const cluster = JSON.parse(json);
return new DefaultCluster(client, clusterType, cluster.entities);
} catch (e) {
console.warn(TAG, `Failed to retrieve cluster data from storage: ${e}`);
}
}
return new DefaultCluster(client, clusterType);
}
export async function storeCluster(cluster) {
const key = storageKeyForCluster(cluster.type);
try {
const json = JSON.stringify({
...cluster,
engageClient: undefined
});
storage.set(key, json);
return true;
} catch (e) {
console.warn(TAG, `Failed to store cluster data ${key}: ${e}`);
return false;
}
}
export async function removeCluster(type) {
storage.delete(storageKeyForCluster(type));
}
function storageKeyForCluster(type) {
return `engage/cluster/${type}`;
}
//# sourceMappingURL=Storage.js.map