11import { getMany } from 'idb-keyval'
2- import { cachedObj , debugLog , expire , reactCache , verifyEntry } from '../shared'
2+ import { addListener , cachedObj , debugLog , dispatch , expire , reactCache , setProperty , verifyEntry } from '../shared'
33
44type valueTypes = {
55 data : cachedObj [ 'data' ] ,
@@ -22,6 +22,7 @@ export function get<
2222> (
2323 cache : reactCache ,
2424 store : Parameters < typeof getMany > [ 1 ] ,
25+ id : string ,
2526 rerender : ( ) => void ,
2627 keyOrKeys : K ,
2728 loader ?: ( missingKeys : string [ ] ) => Promise < void > ,
@@ -34,6 +35,7 @@ export function get<
3435> (
3536 cache : reactCache ,
3637 store : Parameters < typeof getMany > [ 1 ] ,
38+ id : string ,
3739 rerender : ( ) => void ,
3840 keyOrKeys : K ,
3941 loader ?: ( missingKeys : string [ ] ) => Promise < void > ,
@@ -47,6 +49,7 @@ export function get<
4749> (
4850 cache : reactCache ,
4951 store : Parameters < typeof getMany > [ 1 ] ,
52+ id : string ,
5053 rerender : ( ) => void ,
5154 keyOrKeys : K ,
5255 loader ?: ( missingKeys : string [ ] ) => Promise < void > ,
@@ -55,24 +58,25 @@ export function get<
5558) : getReturn < K , T > {
5659 const keys = ( Array . isArray ( keyOrKeys ) ? keyOrKeys : [ keyOrKeys ] ) as string [ ]
5760
61+ addListener ( cache , keys , id , rerender )
62+
5863 const values : Record < string , getValue < T > > = { }
5964 const missing : string [ ] = [ ]
6065
6166 keys . forEach ( key => {
62- if ( ! verifyEntry ( cache [ key ] , expire ) ) {
67+ if ( ! cache [ key ] . promise && ! verifyEntry ( cache [ key ] , expire ) ) {
6368 missing . push ( key )
6469 }
6570 if ( returnType === 'obj' ) {
66- values [ key ] = cache [ key ] ? .obj as getValue < T >
71+ values [ key ] = cache [ key ] . obj as getValue < T >
6772 } else {
68- values [ key ] = cache [ key ] ? .obj ?. data as getValue < T >
73+ values [ key ] = cache [ key ] . obj ?. data as getValue < T >
6974 }
7075 } )
7176
7277 if ( missing . length ) {
7378 debugLog ( 'Get from idb: %s' , missing . join ( ', ' ) )
7479
75- let hit = false
7680 const idbPromise = getMany ( missing , store ) . then (
7781 obj => {
7882 debugLog . enabled && debugLog (
@@ -81,15 +85,18 @@ export function get<
8185 )
8286
8387 const stillMissing : string [ ] = [ ]
88+ const hit : string [ ] = [ ]
8489 obj . forEach ( ( obj , i ) => {
8590 if ( verifyEntry ( { obj } , expire ) ) {
86- hit = true
87- cache [ missing [ i ] ] = { obj }
91+ setProperty ( cache , [ missing [ i ] , 'obj' ] , obj )
92+ hit . push ( missing [ i ] )
8893 } else {
8994 stillMissing . push ( missing [ i ] )
9095 }
9196 } )
9297
98+ dispatch ( cache , hit )
99+
93100 const loaderPromise = typeof loader === 'function' ? loader ( stillMissing ) : undefined
94101
95102 if ( loaderPromise ) {
@@ -106,11 +113,8 @@ export function get<
106113 } ,
107114 )
108115
109- idbPromise . then ( ( ) => hit && rerender ( ) )
110-
111116 missing . forEach ( ( key , i ) => {
112- cache [ key ] = cache [ key ] ?? { }
113- cache [ key ] . promise = idbPromise . then ( values => values [ i ] )
117+ setProperty ( cache , [ key , 'promise' ] , idbPromise . then ( values => values [ i ] ) )
114118 } )
115119 }
116120
0 commit comments