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: locally cache recent comments and jira
Date Fri, 08 Jul 2016 14:10:50 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

The following commit(s) were added to refs/heads/master by this push:
       new  c6cd86c   locally cache recent comments and jira
c6cd86c is described below

commit c6cd86c32da22fb5b91a86476e2a808b39eb66b9
Author: Sam Ruby <rubys@intertwingly.net>
AuthorDate: Fri Jul 8 10:10:21 2016 -0400

    locally cache recent comments and jira
    
    firefox and chrome only at the moment
---
 www/board/agenda/views/models/comments.js.rb    |  8 ++--
 www/board/agenda/views/models/jira.js.rb        |  8 ++--
 www/board/agenda/views/models/jsonstorage.js.rb | 59 ++++++++++++++++++++++++-
 3 files changed, 66 insertions(+), 9 deletions(-)

diff --git a/www/board/agenda/views/models/comments.js.rb b/www/board/agenda/views/models/comments.js.rb
index b820114..8730a83 100644
--- a/www/board/agenda/views/models/comments.js.rb
+++ b/www/board/agenda/views/models/comments.js.rb
@@ -9,10 +9,10 @@ class HistoricalComments
   def self.find(title)
     if @@comments
       return @@comments[title]
-    elsif defined? XMLHttpRequest
-      @@comments = JSONStorage.get('comments') || {}
-      retrieve('historical-comments', :json) do |comments|
-        @@comments = JSONStorage.put('comments', comments) if comments
+    else
+      @@comments = {}
+      JSONStorage.fetch('historical-comments') do |comments|
+        @@comments = comments
       end
     end
   end
diff --git a/www/board/agenda/views/models/jira.js.rb b/www/board/agenda/views/models/jira.js.rb
index c3dc1ee..915fbc3 100644
--- a/www/board/agenda/views/models/jira.js.rb
+++ b/www/board/agenda/views/models/jira.js.rb
@@ -8,10 +8,10 @@ class JIRA
   def self.find(name)
     if @@list
       return @@list.include? name
-    elsif defined? XMLHttpRequest
-      @@list = JSONStorage.get('jira') || []
-      retrieve('jira', :json) do |list|
-        @@list = JSONStorage.put('jira', list) if list
+    else
+      @@list = []
+      JSONStorage.fetch 'jira' do |list|
+        @@list = list
       end
     end
   end
diff --git a/www/board/agenda/views/models/jsonstorage.js.rb b/www/board/agenda/views/models/jsonstorage.js.rb
index ccd06c7..2e0d69f 100644
--- a/www/board/agenda/views/models/jsonstorage.js.rb
+++ b/www/board/agenda/views/models/jsonstorage.js.rb
@@ -1,5 +1,8 @@
 #
-# Simplify access to sessionStorage for JSON objects
+# Originally defined to simplify access to sessionStorage for JSON objects.
+#
+# Now expanded to include caching using fetch and the cache defined in
+# the Service Workers specification (but without the user of SWs).
 #
 
 class JSONStorage
@@ -35,4 +38,58 @@ class JSONStorage
       return JSON.parse(sessionStorage.getItem(name) || 'null')
     end
   end
+
+  # retrieve an cached object.  Note: block may be dispatched twice,
+  # once with slightly stale data and once with current data
+  #
+  # Note: caches only work currently on Firefox and Chrome.  All
+  # other browsers fall back to XMLHttpRequest (AJAX).
+  def self.fetch(name, &block)
+
+    if defined? fetch and defined? caches
+      caches.open('board/agenda').then do |cache|
+        fetched = nil
+        clock_counter += 1
+
+        # construct arguments to fetch
+        args = {
+          method: 'get',
+          credentials: 'include',
+          headers: {'Accept' => 'application/json'},
+        }
+
+        # dispatch request
+        fetch("../json/#{name}", args).then do |response|
+          cache.put(name, response.clone())
+
+          response.json().then do |json| 
+            unless fetched and fetched.inspect == json.inspect
+              clock_counter -= 1 unless fetched
+              fetched = json
+              block(json) if json
+              Main.refresh()
+            end
+          end
+        end
+
+        # check cache
+        cache.match(name).then do |response|
+          if not fetched
+            response.json().then do |json| 
+              clock_counter -= 1
+              fetched = json
+              block(json) if json
+              Main.refresh()
+            end
+          end
+        end
+      end
+
+    elsif defined? XMLHttpRequest
+
+      # retrieve from the network only
+      retrieve name, :json, &block
+
+    end
+  end
 end

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

Mime
View raw message