Commit 0028c318 authored by Boris Kocherov's avatar Boris Kocherov

erp5_officejs: serviceworker download only new changed files

parent 22add6fd
/*jslint indent: 2*/ /*jslint indent: 2*/
/*global self, caches, importScripts, fetch, Promise, Request, Response, jIO, console*/ /*global self, caches, importScripts, fetch, Promise, Request, Response, jIO, console, Headers*/
var global = self, var global = self,
window = self; window = self;
(function (self, caches, fetch) { (function (self, caches, fetch) {
...@@ -12,10 +12,14 @@ var global = self, ...@@ -12,10 +12,14 @@ var global = self,
importScripts('rsvp.js', 'jiodev.js'); importScripts('rsvp.js', 'jiodev.js');
Response.prototype.metadata = function () { Response.prototype.metadata = function () {
var headers = {};
Array.from(this.headers.entries()).forEach(function (data) {
headers[data[0]] = data[1];
});
return { return {
//'ok': this.ok //'ok': this.ok
url: this.url, url: this.url,
headers: Array.from(this.headers.entries()) headers: headers
}; };
}; };
...@@ -41,21 +45,60 @@ var global = self, ...@@ -41,21 +45,60 @@ var global = self,
// debugger; // debugger;
// Perform install step: loading each required file into cache // Perform install step: loading each required file into cache
// create a new jio // create a new jio
var requests = self.CACHE_REQUIRED_FILES.map(function (file_name) { // TODO:
return new Request(file_name); // delete from cache not existing in self.CACHE_REQUIRED_FILES files.
}); var result_fetch = Promise.resolve(), // queue for fetching files
result_jio_save = Promise.resolve(); // queue for save files
// in end service mark as installed
event.waitUntil( event.waitUntil(
Promise.all(requests.map(function (request) { new Promise(function (resolve, reject) {
return fetch(request).then(function (response) { self.CACHE_REQUIRED_FILES.map(function (url, i) {
return response.metadata_w_blob(); var request = new Request(url),
requests_len = self.CACHE_REQUIRED_FILES.length - 1,
jio_key = request.url;
// files download one by one
result_fetch = result_fetch.then(function () {
return new Promise(function (resolve) {
self.jio_cache_storage.get(jio_key)
.push(function (metadata) {
resolve(metadata);
})
.push(undefined, resolve);
}); });
})) })
.then(function (responses) { .then(function (metadata) {
return Promise.all( if (metadata.url !== undefined) {
responses.map(function (response, i) { var modification_mark_exist = false;
return new Promise(function (resolve, reject) { if (metadata.headers.ETag !== undefined) {
var jio_key = requests[i].url; modification_mark_exist = true;
request.headers.append('If-None-Match', metadata.headers.Etag);
} else if (metadata.headers['last-modified'] !== undefined) {
modification_mark_exist = true;
request.headers.append('If-Modified-Since', metadata.headers['last-modified']);
}
if (modification_mark_exist)
return fetch(request)
.then(undefined, function () {
// We say 'file not changed' if downloading is not possible with additional header
return new Response(null, {status: 304});
});
}
return fetch(request);
})
.then(function (response) {
if (response.status === 304) {
if (i === requests_len) {
// latest file saved
result_jio_save = result_jio_save.then(resolve);
}
return Promise.resolve();
} else {
return response.metadata_w_blob()
.then(function (response) {
// files save one by one
result_jio_save = result_jio_save.then(function () {
return new Promise(function (resolve) {
self.jio_cache_storage.put(jio_key, response.metadata) self.jio_cache_storage.put(jio_key, response.metadata)
.push(function () { .push(function () {
return self.jio_cache_storage.putAttachment( return self.jio_cache_storage.putAttachment(
...@@ -72,16 +115,23 @@ var global = self, ...@@ -72,16 +115,23 @@ var global = self,
console.log(error); console.log(error);
reject(); reject();
}); });
});
});
if (i === requests_len) {
// latest file saved
result_jio_save = result_jio_save.then(resolve);
}
return Promise.resolve();
});
}
});
});
}) })
.then(undefined, console.log);
})
)
.then(function () { .then(function () {
console.log('cache loaded'); console.log('cache loaded');
return self.skipWaiting(); return self.skipWaiting();
}) })
.then(undefined, console.log); .then(undefined, console.log)
})
); );
}; };
...@@ -92,7 +142,6 @@ var global = self, ...@@ -92,7 +142,6 @@ var global = self,
.push(function (metadata) { .push(function (metadata) {
return self.jio_cache_storage.getAttachment(jio_key, 'body') return self.jio_cache_storage.getAttachment(jio_key, 'body')
.push(function (body) { .push(function (body) {
//console.log(metadata.url + ' return from jio');
resolve(new Response(body, {'headers': metadata.headers})); resolve(new Response(body, {'headers': metadata.headers}));
}); });
}) })
......
...@@ -230,7 +230,7 @@ ...@@ -230,7 +230,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>951.9272.8708.15735</string> </value> <value> <string>951.20619.45573.30156</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -248,7 +248,7 @@ ...@@ -248,7 +248,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1463082861.33</float> <float>1463735862.94</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