- From: Richard Maher <notifications@github.com>
- Date: Fri, 11 Mar 2016 05:05:59 -0800
- To: slightlyoff/ServiceWorker <ServiceWorker@noreply.github.com>
- Message-ID: <slightlyoff/ServiceWorker/issues/844/195356225@github.com>
Sorry registers Program Counter (PC) and Stack Pointer (SP) betray my age and are pretty useless in OO interpretive programming but it was a metaphor. Here's the code: 'use strict'; console.log("Started", self); function optimusConor(event) { console.log(Date(), 'Handling fetch event for', event.request.url); const MAXDETENT = 300, NOCACHE = {cache : "no-cache"}, WORKCACHE = "FAC$CACHE"; var timerId = 0, replied = false; function reply(result){ console.log("In reply", replied); if (replied) return; console.log("After reply", replied); replied = true; event.respondWith(result); console.log("Replied : ", replied); } function wait(ms){ return new Promise( function(resolve){ timerId = setTimeout(function(){resolve()}, ms); }); } var inBuff = new Promise( function(resolve,reject){ caches.match(event.request.clone()) .then(response => response === undefined ? reject() : resolve(response)); } ).catch(()=>{}); event.waitUntil(new Promise( function(allDone){ console.log("in promise"); fetch(event.request.clone(), NOCACHE) .then(response => { console.log('Response from network is:', response); if (response.ok) { clearTimeout(timerId); console.log("Storing cache"); reply(response); caches.open(WORKCACHE).then(cache => { cache.put(event.request, response.clone()); }) } allDone(); }) .catch(function(error) { console.log('Fetching failed:', error); allDone(); inBuff.then(response => reply(response)) .catch(() => {console.log("Error3 is: ", error)}); }) ; wait(MAXDETENT).then(inBuff.then(response => reply(response))); })); } self.addEventListener('install', function(event) { self.skipWaiting(); console.log('Installed', event); }); self.addEventListener('activate', function(event) { self.addEventListener('fetch', optimusConor); console.log('Activated', event); }); --- Reply to this email directly or view it on GitHub: https://github.com/slightlyoff/ServiceWorker/issues/844#issuecomment-195356225
Received on Friday, 11 March 2016 13:06:32 UTC