whimsical-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ru...@apache.org
Subject [whimsy] branch master updated: respond from cache if server isn't fast enough
Date Wed, 22 Nov 2017 20:13:34 GMT
This is an automated email from the ASF dual-hosted git repository.

rubys pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/whimsy.git


The following commit(s) were added to refs/heads/master by this push:
     new aacd960  respond from cache if server isn't fast enough
aacd960 is described below

commit aacd960bdbe22a22d7d5ddb19bad1576e8111ea7
Author: Sam Ruby <rubys@intertwingly.net>
AuthorDate: Wed Nov 22 15:13:19 2017 -0500

    respond from cache if server isn't fast enough
---
 www/board/agenda/views/sw.js.rb | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/www/board/agenda/views/sw.js.rb b/www/board/agenda/views/sw.js.rb
index 50897d2..9951284 100644
--- a/www/board/agenda/views/sw.js.rb
+++ b/www/board/agenda/views/sw.js.rb
@@ -40,8 +40,8 @@ self.addEventListener :fetch do |event|
         timeoutId = nil
 
         caches.open('board/agenda').then do |cache|
-          # respond from cache if the server isn't fast enough
-          timeoutId = setTimeout timeout do
+          # common logic to reply from cache
+          replyFromCache = lambda do
             timeoutId = nil
             cache.match(request).then do |response|
               if response
@@ -52,22 +52,25 @@ self.addEventListener :fetch do |event|
             end
           end
 
+          # respond from cache if the server isn't fast enough
+          timeoutId = setTimeout(replyFromCache, timeout)
+
           # fetch bootstrap.html
           fetch(request).then {|response|
-            # cache the response if OK, fulfull the response if not timed out
+            # cache the response if OK, fulfill the response if not timed out
             if response.ok
               cache.put(request, response.clone())
               if timeoutId
                 clearTimeout timeoutId 
                 fulfill response
               end
+            else
+              # bad response: use cache instead
+              replyFromCache()
             end
           }.catch {|failure|
-            # fetch rejected before the timeout
-            if timeoutId
-              clearTimeout timeoutId 
-              reject failure 
-            end
+            # no response: use cache instead
+            replyFromCache()
           }
         end
       end

-- 
To stop receiving notification emails like this one, please contact
['"commits@whimsical.apache.org" <commits@whimsical.apache.org>'].

Mime
View raw message