2021-10-20 13:17:02 -07:00
|
|
|
import localforage from 'localforage';
|
|
|
|
|
2022-03-18 14:04:08 -07:00
|
|
|
interface IKVStore extends LocalForage {
|
2023-02-15 13:26:27 -08:00
|
|
|
getItemOrError: (key: string) => Promise<any>
|
2022-03-18 14:04:08 -07:00
|
|
|
}
|
|
|
|
|
2021-10-20 13:17:02 -07:00
|
|
|
// localForage
|
|
|
|
// https://localforage.github.io/localForage/#settings-api-config
|
2022-03-23 19:43:36 -07:00
|
|
|
export const KVStore = localforage.createInstance({
|
2021-10-20 13:17:02 -07:00
|
|
|
name: 'soapbox',
|
|
|
|
description: 'Soapbox offline data store',
|
|
|
|
driver: localforage.INDEXEDDB,
|
|
|
|
storeName: 'keyvaluepairs',
|
2022-03-23 19:43:36 -07:00
|
|
|
}) as IKVStore;
|
2021-10-20 13:17:02 -07:00
|
|
|
|
|
|
|
// 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) => {
|
2021-10-20 13:17:02 -07:00
|
|
|
return KVStore.getItem(key).then(value => {
|
|
|
|
if (value === null) {
|
|
|
|
throw new Error(`KVStore: null value for key ${key}`);
|
|
|
|
} else {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export default KVStore;
|