@@ -40,14 +40,7 @@ class CacheRegistryDatabase {
4040 getOlderThan ( cacheName , timestamp ) {
4141 const getOlderThanOp = ( store ) => {
4242 const index = store . index ( "cacheNameAndTimestamp" )
43- // Use compound key range: [cacheName, timestamp]
44- const range = IDBKeyRange . bound (
45- [ cacheName , 0 ] , // start of range
46- [ cacheName , timestamp ] , // end of range
47- false , // lowerOpen: include lower bound
48- true // upperOpen: exclude upper bound
49- )
50- const cursorRequest = index . openCursor ( range )
43+ const cursorRequest = index . openCursor ( this . #getTimestampRange( cacheName , timestamp ) )
5144
5245 return this . #cursorRequestToPromise( cursorRequest )
5346 }
@@ -57,10 +50,8 @@ class CacheRegistryDatabase {
5750 getEntryCount ( cacheName ) {
5851 const countOp = ( store ) => {
5952 const index = store . index ( "cacheNameAndTimestamp" )
60- const range = IDBKeyRange . bound (
61- [ cacheName , 0 ] ,
62- [ cacheName , Infinity ]
63- )
53+ const range = this . #getTimestampRange( cacheName )
54+
6455 return this . #requestToPromise( index . count ( range ) )
6556 }
6657 return this . #performOperation( STORE_NAME , countOp , "readonly" )
@@ -69,13 +60,9 @@ class CacheRegistryDatabase {
6960 getOldestEntries ( cacheName , limit ) {
7061 const getOldestOp = ( store ) => {
7162 const index = store . index ( "cacheNameAndTimestamp" )
72- const range = IDBKeyRange . bound (
73- [ cacheName , 0 ] ,
74- [ cacheName , Infinity ]
75- )
76- const cursorRequest = index . openCursor ( range )
63+ const cursorRequest = index . openCursor ( this . #getTimestampRange( cacheName ) )
7764
78- return this . #cursorRequestToPromiseWithLimit ( cursorRequest , limit )
65+ return this . #cursorRequestToPromise ( cursorRequest , limit )
7966 }
8067 return this . #performOperation( STORE_NAME , getOldestOp , "readonly" )
8168 }
@@ -110,13 +97,13 @@ class CacheRegistryDatabase {
11097 } )
11198 }
11299
113- #cursorRequestToPromise( request ) {
100+ #cursorRequestToPromise( request , limit = Infinity ) {
114101 return new Promise ( ( resolve , reject ) => {
115102 const results = [ ]
116103
117104 request . onsuccess = ( event ) => {
118105 const cursor = event . target . result
119- if ( cursor ) {
106+ if ( cursor && results . length < limit ) {
120107 results . push ( cursor . value )
121108 cursor . continue ( )
122109 } else {
@@ -128,22 +115,14 @@ class CacheRegistryDatabase {
128115 } )
129116 }
130117
131- #cursorRequestToPromiseWithLimit( request , limit ) {
132- return new Promise ( ( resolve , reject ) => {
133- const results = [ ]
134-
135- request . onsuccess = ( event ) => {
136- const cursor = event . target . result
137- if ( cursor && results . length < limit ) {
138- results . push ( cursor . value )
139- cursor . continue ( )
140- } else {
141- resolve ( results )
142- }
143- }
144-
145- request . onerror = ( ) => reject ( request . error )
146- } )
118+ #getTimestampRange( cacheName , upperBound = Infinity ) {
119+ // Use compound key range: [cacheName, timestamp]
120+ return IDBKeyRange . bound (
121+ [ cacheName , 0 ] , // start of range
122+ [ cacheName , upperBound ] , // end of range
123+ false , // lowerOpen: include lower bound
124+ true // upperOpen: exclude upper bound
125+ )
147126 }
148127}
149128
0 commit comments