@@ -115,6 +115,7 @@ export class IDBCache implements AsyncStorage {
115115 private cacheBuster : string ;
116116 private debug : boolean ;
117117 private maxTotalChunks ?: number ;
118+ private priority : "normal" | "low" = "normal" ;
118119
119120 constructor ( config : IDBCacheConfig ) {
120121 const {
@@ -127,6 +128,7 @@ export class IDBCache implements AsyncStorage {
127128 cleanupInterval = CLEANUP_INTERVAL ,
128129 pbkdf2Iterations = DEFAULT_PBKDF2_ITERATIONS ,
129130 maxTotalChunks,
131+ priority = "normal" ,
130132 } = config ;
131133
132134 this . storeName = "cache" ;
@@ -139,6 +141,7 @@ export class IDBCache implements AsyncStorage {
139141 this . pbkdf2Iterations = pbkdf2Iterations ;
140142 this . maxTotalChunks = maxTotalChunks ;
141143 this . pendingRequests = new Map ( ) ;
144+ this . priority = priority ;
142145
143146 if ( ! window . indexedDB )
144147 throw new DatabaseError ( "IndexedDB is not supported." ) ;
@@ -230,6 +233,9 @@ export class IDBCache implements AsyncStorage {
230233 public async cleanup ( ) : Promise < void > {
231234 try {
232235 const db = await this . dbReadyPromise ;
236+ if ( this . priority === "low" ) {
237+ await waitForAnimationFrame ( ) ;
238+ }
233239 const transaction = db . transaction ( this . storeName , "readwrite" ) ;
234240 const store = transaction . store ;
235241 const timestampIndex = store . index ( "byTimestamp" ) ;
@@ -407,7 +413,9 @@ export class IDBCache implements AsyncStorage {
407413 if ( ! this . dbReadyPromise ) return null ;
408414 await this . ensureWorkerInitialized ( ) ;
409415
410- await waitForAnimationFrame ( ) ;
416+ if ( this . priority === "low" ) {
417+ await waitForAnimationFrame ( ) ;
418+ }
411419 const db = await this . dbReadyPromise ;
412420 const baseKey = await deterministicUUID ( `${ this . cacheKey } :${ itemKey } ` ) ;
413421 const now = Date . now ( ) ;
@@ -544,11 +552,16 @@ export class IDBCache implements AsyncStorage {
544552 if ( ! this . dbReadyPromise ) return ;
545553 await this . ensureWorkerInitialized ( ) ;
546554
547- await waitForAnimationFrame ( ) ;
555+ if ( this . priority === "low" ) {
556+ await waitForAnimationFrame ( ) ;
557+ }
548558 const db = await this . dbReadyPromise ;
549559 const baseKey = await deterministicUUID ( `${ this . cacheKey } :${ itemKey } ` ) ;
550560 const expirationTimestamp = Date . now ( ) + this . gcTime ;
551561
562+ if ( this . priority === "low" ) {
563+ await waitForAnimationFrame ( ) ;
564+ }
552565 const existingChunkKeys = await getAllChunkKeysForBaseKey (
553566 db ,
554567 this . storeName ,
@@ -570,15 +583,22 @@ export class IDBCache implements AsyncStorage {
570583 const chunk = value . slice ( i , i + this . chunkSize ) ;
571584 const chunkIndex = Math . floor ( i / this . chunkSize ) ;
572585
586+ if ( this . priority === "low" ) {
587+ await waitForAnimationFrame ( ) ;
588+ }
573589 const chunkHash = await deterministicUUID (
574- `${ this . cacheKey } :${ this . cacheBuster } :${ chunk } `
590+ `${ this . cacheKey } :${ this . cacheBuster } :${ chunk } ` ,
591+ this . priority
575592 ) ;
576593 const chunkKey = generateChunkKey ( baseKey , chunkIndex , chunkHash ) ;
577594 newChunkKeys . add ( chunkKey ) ;
578595
579596 const isLastChunk = chunkIndex === totalChunks - 1 ;
580597
581598 if ( existingChunkKeysSet . has ( chunkKey ) ) {
599+ if ( this . priority === "low" ) {
600+ await waitForAnimationFrame ( ) ;
601+ }
582602 const existingChunk = await db . get ( this . storeName , chunkKey ) ;
583603 if (
584604 existingChunk &&
@@ -636,7 +656,9 @@ export class IDBCache implements AsyncStorage {
636656 }
637657
638658 await Promise . all ( operationPromises ) ;
639-
659+ if ( this . priority === "low" ) {
660+ await waitForAnimationFrame ( ) ;
661+ }
640662 await tx . done ;
641663
642664 const duration = Date . now ( ) - startTime ;
0 commit comments