﻿TC.control = TC.control || {};

if (!TC.Control) {
    TC.syncLoadJS(TC.apiLocation + 'TC/Control');
}

(function () {

    TC.control.SWCacheClient = function () {
        const self = this;
        TC.Control.apply(this, arguments);
        self.serviceWorkerEnabled = false;
    };

    TC.inherit(TC.control.SWCacheClient, TC.Control);

    var ctlProto = TC.control.SWCacheClient.prototype;

    ctlProto.CLASS = 'tc-ctl-swcc';

    ctlProto.register = function (map) {
        const self = this;

        const result = TC.Control.prototype.register.call(self, map);

        // Si el navegador es compatible, añadimos el service worker.
        self._swPromise = new Promise(function (resolve, reject) {
            if (navigator.serviceWorker) {

                navigator.serviceWorker.register('tc-cb-service-worker.js', {
                    scope: './'
                }).then(
                    function (reg) {
                        self.serviceWorkerEnabled = true;
                        if (reg.installing) {
                            resolve(reg.installing);
                        } else if (reg.waiting) {
                            resolve(reg.waiting);
                        } else if (reg.active) {
                            resolve(reg.active);
                        }
                        console.log(reg.scope, 'register');
                    },
                    function (reason) {
                        self.serviceWorkerEnabled = false;
                        reject();
                        console.error('Could not register service worker: ' + reason);
                    });
            }
            else {                
                reject(new Error("Browser does not support service workers"));
            }
        });

        return result;
    };

    ctlProto.getServiceWorker = function () {
        if (!this._swPromise) {
            return Promise.reject(new Error('No service worker available'));
        }
        return this._swPromise;
    };

    var addMessageEventListener = function (resolve, reject, cacheName, action, eventName) {
        var MESSAGE = 'message';
        var messageHandler = function messageHandler(event) {
            if (event.data.name === cacheName) {
                if (event.data.action === action && event.data.event === eventName) {
                    resolve(cacheName);
                }
                else if (event.data.event === 'error') {
                    reject();
                }
                navigator.serviceWorker.removeEventListener(MESSAGE, messageHandler);
            }
        };
        navigator.serviceWorker.addEventListener(MESSAGE, messageHandler);
    };

    ctlProto.createCache = function (name, options) {
        var self = this;
        return new Promise(function (resolve, reject) {
            self.getServiceWorker().then(function (sw) {
                var ACTION = 'create';
                var opts = options || {};
                addMessageEventListener(resolve, reject, name, ACTION, 'cached');
                sw.postMessage({
                    action: ACTION,
                    name: name,
                    list: opts.urlList || [],
                    silent: opts.silent
                });
            }, function () {
                resolve(false);
            });
        });
    };

    ctlProto.deleteCache = function (name, options) {
        var self = this;
        return new Promise(function (resolve, reject) {
            self.getServiceWorker().then(function (sw) {
                var ACTION = 'delete';
                var opts = options || {};
                addMessageEventListener(resolve, reject, name, ACTION, 'deleted');
                sw.postMessage({
                    action: ACTION,
                    name: name,
                    silent: opts.silent
                });
            }, function () {
                resolve(false);
            });
        });
    };

})();

@@ -445,16 +445,66 @@
 -swcc';%0D
+%0A    ctlProto.SW_URL = 'tc-cb-service-worker.js';%0D
 %0A%0D%0A    c
@@ -881,33 +881,19 @@
 ter(
-'tc-cb-service-worker.js'
+self.SW_URL
 , %7B%0D
@@ -1588,32 +1588,49 @@
          reject(
+new Error(reason)
 );%0D%0A            
@@ -1887,24 +1887,463 @@
      %7D);%0D%0A%0D%0A
+        self._swPromise.catch(() =%3E %7B%0D%0A            if (location.protocol !== 'https:') %7B%0D%0A                map.toast(self.getLocaleString('httpsRequired.warning', %7B url: location.href.replace(location.protocol, '') %7D), %7B type: TC.Consts.msgType.WARNING %7D);%0D%0A            %7D%0D%0A            else %7B%0D%0A                map.toast(self.getLocaleString('browserNotCompatible.warning'), %7B type: TC.Consts.msgType.WARNING %7D);%0D%0A            %7D%0D%0A        %7D);%0D%0A
         retu
@@ -3049,39 +3049,205 @@
  reject(
-);%0D%0A                %7D%0D%0A
+Error(%60Error message from service worker %5B$%7Bevent.data.url%7D - $%7Bevent.data.action%7D - $%7Bevent.data.name%7D%5D%60));%0D%0A                %7D%0D%0A                if (event.data.event !== 'progress') %7B%0D%0A    
         
@@ -3321,24 +3321,43 @@
 eHandler);%0D%0A
+                %7D%0D%0A
             

Add SW compatibility warnings