1
1
// API for Tauri or web storage
2
- import { LazyStore } from '@tauri-apps/plugin-store' ;
2
+ import { getStore , LazyStore , Store } from '@tauri-apps/plugin-store' ;
3
3
import localforage from 'localforage' ;
4
4
import { Dispatch , SetStateAction , useCallback , useEffect , useRef , useState } from 'react' ;
5
5
import { useTauriContext } from './TauriProvider' ;
@@ -21,40 +21,40 @@ function getTauriStore(filename: string): LazyStore {
21
21
22
22
// returns an API to get a item, set an item from a specific category of data
23
23
// why? we don't to have loading variable for multiple values
24
- export function createStorage ( storeName : string ) {
24
+ export function createStorage ( storePath : string ) {
25
25
let loading = useTauriContext ( ) . loading ;
26
26
const [ data , setData ] = useState < Record < string , any > > ( ) ;
27
27
loading = loading || data === undefined ;
28
28
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 ) ;
32
30
const timeoutRef = useRef < number > ( undefined ) ;
33
31
34
32
// load data
35
33
useEffect ( ( ) => {
36
34
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 ) ;
49
48
}
50
- )
51
- }
52
-
49
+ } catch ( e ) {
50
+ console . error ( e ) ;
51
+ }
52
+ } ) ( ) ;
53
53
} else {
54
- localforage . getItem ( storeName , ( err , value ) => {
54
+ localforage . getItem ( storePath , ( err , value ) => {
55
55
// make store a {} again in catch
56
56
if ( err !== undefined && value === null || Array . isArray ( value ) ) {
57
- localforage . setItem ( storeName , { } , ( err , val ) => {
57
+ localforage . setItem ( storePath , { } , ( err , val ) => {
58
58
if ( err !== null && err !== undefined ) {
59
59
return alert ( 'cannot store data, application will not work as intended' ) ;
60
60
}
@@ -65,7 +65,7 @@ export function createStorage(storeName: string) {
65
65
}
66
66
} ) ;
67
67
}
68
- } , [ storeName ] ) ;
68
+ } , [ storePath ] ) ;
69
69
70
70
const setItem = useCallback ( ( key : string , newValueOrHandler : Dispatch < SetStateAction < any > > ) => {
71
71
if ( loading ) return ;
@@ -87,12 +87,12 @@ export function createStorage(storeName: string) {
87
87
timeoutRef . current = window . setTimeout ( ( ) => fileStoreRef . current ! . save ( ) , SAVE_DELAY )
88
88
} ) ;
89
89
} else {
90
- timeoutRef . current = window . setTimeout ( ( ) => localforage . setItem ( storeName , newData ) , SAVE_DELAY ) ;
90
+ timeoutRef . current = window . setTimeout ( ( ) => localforage . setItem ( storePath , newData ) , SAVE_DELAY ) ;
91
91
}
92
92
}
93
93
return newData ;
94
94
} ) ;
95
- } , [ storeName , loading , fileStoreRef , localDataRef , timeoutRef ] ) ;
95
+ } , [ storePath , loading , fileStoreRef , localDataRef , timeoutRef ] ) ;
96
96
97
97
const getItem = useCallback ( ( key : string , defaultValue : object ) => {
98
98
if ( loading || data === undefined ) return defaultValue ;
0 commit comments