localforage: create custom KVStore instance, refactor Instance actions

This commit is contained in:
Alex Gleason
2021-10-20 15:17:02 -05:00
parent 3c5384f318
commit c8cec8fdac
5 changed files with 63 additions and 56 deletions

View File

@@ -0,0 +1,24 @@
import localforage from 'localforage';
// localForage
// https://localforage.github.io/localForage/#settings-api-config
export const KVStore = localforage.createInstance({
name: 'soapbox',
description: 'Soapbox offline data store',
driver: localforage.INDEXEDDB,
storeName: 'keyvaluepairs',
});
// localForage returns 'null' when a key isn't found.
// In the Redux action flow, we want it to fail harder.
KVStore.getItemOrError = key => {
return KVStore.getItem(key).then(value => {
if (value === null) {
throw new Error(`KVStore: null value for key ${key}`);
} else {
return value;
}
});
};
export default KVStore;