Share the background asset warm pool across idle requests

This commit is contained in:
Jonathan Sykes
2026-10-08 01:40:48 +08:00
parent 173eab8026
commit 524a7b7f1d
2 changed files with 16 additions and 6 deletions

View File

@@ -291,7 +291,7 @@ self.addEventListener('fetch', (e) => {
// ---- Message: handle SKIP_WAITING from the client ----
const playingClients = new Map(), deferredClients = new Set();
let activeLayout = 'classic', layoutReply;
let activeLayout = 'classic', layoutReply, warmTask;
self.addEventListener('message', (e) => {
if (ASSET_SYNC && e.data?.type === 'LAYOUT' && e.source && typeof e.data.value === 'string') {
activeLayout = e.data.value;
@@ -307,10 +307,13 @@ self.addEventListener('message', (e) => {
})());
}
if (ASSET_SYNC && e.data?.type === 'WARM_ASSETS' && !e.data.saveData) {
e.waitUntil((async()=>{
// Boot, activation and multiple tabs can report idle together. Share the
// verified pool so they never download the same missing group twice.
warmTask ||= (async()=>{
const cache=await caches.open(AssetSyncCore.CACHE), m=(await AssetSyncCore.state(cache))?.current;
if(m)await AssetSyncCore.syncAssets(m,{cache,fetchFn:fetch,groups:Object.keys(m.groups).filter(name=>m.groups[name].background)});
})().catch(error=>console.warn('[asset-sync] idle warm interrupted',error.message)));
})().catch(error=>console.warn('[asset-sync] idle warm interrupted',error.message)).finally(()=>{warmTask=null;});
e.waitUntil(warmTask);
}
if (e.data && e.data.type === 'PLAYING' && e.source) {
playingClients.set(e.source.id, !!e.data.value);