1
1
import type { ConfigurableFlush , MaybeRefOrGetter , RemovableRef } from '@vueuse/shared'
2
2
import { toValue } from '@vueuse/shared'
3
+ import { watchPausable } from '@vueuse/core'
3
4
import type { Ref } from 'vue-demi'
4
- import { ref , shallowRef , watch } from 'vue-demi'
5
+ import { ref , shallowRef } from 'vue-demi'
5
6
import { del , get , set , update } from 'idb-keyval'
6
7
7
8
export interface UseIDBOptions extends ConfigurableFlush {
@@ -31,7 +32,12 @@ export interface UseIDBOptions extends ConfigurableFlush {
31
32
* @default true
32
33
*/
33
34
writeDefaults ?: boolean
35
+ }
34
36
37
+ export interface UseIDBKeyvalReturn < T > {
38
+ data : RemovableRef < T >
39
+ isFinished : Ref < boolean >
40
+ set ( value : T ) : Promise < void >
35
41
}
36
42
37
43
/**
@@ -44,7 +50,7 @@ export function useIDBKeyval<T>(
44
50
key : IDBValidKey ,
45
51
initialValue : MaybeRefOrGetter < T > ,
46
52
options : UseIDBOptions = { } ,
47
- ) : { data : RemovableRef < T > ; isFinished : Ref < boolean > } {
53
+ ) : UseIDBKeyvalReturn < T > {
48
54
const {
49
55
flush = 'pre' ,
50
56
deep = true ,
@@ -99,7 +105,21 @@ export function useIDBKeyval<T>(
99
105
}
100
106
}
101
107
102
- watch ( data , ( ) => write ( ) , { flush, deep } )
108
+ const {
109
+ pause : pauseWatch ,
110
+ resume : resumeWatch ,
111
+ } = watchPausable ( data , ( ) => write ( ) , { flush, deep } )
103
112
104
- return { isFinished, data : data as RemovableRef < T > }
113
+ async function setData ( value : T ) : Promise < void > {
114
+ pauseWatch ( )
115
+ data . value = value
116
+ await write ( )
117
+ resumeWatch ( )
118
+ }
119
+
120
+ return {
121
+ set : setData ,
122
+ isFinished,
123
+ data : data as RemovableRef < T > ,
124
+ }
105
125
}
0 commit comments