Commit eb53a1ca authored by Yusei Tahara's avatar Yusei Tahara Committed by Romain Courteaud

[erp5_web_renderjs_ui] Update service worker code. Use lower case for non-global variables.

parent d114caa1
...@@ -243,13 +243,13 @@ ...@@ -243,13 +243,13 @@
client_id = event.clientId.toString(), client_id = event.clientId.toString(),
// CACHE_MAP is a temprary data store. // CACHE_MAP is a temprary data store.
// This should be kept until service worker stops. // This should be kept until service worker stops.
CACHE_KEY = CACHE_MAP[client_id], cache_key = CACHE_MAP[client_id],
ERP5JS_CACHE; erp5js_cache;
url.hash = ''; url.hash = '';
console.log("Client Id = " + client_id); console.log("Client Id = " + client_id);
if (CACHE_KEY) { if (cache_key) {
console.log("CACHE_KEY from CACHE_MAP " + CACHE_KEY); console.log("cache_key from CACHE_MAP " + cache_key);
} }
if (!client_id) { if (!client_id) {
...@@ -257,9 +257,9 @@ ...@@ -257,9 +257,9 @@
// use the CACHE_NAME that is defined in this service worker. // use the CACHE_NAME that is defined in this service worker.
// It means that even if there is a new Cache, but web browser // It means that even if there is a new Cache, but web browser
// uses the Cache that was installed by this service worker. // uses the Cache that was installed by this service worker.
CACHE_KEY = CACHE_NAME; cache_key = CACHE_NAME;
CACHE_MAP[client_id] = CACHE_KEY; CACHE_MAP[client_id] = cache_key;
console.log("CACHE_KEY from Service Worker " + CACHE_KEY); console.log("cache_key from Service Worker " + cache_key);
} }
if ((event.request.method !== 'GET') || if ((event.request.method !== 'GET') ||
(required_url_list.indexOf(url.toString()) === -1)) { (required_url_list.indexOf(url.toString()) === -1)) {
...@@ -270,50 +270,50 @@ ...@@ -270,50 +270,50 @@
return event.respondWith( return event.respondWith(
Promise.resolve() Promise.resolve()
.then(function () { .then(function () {
if (!CACHE_KEY) { if (!cache_key) {
// CLIENT_CACHE_MAPPING_NAME stores CACHE_KEY of each client. // CLIENT_CACHE_MAPPING_NAME stores cache_key of each client.
return caches.open(CLIENT_CACHE_MAPPING_NAME) return caches.open(CLIENT_CACHE_MAPPING_NAME)
.then(function (erp5js_cache) { .then(function (cache) {
// Service worker forget everything when it stops. So, when it started // Service worker forget everything when it stops. So, when it started
// again, CACHE_MAP is empty, get the associated CACHE_KEY from the // again, CACHE_MAP is empty, get the associated cache_key from the
// special Cache named CLIENT_CACHE_MAPPING_NAME. // special Cache named CLIENT_CACHE_MAPPING_NAME.
ERP5JS_CACHE = erp5js_cache; erp5js_cache = cache;
return erp5js_cache.match(client_id) return erp5js_cache.match(client_id)
.then(function (response) { .then(function (response) {
if (response) { if (response) {
// We use Cache Storage as a persistent database. // We use Cache Storage as a persistent database.
CACHE_KEY = response.statusText; cache_key = response.statusText;
CACHE_MAP[client_id] = CACHE_KEY; CACHE_MAP[client_id] = cache_key;
console.log("CACHE_KEY from Cache Storage " + CACHE_KEY); console.log("cache_key from Cache Storage " + cache_key);
} }
}) });
}) });
} }
}) })
.then(function () { .then(function () {
if (!CACHE_KEY) { if (!cache_key) {
// If associated CACHE_KEY is not found, it means this client is a new one. // If associated cache_key is not found, it means this client is a new one.
// Let's find the latest Cache. // Let's find the latest Cache.
return caches.keys() return caches.keys()
.then(function (keys) { .then(function (keys) {
keys = keys.filter(function (key) {return key.startsWith(prefix); }); keys = keys.filter(function (key) {return key.startsWith(prefix); });
console.log("KEYS = " + keys); console.log("KEYS = " + keys);
if (keys.length) { if (keys.length) {
CACHE_KEY = keys.sort().reverse()[0]; cache_key = keys.sort().reverse()[0];
CACHE_MAP[client_id] = CACHE_KEY; CACHE_MAP[client_id] = cache_key;
} else { } else {
CACHE_KEY = CACHE_NAME; cache_key = CACHE_NAME;
CACHE_MAP[client_id] = CACHE_NAME; CACHE_MAP[client_id] = CACHE_NAME;
} }
// Save the associated CACHE_KEY in a persistent database because service // Save the associated cache_key in a persistent database because service
// worker forget everything when it stops. // worker forget everything when it stops.
ERP5JS_CACHE.put(client_id, new Response(null, {"statusText": CACHE_KEY})); erp5js_cache.put(client_id, new Response(null, {"statusText": cache_key}));
}) });
} }
}) })
.then(function () { .then(function () {
// Finally we have the associated CACHE_KEY. Let's find a cached response. // Finally we have the associated cache_key. Let's find a cached response.
return caches.open(CACHE_KEY); return caches.open(cache_key);
}) })
.then(function (cache) { .then(function (cache) {
// Don't give request object itself. Firefox's Cache Storage // Don't give request object itself. Firefox's Cache Storage
...@@ -328,7 +328,7 @@ ...@@ -328,7 +328,7 @@
} }
// Not in cache - return the result from the live server // Not in cache - return the result from the live server
// `fetch` is essentially a "fallback" // `fetch` is essentially a "fallback"
console.log("MISS " + CACHE_KEY + " " + url); console.log("MISS " + cache_key + " " + url);
return fetch(event.request); return fetch(event.request);
}) })
); );
......
...@@ -228,7 +228,7 @@ ...@@ -228,7 +228,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>976.15605.32654.49476</string> </value> <value> <string>976.15722.43793.37154</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -246,7 +246,7 @@ ...@@ -246,7 +246,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1559924528.42</float> <float>1559931548.14</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment