| 60 | } |
| 61 | |
| 62 | constructor( |
| 63 | private scope: ServiceWorkerGlobalScope, |
| 64 | private adapter: Adapter, |
| 65 | private database: Database, |
| 66 | idle: IdleScheduler, |
| 67 | private debugHandler: DebugHandler, |
| 68 | readonly manifest: Manifest, |
| 69 | readonly manifestHash: string, |
| 70 | ) { |
| 71 | this.indexUrl = this.adapter.normalizeUrl(this.manifest.index); |
| 72 | // The hashTable within the manifest is an Object - convert it to a Map for easier lookups. |
| 73 | Object.keys(manifest.hashTable).forEach((url) => { |
| 74 | this.hashTable.set(adapter.normalizeUrl(url), manifest.hashTable[url]); |
| 75 | }); |
| 76 | |
| 77 | // Process each `AssetGroup` declared in the manifest. Each declared group gets an `AssetGroup` |
| 78 | // instance created for it, of a type that depends on the configuration mode. |
| 79 | const assetCacheNamePrefix = `${manifestHash}:assets`; |
| 80 | this.assetGroups = (manifest.assetGroups || []).map((config) => { |
| 81 | // Check the caching mode, which determines when resources will be fetched/updated. |
| 82 | switch (config.installMode) { |
| 83 | case 'prefetch': |
| 84 | return new PrefetchAssetGroup( |
| 85 | scope, |
| 86 | adapter, |
| 87 | idle, |
| 88 | config, |
| 89 | this.hashTable, |
| 90 | database, |
| 91 | assetCacheNamePrefix, |
| 92 | ); |
| 93 | case 'lazy': |
| 94 | return new LazyAssetGroup( |
| 95 | scope, |
| 96 | adapter, |
| 97 | idle, |
| 98 | config, |
| 99 | this.hashTable, |
| 100 | database, |
| 101 | assetCacheNamePrefix, |
| 102 | ); |
| 103 | } |
| 104 | }); |
| 105 | |
| 106 | // Process each `DataGroup` declared in the manifest. |
| 107 | this.dataGroups = (manifest.dataGroups || []).map( |
| 108 | (config) => |
| 109 | new DataGroup(scope, adapter, config, database, debugHandler, `${config.version}:data`), |
| 110 | ); |
| 111 | |
| 112 | // Create `include`/`exclude` RegExps for the `navigationUrls` declared in the manifest. |
| 113 | const includeUrls = manifest.navigationUrls.filter((spec) => spec.positive); |
| 114 | const excludeUrls = manifest.navigationUrls.filter((spec) => !spec.positive); |
| 115 | this.navigationUrls = { |
| 116 | include: includeUrls.map((spec) => new RegExp(spec.regex)), |
| 117 | exclude: excludeUrls.map((spec) => new RegExp(spec.regex)), |
| 118 | }; |
| 119 | } |