whimsical-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ru...@apache.org
Subject [whimsy] 02/02: fetch and cache agenda json
Date Tue, 26 Jul 2016 01:47:18 GMT
This is an automated email from the ASF dual-hosted git repository.

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

commit 161d230378a802f067ae61667a521248f4f609f1
Author: Sam Ruby <rubys@intertwingly.net>
AuthorDate: Mon Jul 25 21:45:13 2016 -0400

    fetch and cache agenda json
---
 www/board/agenda/views/models/agenda.js.rb | 62 +++++++++++++++++++++++++-----
 1 file changed, 52 insertions(+), 10 deletions(-)

diff --git a/www/board/agenda/views/models/agenda.js.rb b/www/board/agenda/views/models/agenda.js.rb
index e35c45e..49a9350 100644
--- a/www/board/agenda/views/models/agenda.js.rb
+++ b/www/board/agenda/views/models/agenda.js.rb
@@ -58,18 +58,60 @@ class Agenda
     if etag
       @@etag = etag
     elsif digest != @@digest or not @@etag
-      xhr = XMLHttpRequest.new()
-      xhr.open('GET', "../#{@@date}.json", true)
-      xhr.setRequestHeader('If-None-Match', @@etag) if @@etag
-      xhr.responseType = 'text'
-      def xhr.onreadystatechange()
-        if xhr.readyState == 4 and xhr.status == 200 and xhr.responseText != ''
-          @@etag = xhr.getResponseHeader('ETag')
-          Agenda.load(JSON.parse(xhr.responseText))
-          Main.refresh()
+      if PageCache.enabled
+        loaded = false
+
+        # if bootstrapping and cache is available, load it
+        if not digest
+          caches.open('board/agenda').then do |cache|
+            cache.match("#{@@date}.json").then do |response|
+              if response
+                response.json().then do |json| 
+                  Agenda.load(json) unless loaded
+                  Main.refresh()
+                end
+              end
+            end
+          end
         end
+
+        # set fetch options: credentials and etag
+        options = {credentials: 'include'}
+        options['headers'] = {'If-None-Match' => @@etag} if @@etag
+
+        # perform fetch
+        fetch("../#{@@date}.json", options).then do |response|
+          if response
+            loaded = true
+
+            # load response into the agenda
+            response.clone().json().then do |json| 
+              @@etag = response.headers.get('etag')
+              Agenda.load(json)
+              Main.refresh()
+            end
+
+            # save response in the cache
+            caches.open('board/agenda').then do |cache|
+              cache.put("#{@@date}.json", response)
+            end
+          end
+        end
+      else
+        # AJAX fallback
+        xhr = XMLHttpRequest.new()
+        xhr.open('GET', "../#{@@date}.json", true)
+        xhr.setRequestHeader('If-None-Match', @@etag) if @@etag
+        xhr.responseType = 'text'
+        def xhr.onreadystatechange()
+          if xhr.readyState==4 and xhr.status==200 and xhr.responseText!=''
+            @@etag = xhr.getResponseHeader('ETag')
+            Agenda.load(JSON.parse(xhr.responseText))
+            Main.refresh()
+          end
+        end
+        xhr.send()
       end
-      xhr.send()
     end
 
     @@digest = digest

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

Mime
View raw message