# HG changeset patch
# User Ranjith S R <ranjith@qualiantech.com>
# Date 1582196782 -19800
#      Thu Feb 20 16:36:22 2020 +0530
# Node ID 3cdcef2252e237b40ad0339e4885906a4804191e
# Parent  da8793cc06b960e6bc0b96d38a134b3e7c418502
Fixed issue 43266 : Reloading All Cache files if anyone is missing in Cache

* When loading POS, if old and new manifest are same, manifest files should exist in chrome cache.
  If it is not exists, all Cache files should be reload again from the server
* Main Window should be rendered when ServiceController is not activated
  When user refresh POS with Shift + Refresh, ServiceController will not be initiated and activated.
  But Cache Validation will be initiated due to New Manifest and Old Manifest are same.
  So, Main window should be rendered by calling the callback
  when ServiceController is not activated and Cache Validation is enabled

diff -r da8793cc06b9 -r 3cdcef2252e2 web/org.openbravo.mobile.core/source/offline/ob-fetch-manifest.js
--- a/web/org.openbravo.mobile.core/source/offline/ob-fetch-manifest.js	Wed Feb 19 09:01:25 2020 +0000
+++ b/web/org.openbravo.mobile.core/source/offline/ob-fetch-manifest.js	Thu Feb 20 16:36:22 2020 +0530
@@ -13,18 +13,22 @@
   successNoRefreshCallback,
   errorCallback
 ) {
-  var setManifest = false;
-  var manifest;
   //make sure that Service Workers are supported.
-  var url =
-    '../../org.openbravo.mobile.core/OBPOS_Main/AppCacheManifest?_appName=WebPOS';
-  var oldManifest;
+
+  var setManifest = false,
+    manifest,
+    manifestType,
+    url =
+      '../../org.openbravo.mobile.core/OBPOS_Main/AppCacheManifest?_appName=WebPOS',
+    oldManifest;
 
   function send_message_to_sw() {
     var appData = {};
+    appData.type = manifestType;
     appData.manifest = manifest;
     appData.appName = OB.MobileApp.model.get('appName');
     navigator.serviceWorker.controller.postMessage(appData);
+    manifestType = null;
   }
 
   fetch(url)
@@ -42,6 +46,7 @@
       return responseClone.text().then(function(text) {
         manifest = text;
         oldManifest = OB.UTIL.localStorage.getItem('manifest');
+        manifestType = null;
         if (oldManifest === null) {
           OB.info('[Manifest] Dont have manifest yet');
           setManifest = true;
@@ -50,8 +55,9 @@
             OB.info('[Manifest] Taking the meanifest new version');
             setManifest = true;
           } else {
-            OB.info('[Manifest] Manifest is still up to date');
-            successNoRefreshCallback();
+            OB.info('[Manifest] Validating Manifest in Cache');
+            manifestType = 'validate';
+            setManifest = true;
           }
         }
       });
@@ -91,6 +97,8 @@
         setManifest === true
       ) {
         send_message_to_sw();
+      } else if (setManifest === true && manifestType === 'validate') {
+        successNoRefreshCallback();
       }
     })
     ['catch'](function() {
@@ -162,6 +170,15 @@
             errorCallback();
           }
           break;
+        case 'validate':
+          if (e.data.result) {
+            OB.info('[Manifest] Manifest is still up to date');
+            successNoRefreshCallback();
+          } else {
+            OB.info('[Manifest] Loading Manifest again');
+            send_message_to_sw();
+          }
+          break;
       }
     });
   }
diff -r da8793cc06b9 -r 3cdcef2252e2 web/org.openbravo.mobile.core/source/offline/ob-sources-sw.js
--- a/web/org.openbravo.mobile.core/source/offline/ob-sources-sw.js	Wed Feb 19 09:01:25 2020 +0000
+++ b/web/org.openbravo.mobile.core/source/offline/ob-sources-sw.js	Thu Feb 20 16:36:22 2020 +0530
@@ -209,6 +209,32 @@
 self.addEventListener('message', function(e) {
   receivedManifest = e.data.manifest;
   cacheName = e.data.appName;
+  if (e.data.type === 'validate') {
+    var cacheList = [];
+    caches.open('WebPOS').then(function(cache) {
+      cacheFiles.forEach(function(cachePath) {
+        cacheList.push(
+          new Promise(function(res, rej) {
+            cache.match(cachePath).then(function(response) {
+              res(response ? true : false);
+            });
+          })
+        );
+      });
+      Promise.all(cacheList).then(function(values) {
+        this.clients.matchAll().then(function(clients) {
+          clients.forEach(function(client) {
+            client.postMessage({
+              status: 'validate',
+              result: values.filter(Boolean).length === cacheFiles.length
+            });
+          });
+        });
+      });
+    });
+    return;
+  }
+  cacheFiles.length = 1;
   caches['delete'](cacheName).then(function() {
     var messageFetch =
       '[Service Worker] Fetching manifest: ' + receivedManifest;
