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

commit 66cea4cb6fe8fb44734e5e1dd32b556d44f0b623
Author: Sam Ruby <[email protected]>
AuthorDate: Tue Jun 5 19:17:58 2018 -0400

    move to latest agenda if the current one isn't the latest
    
    Background: client will optimistically show the latest agenda it knows about
    and then inquire to the server to see if it is correct.
---
 www/board/agenda/views/layout/main.js.rb      |  3 ++
 www/board/agenda/views/models/pagecache.js.rb | 40 ++++++++++++++++++++++++---
 www/board/agenda/views/sw.js.rb               | 10 +++----
 3 files changed, 44 insertions(+), 9 deletions(-)

diff --git a/www/board/agenda/views/layout/main.js.rb 
b/www/board/agenda/views/layout/main.js.rb
index d63c636..7d54767 100644
--- a/www/board/agenda/views/layout/main.js.rb
+++ b/www/board/agenda/views/layout/main.js.rb
@@ -82,6 +82,7 @@ class Main < Vue
     self.route(path, query)
     history.pushState({path: path, query: query}, nil, path)
     window.onresize()
+    Main.latest = false if path
   end
 
   # refresh the current page
@@ -99,6 +100,7 @@ class Main < Vue
     Main.navigate = self.navigate
     Main.refresh  = self.refresh
     Main.view  = $refs.view
+    Main.item = Agenda
 
     # store initial state in history, taking care not to overwrite
     # history set by the Search component.
@@ -111,6 +113,7 @@ class Main < Vue
         if path.start_with? base
           path = path.slice(base.length) 
         elsif path.end_with? '/latest/'
+          Main.latest = true
           path = '.'
         end
       end
diff --git a/www/board/agenda/views/models/pagecache.js.rb 
b/www/board/agenda/views/models/pagecache.js.rb
index 873fc66..0d0b0e1 100644
--- a/www/board/agenda/views/models/pagecache.js.rb
+++ b/www/board/agenda/views/models/pagecache.js.rb
@@ -45,11 +45,13 @@ class PageCache
     navigator.serviceWorker.register(swjs, scope).then do
       # watch for reload requests from the service worker
       navigator.serviceWorker.addEventListener 'message' do |event|
-        if event.data.type == 'reload'
-          # ignore reload request if any input or textarea element is visible
-          inputs = document.querySelectorAll('input, textarea')
-          unless Array(inputs).map {|element| element.offsetWidth}.max() > 0
+        # ignore requests if any input or textarea element is visible
+        inputs = document.querySelectorAll('input, textarea')
+        unless Array(inputs).map {|element| element.offsetWidth}.max() > 0
+          if event.data.type == 'reload'
             window.location.reload() 
+          elsif event.data.type == 'latest' and Main.latest
+            self.latest(event.data.body)
           end
         end
       end
@@ -61,5 +63,35 @@ class PageCache
           url: base + 'bootstrap.html'
       end
     end
+
+    if Main.item == Agenda and Main.latest
+      fetch('bootstrap.html').then do |response|
+        response.text().then do |body|
+          self.latest(body)
+        end
+      end
+    end
+  end
+
+  # if the entry point URL is /latest/, the service worker will optimistically
+  # show the latest known agenda. If it turns out that there is a later one,
+  # refresh with that page.
+  def self.latest(body)
+    # ignore requests if any input or textarea element is visible
+    inputs = document.querySelectorAll('input, textarea')
+    return if Array(inputs).map {|element| element.offsetWidth}.max() > 0
+
+    latest = nil
+    data = body[/"agendas":\[.*?\]/]
+
+    agenda_re = Regexp.new('board_agenda_\d\d\d\d_\d\d_\d\d.txt', 'g')
+    while agenda = agenda_re.exec(data)
+      latest = agenda[0] unless latest and latest > agenda[0]
+    end
+
+    if latest and latest != Agenda.file
+      date = latest[/\d\d\d\d_\d\d_\d\d/].gsub('_', '-')
+      window.location.href = "../#{date}/"
+    end
   end
 end
diff --git a/www/board/agenda/views/sw.js.rb b/www/board/agenda/views/sw.js.rb
index 72e614f..e24b437 100644
--- a/www/board/agenda/views/sw.js.rb
+++ b/www/board/agenda/views/sw.js.rb
@@ -54,10 +54,10 @@ def cache_replace(cache, request, response)
 end
 
 # broadcast a message to all clients
-def broadcast(type)
+def broadcast(message)
   clients.matchAll().then do |clients|
     clients.each do |client|
-      client.postMessage(type: type)
+      client.postMessage(message)
     end
   end
 end
@@ -80,7 +80,7 @@ def preload(cache, base, text, toolate)
         fetch(request).then do |response|
           cache_replace(cache, request, response) if response.ok
           count -= 1
-          broadcast(:reload) if toolate and changed and count == 0
+          broadcast(type: 'reload') if toolate and changed and count == 0
         end
       end
     end
@@ -120,9 +120,9 @@ def latest(event)
             request = Request.new(match.url, cache: "no-store")
             fetch(request).then do |response| 
               if response.ok
-                response.match().text().then do |after|
+                response.clone().text().then do |after|
                   cache.put request, response
-                  broadcast(:latest) if after != before
+                  broadcast(type: 'latest', body: after) # if after != before
                 end
               end
             end

-- 
To stop receiving notification emails like this one, please contact
[email protected].

Reply via email to