You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@whimsical.apache.org by ru...@apache.org on 2017/10/13 13:39:07 UTC

[whimsy] branch master updated: use latest bootstrap if the server responds fast enough

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 b5964ec  use latest bootstrap if the server responds fast enough
b5964ec is described below

commit b5964ec4147110b0aff10a0b15d583274be6ea07
Author: Sam Ruby <ru...@intertwingly.net>
AuthorDate: Fri Oct 13 09:38:25 2017 -0400

    use latest bootstrap if the server responds fast enough
---
 www/board/agenda/views/models/agenda.js.rb |  1 +
 www/board/agenda/views/sw.js.rb            | 49 ++++++++++++++++++++++++++----
 2 files changed, 44 insertions(+), 6 deletions(-)

diff --git a/www/board/agenda/views/models/agenda.js.rb b/www/board/agenda/views/models/agenda.js.rb
index 575287b..c01f9a5 100644
--- a/www/board/agenda/views/models/agenda.js.rb
+++ b/www/board/agenda/views/models/agenda.js.rb
@@ -8,6 +8,7 @@ class Agenda
   Vue.util.defineReactive @@index, []
   @@etag = nil
   @@digest = nil
+  Vue.util.defineReactive @@date, ''
 
   # (re)-load an agenda, creating instances for each item, and linking
   # each instance to their next and previous items.
diff --git a/www/board/agenda/views/sw.js.rb b/www/board/agenda/views/sw.js.rb
index 6580dbc..4d7fde6 100644
--- a/www/board/agenda/views/sw.js.rb
+++ b/www/board/agenda/views/sw.js.rb
@@ -1,11 +1,15 @@
 #
 # A very simple service worker
 #
-#   *) Return back cached bootstrap page instead of fetching agenda pages
-#      from the network.  Bootstrap will construct page from cached
-#      agenda.json, as well as update the cache.
+#   *) Replace calls to fetch an agenda page with calls to fetch a bootstrap
+#      page.   This page will reconstruct the page requested using cached
+#      data and then request fresh data.  If the server doesn't respond
+#      with 0.5 seconds or fails, return a cached version of the bootstrap
+#      page.
 # 
 
+timeout = 500
+
 # install immediately
 self.addEventListener :install do
   return self.skipWaiting()
@@ -24,11 +28,44 @@ self.addEventListener :fetch do |event|
 
   if url =~ %r{^\d\d\d\d-\d\d-\d\d/} and event.request.method == 'GET'
     return if url.end_with? '/bootstrap.html'
+
     event.respondWith(
-      caches.open('board/agenda').then do |cache|
+      Promise.new do |fulfill, reject|
         date =  url.split('/')[0]
-        return cache.match("#{date}/bootstrap.html").then do |response|
-          return response || fetch(event.request.url, credentials: 'include')
+        bootstrap = "#{date}/bootstrap.html"
+        request = Request.new(bootstrap)
+        error = nil
+
+        caches.open('board/agenda').then do |cache|
+          # respond from cache if the server isn't fast enough
+          timeoutId = setTimeout timeout do
+            timeoutId = nil
+            cache.match(request).then do |response|
+              if response
+                fulfill response
+              else
+                fetch(event.request).then(fulfill, reject)
+              end
+            end
+          end
+
+          # fetch bootstrap.html
+          fetch(request).then {|response|
+            # cache the response if OK, fulfull the response if not timed out
+            if response.ok
+              cache.put(request, response.clone())
+              if timeoutId
+                clearTimeout timeoutId 
+                fulfill response
+              end
+            end
+          }.catch {|failure|
+            # fetch rejected before the timeout
+            if timeoutId
+              clearTimeout timeoutId 
+              reject failure 
+            end
+          }
         end
       end
     )

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