Skip to content

Commit 64a4123

Browse files
committed
fix: tauri store
1 parent e069f0c commit 64a4123

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

src/tauri/storage.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// API for Tauri or web storage
2-
import { LazyStore } from '@tauri-apps/plugin-store';
2+
import { getStore, LazyStore, Store } from '@tauri-apps/plugin-store';
33
import localforage from 'localforage';
44
import { Dispatch, SetStateAction, useCallback, useEffect, useRef, useState } from 'react';
55
import { useTauriContext } from './TauriProvider';
@@ -21,40 +21,40 @@ function getTauriStore(filename: string): LazyStore {
2121

2222
// returns an API to get a item, set an item from a specific category of data
2323
// why? we don't to have loading variable for multiple values
24-
export function createStorage(storeName: string) {
24+
export function createStorage(storePath: string) {
2525
let loading = useTauriContext().loading;
2626
const [data, setData] = useState<Record<string, any>>();
2727
loading = loading || data === undefined;
2828
const localDataRef = useRef(null);
29-
const fileStoreRef = useRef<LazyStore | null>(
30-
RUNNING_IN_TAURI ? getTauriStore(storeName) : null
31-
);
29+
const fileStoreRef = useRef<Store | null>(null);
3230
const timeoutRef = useRef<number>(undefined);
3331

3432
// load data
3533
useEffect(() => {
3634
if (RUNNING_IN_TAURI) {
37-
if (fileStoreRef.current === null) console.error('fileStoreRef is undefined');
38-
else {
39-
fileStoreRef.current.get('data').then(
40-
value => {
41-
if (value === undefined || value === null) {
42-
const newValue = {};
43-
fileStoreRef.current!.set('data', newValue)
44-
.then(() => setData(newValue));
45-
} else {
46-
console.log(`value is undefined? ${value === undefined}`);
47-
setData(value);
48-
}
35+
(async () => {
36+
try {
37+
const store = await getStore(storePath);
38+
if (store === null) throw new Error('invalid path for store');
39+
fileStoreRef.current = store;
40+
const value = await fileStoreRef.current.get<Record<string, any>>('data');
41+
if (value === undefined) {
42+
const newValue = {};
43+
await fileStoreRef.current!.set('data', newValue);
44+
setData(newValue);
45+
} else {
46+
console.log(`value is undefined? ${JSON.stringify(value)}`);
47+
setData(value);
4948
}
50-
)
51-
}
52-
49+
} catch (e) {
50+
console.error(e);
51+
}
52+
})();
5353
} else {
54-
localforage.getItem(storeName, (err, value) => {
54+
localforage.getItem(storePath, (err, value) => {
5555
// make store a {} again in catch
5656
if (err !== undefined && value === null || Array.isArray(value)) {
57-
localforage.setItem(storeName, {}, (err, val) => {
57+
localforage.setItem(storePath, {}, (err, val) => {
5858
if (err !== null && err !== undefined) {
5959
return alert('cannot store data, application will not work as intended');
6060
}
@@ -65,7 +65,7 @@ export function createStorage(storeName: string) {
6565
}
6666
});
6767
}
68-
}, [storeName]);
68+
}, [storePath]);
6969

7070
const setItem = useCallback((key: string, newValueOrHandler: Dispatch<SetStateAction<any>>) => {
7171
if (loading) return;
@@ -87,12 +87,12 @@ export function createStorage(storeName: string) {
8787
timeoutRef.current = window.setTimeout(() => fileStoreRef.current!.save(), SAVE_DELAY)
8888
});
8989
} else {
90-
timeoutRef.current = window.setTimeout(() => localforage.setItem(storeName, newData), SAVE_DELAY);
90+
timeoutRef.current = window.setTimeout(() => localforage.setItem(storePath, newData), SAVE_DELAY);
9191
}
9292
}
9393
return newData;
9494
});
95-
}, [storeName, loading, fileStoreRef, localDataRef, timeoutRef]);
95+
}, [storePath, loading, fileStoreRef, localDataRef, timeoutRef]);
9696

9797
const getItem = useCallback((key: string, defaultValue: object) => {
9898
if (loading || data === undefined) return defaultValue;

0 commit comments

Comments
 (0)