# HG changeset patch
# User Ranjith S R <ranjith@qualiantech.com>
# Date 1567596171 -19800
#      Wed Sep 04 16:52:51 2019 +0530
# Node ID 7ad1688ccd9d92cef59928b91a476478b6f82f90
# Parent  8734f3fe6fa77f550118ad0cce23310e8b257846
Related to issue 41671 : Reloading All Cache files if anyone is missing in Cache

diff -r 8734f3fe6fa7 -r 7ad1688ccd9d web/org.openbravo.mobile.core/source/offline/ob-fetch-manifest.js
--- a/web/org.openbravo.mobile.core/source/offline/ob-fetch-manifest.js	Tue Sep 03 11:06:19 2019 +0530
+++ b/web/org.openbravo.mobile.core/source/offline/ob-fetch-manifest.js	Wed Sep 04 16:52:51 2019 +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,10 @@
             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');
+            OB.UTIL.localStorage.removeItem('manifest');
+            manifestType = 'validate';
+            setManifest = true;
           }
         }
       });
@@ -162,6 +169,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 8734f3fe6fa7 -r 7ad1688ccd9d web/org.openbravo.mobile.core/source/offline/ob-sources-sw.js
--- a/web/org.openbravo.mobile.core/source/offline/ob-sources-sw.js	Tue Sep 03 11:06:19 2019 +0530
+++ b/web/org.openbravo.mobile.core/source/offline/ob-sources-sw.js	Wed Sep 04 16:52:51 2019 +0530
@@ -212,6 +212,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;
