sw-update-ui.js (1103B)
1 importScripts('/resources/testharness.js'); 2 importScripts('sw-helpers.js'); 3 4 async function updateUI(event) { 5 let updateParams = []; 6 switch (event.registration.id) { 7 case 'update-once': 8 updateParams = [{title: 'Title1'}]; 9 break; 10 case 'update-twice': 11 updateParams = [{title: 'Title1'}, {title: 'Title2'}]; 12 break; 13 } 14 15 return Promise.all(updateParams.map(param => event.updateUI(param))) 16 .then(() => 'update success') 17 .catch(e => e.name); 18 } 19 20 self.addEventListener('backgroundfetchsuccess', event => { 21 if (event.registration.id === 'update-inactive') { 22 // Post an async task before calling updateUI from the inactive event. 23 // Any async behaviour outside `waitUntil` should mark the event as 24 // inactive, and subsequent calls to `updateUI` should fail. 25 new Promise(r => step_timeout(r, 0)) 26 .then(() => event.updateUI({ title: 'New title' })) 27 .catch(e => sendMessageToDocument({ update: e.name })); 28 return; 29 } 30 31 event.waitUntil(updateUI(event) 32 .then(update => sendMessageToDocument({ update }))); 33 });