bigbuffet-rw/app/soapbox/storage/kv_store.ts

29 lines
769 B
TypeScript
Raw Normal View History

import localforage from 'localforage';
2022-03-18 14:04:08 -07:00
interface IKVStore extends LocalForage {
getItemOrError?: (key: string) => Promise<any>,
}
// localForage
// https://localforage.github.io/localForage/#settings-api-config
2022-03-18 14:04:08 -07:00
export const KVStore: IKVStore = 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.
2022-03-15 06:48:18 -07:00
KVStore.getItemOrError = (key: string) => {
return KVStore.getItem(key).then(value => {
if (value === null) {
throw new Error(`KVStore: null value for key ${key}`);
} else {
return value;
}
});
};
export default KVStore;