sw-helpers.js (919B)
1 // The source to post setup and completion results to. 2 let source = null; 3 4 function sendMessageToDocument(msg) { 5 source.postMessage(msg); 6 } 7 8 // This is needed to create a local javascript object identical to the 9 // one returned by a BackgroundFetchEvent, so that it can be serialized 10 // and transmitted from the service worker context to the document. 11 function cloneRegistration(registration) { 12 function deepCopy(src) { 13 if (typeof src !== 'object' || src === null) 14 return src; 15 var dst = Array.isArray(src) ? [] : {}; 16 for (var property in src) { 17 if (typeof src[property] === 'function') 18 continue; 19 dst[property] = deepCopy(src[property]); 20 } 21 return dst; 22 } 23 24 return deepCopy(registration); 25 } 26 27 // Notify the document that the SW is registered and ready. 28 self.addEventListener('message', event => { 29 source = event.source; 30 sendMessageToDocument('ready'); 31 });