Re: [ServiceWorker] setTimeout/setInterval are unreliable in ServiceWorkers (#838)

Thanks for the offer of the side-bar Jake but I use neither Twatter or FacePlant. I did email you not so long ago but just assumed that you're pretty busy or I ended up in junk-email. Either way, when it comes to "this issue"  it was all over as soon as @mkruisselbrink posted their use case.

Don't get me wrong, I've learned/am learning a lot from your code example and think this thread should stay open as a platform for displaying and viewing it, but as an issue this could've been closed 8 days ago.

Anyway, I led everyone astray by saying it was a Chrome issue. My bad. Both Chrome and FF respond (fire fetch events) to every "fetch" in the main thread.  Both Chrome and FF only fire a fetch event once for an IMG element that is added/removed from the DOM many times. Probably documented like that somewhere?

But while I'm here and back on topic: -

When is the response cached? When the Fetch Event is triggered? PreventDefault() to stop it? Do I need cache: no-store on my INIT parameter to my FETCH call? Does it happen at event.respondWith() time?

If I understood @mkruisselbrink 's strategy correctly, the goal was to deliver data as fresh as possible and only go to cache if the network is slow. BUT even then leave the network fetch running to update the cache whenever it finishes.

Without something like INIT "no-cache" aren't we at risk of the FETCH just reading cache anyway? I'm not saying your example was meant to be complete and I can't break it (or really understand it :-) see below) but in my test example I was manually updating cache with a PUT(). Silly me?

Also, with the (Service Worker acting as a Shared Worker?) and handling fetches from all tabs from the same source, I assume a second page registering the same SW is just like the documented skipWaiting() replacement process? But what happened to the fetchEvent.clientID field?

Anyway, here's your code again with some comments to try to help me. I'm sure every other reader immediately understood your code so I apologise to them now: -

function wait(ms) {
  return new Promise(resolve => {
    setTimeout(resolve, ms);
  });
}
 
function promiseAny(promises) {
  return new Promise((resolve, reject) => {

    // Kick off all Promises (thenable) in array
    // p is each element of promises[]
    promises = promises.map(p => Promise.resolve(p));

    // For first promise, queue if settled "resolved" call Fetch's resolve
    promises.forEach(p => p.then(resolve)); // Didn't we just force all "resolved"?
                                            // No, we effectively yeilded to
                                            // allow Array.map() to iterate?
           // This works out who won and 
           // then resolves this promise to that.

    // a is "previous" b is "current" (p0,p1)
    // Call Fetch's reject if none resolved 
    // Bit lost here :-( Hardcoding/relying on promises.length == 2 ?
    // Does promises.reduce() method return a promise that is .catch(able) here or null?
    promises.reduce((a, b) => a.catch(b)).catch(() => reject(Error("All failed")));

  });
};


---
Reply to this email directly or view it on GitHub:
https://github.com/slightlyoff/ServiceWorker/issues/838#issuecomment-191226678

Received on Wednesday, 2 March 2016 12:45:43 UTC