@@ -10,6 +10,7 @@ import {Adapter} from './adapter';
1010import { CacheState , Debuggable , DebugIdleState , DebugState , DebugVersion , NormalizedUrl , UpdateCacheStatus , UpdateSource } from './api' ;
1111import { AppVersion } from './app-version' ;
1212import { Database } from './database' ;
13+ import { CacheTable } from './db-cache' ;
1314import { DebugHandler } from './debug' ;
1415import { errorToString } from './error' ;
1516import { IdleScheduler } from './idle' ;
@@ -110,6 +111,9 @@ export class Driver implements Debuggable, UpdateSource {
110111
111112 debugger : DebugHandler ;
112113
114+ // A promise resolving to the control DB table.
115+ private controlTable = this . db . open ( 'control' ) ;
116+
113117 constructor (
114118 private scope : ServiceWorkerGlobalScope , private adapter : Adapter , private db : Database ) {
115119 // Set up all the event handlers that the SW needs.
@@ -517,8 +521,7 @@ export class Driver implements Debuggable, UpdateSource {
517521 // the SW has run or the DB state has been wiped or is inconsistent. In that case,
518522 // load a fresh copy of the manifest and reset the state from scratch.
519523
520- // Open up the DB table.
521- const table = await this . db . open ( 'control' ) ;
524+ const table = await this . controlTable ;
522525
523526 // Attempt to load the needed state from the DB. If this fails, the catch {} block
524527 // will populate these variables with freshly constructed values.
@@ -568,12 +571,7 @@ export class Driver implements Debuggable, UpdateSource {
568571
569572 // Schedule cleaning up obsolete caches in the background.
570573 this . idle . schedule ( 'init post-load (cleanup)' , async ( ) => {
571- try {
572- await this . cleanupCaches ( ) ;
573- } catch ( err ) {
574- // Nothing to do - cleanup failed. Just log it.
575- this . debugger . log ( err , 'cleanupCaches @ init post-load' ) ;
576- }
574+ await this . cleanupCaches ( ) ;
577575 } ) ;
578576
579577 // Initialize the `versions` map by setting each hash to a new `AppVersion` instance
@@ -901,8 +899,7 @@ export class Driver implements Debuggable, UpdateSource {
901899 * Synchronize the existing state to the underlying database.
902900 */
903901 private async sync ( ) : Promise < void > {
904- // Open up the DB table.
905- const table = await this . db . open ( 'control' ) ;
902+ const table = await this . controlTable ;
906903
907904 // Construct a serializable map of hashes to manifests.
908905 const manifests : ManifestMap = { } ;
@@ -931,56 +928,45 @@ export class Driver implements Debuggable, UpdateSource {
931928 }
932929
933930 async cleanupCaches ( ) : Promise < void > {
934- // Query for all currently active clients, and list the client ids. This may skip
935- // some clients in the browser back-forward cache, but not much can be done about
936- // that.
937- const activeClients : ClientId [ ] =
938- ( await this . scope . clients . matchAll ( ) ) . map ( client => client . id ) ;
939-
940- // A simple list of client ids that the SW has kept track of. Subtracting
941- // activeClients from this list will result in the set of client ids which are
942- // being tracked but are no longer used in the browser, and thus can be cleaned up.
943- const knownClients : ClientId [ ] = Array . from ( this . clientVersionMap . keys ( ) ) ;
944-
945- // Remove clients in the clientVersionMap that are no longer active.
946- knownClients . filter ( id => activeClients . indexOf ( id ) === - 1 )
947- . forEach ( id => this . clientVersionMap . delete ( id ) ) ;
948-
949- // Next, determine the set of versions which are still used. All others can be
950- // removed.
951- const usedVersions = new Set < string > ( ) ;
952- this . clientVersionMap . forEach ( ( version , _ ) => usedVersions . add ( version ) ) ;
953-
954- // Collect all obsolete versions by filtering out used versions from the set of all versions.
955- const obsoleteVersions =
956- Array . from ( this . versions . keys ( ) )
957- . filter ( version => ! usedVersions . has ( version ) && version !== this . latestHash ) ;
958-
959- // Remove all the versions which are no longer used.
960- await obsoleteVersions . reduce ( async ( previous , version ) => {
961- // Wait for the other cleanup operations to complete.
962- await previous ;
963-
964- // Try to get past the failure of one particular version to clean up (this
965- // shouldn't happen, but handle it just in case).
966- try {
967- // Get ahold of the AppVersion for this particular hash.
968- const instance = this . versions . get ( version ) ! ;
931+ try {
932+ // Query for all currently active clients, and list the client IDs. This may skip some clients
933+ // in the browser back-forward cache, but not much can be done about that.
934+ const activeClients =
935+ new Set < ClientId > ( ( await this . scope . clients . matchAll ( ) ) . map ( client => client . id ) ) ;
969936
970- // Delete it from the canonical map.
971- this . versions . delete ( version ) ;
937+ // A simple list of client IDs that the SW has kept track of. Subtracting `activeClients` from
938+ // this list will result in the set of client IDs which are being tracked but are no longer
939+ // used in the browser, and thus can be cleaned up.
940+ const knownClients : ClientId [ ] = Array . from ( this . clientVersionMap . keys ( ) ) ;
972941
973- // Clean it up.
974- await instance . cleanup ( ) ;
975- } catch ( err ) {
976- // Oh well? Not much that can be done here. These caches will be removed when
977- // the SW revs its format version, which happens from time to time.
978- this . debugger . log ( err , `cleanupCaches - cleanup ${ version } ` ) ;
979- }
980- } , Promise . resolve ( ) ) ;
942+ // Remove clients in the `clientVersionMap` that are no longer active.
943+ const obsoleteClients = knownClients . filter ( id => ! activeClients . has ( id ) ) ;
944+ obsoleteClients . forEach ( id => this . clientVersionMap . delete ( id ) ) ;
981945
982- // Commit all the changes to the saved state.
983- await this . sync ( ) ;
946+ // Next, determine the set of versions which are still used. All others can be removed.
947+ const usedVersions = new Set ( this . clientVersionMap . values ( ) ) ;
948+
949+ // Collect all obsolete versions by filtering out used versions from the set of all versions.
950+ const obsoleteVersions =
951+ Array . from ( this . versions . keys ( ) )
952+ . filter ( version => ! usedVersions . has ( version ) && version !== this . latestHash ) ;
953+
954+ // Remove all the versions which are no longer used.
955+ obsoleteVersions . forEach ( version => this . versions . delete ( version ) ) ;
956+
957+ // Commit all the changes to the saved state.
958+ await this . sync ( ) ;
959+
960+ // Delete all caches that are no longer needed.
961+ const allCaches = await this . adapter . caches . keys ( ) ;
962+ const usedCaches = new Set ( await this . getCacheNames ( ) ) ;
963+ const cachesToDelete = allCaches . filter ( name => ! usedCaches . has ( name ) ) ;
964+ await Promise . all ( cachesToDelete . map ( name => this . adapter . caches . delete ( name ) ) ) ;
965+ } catch ( err ) {
966+ // Oh well? Not much that can be done here. These caches will be removed on the next attempt
967+ // or when the SW revs its format version, which happens from time to time.
968+ this . debugger . log ( err , 'cleanupCaches' ) ;
969+ }
984970 }
985971
986972 /**
@@ -1150,4 +1136,12 @@ export class Driver implements Debuggable, UpdateSource {
11501136 } ) ;
11511137 }
11521138 }
1139+
1140+ private async getCacheNames ( ) : Promise < string [ ] > {
1141+ const controlTable = await this . controlTable as CacheTable ;
1142+ const appVersions = Array . from ( this . versions . values ( ) ) ;
1143+ const appVersionCacheNames =
1144+ await Promise . all ( appVersions . map ( version => version . getCacheNames ( ) ) ) ;
1145+ return [ controlTable . cacheName ] . concat ( ...appVersionCacheNames ) ;
1146+ }
11531147}
0 commit comments