notificationclick_focus.serviceworker.js (1421B)
1 // Any copyright is dedicated to the Public Domain. 2 // http://creativecommons.org/publicdomain/zero/1.0/ 3 4 function promisifyTimerFocus(client, delay) { 5 return new Promise(function (resolve, reject) { 6 setTimeout(function () { 7 client.focus().then(resolve, reject); 8 }, delay); 9 }); 10 } 11 12 onnotificationclick = function (e) { 13 e.waitUntil( 14 self.clients.matchAll().then(function (matchedClients) { 15 if (matchedClients.length === 0) { 16 dump( 17 "********************* CLIENTS LIST EMPTY! Test will timeout! ***********************\n" 18 ); 19 return Promise.resolve(); 20 } 21 22 var immediatePromise = matchedClients[0].focus(); 23 var withinTimeout = promisifyTimerFocus(matchedClients[0], 100); 24 25 var afterTimeout = promisifyTimerFocus(matchedClients[0], 2000).then( 26 function () { 27 throw new Error("Should have failed!"); 28 }, 29 function () { 30 return Promise.resolve(); 31 } 32 ); 33 34 return Promise.all([immediatePromise, withinTimeout, afterTimeout]) 35 .then(function () { 36 matchedClients.forEach(function (client) { 37 client.postMessage({ ok: true }); 38 }); 39 }) 40 .catch(function (ex) { 41 dump("Error " + ex + "\n"); 42 matchedClients.forEach(function (client) { 43 client.postMessage({ ok: false }); 44 }); 45 }); 46 }) 47 ); 48 };