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

erp5_officejs: serviceworker download only new changed files

parent 22add6fd
/*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,
window = self;
(function (self, caches, fetch) {
......@@ -12,10 +12,14 @@ var global = self,
importScripts('rsvp.js', 'jiodev.js');
Response.prototype.metadata = function () {
var headers = {};
Array.from(this.headers.entries()).forEach(function (data) {
headers[data[0]] = data[1];
});
return {
//'ok': this.ok
url: this.url,
headers: Array.from(this.headers.entries())
headers: headers
};
};
......@@ -41,47 +45,93 @@ var global = self,
// debugger;
// Perform install step: loading each required file into cache
// create a new jio
var requests = self.CACHE_REQUIRED_FILES.map(function (file_name) {
return new Request(file_name);
});
// TODO:
// 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(
Promise.all(requests.map(function (request) {
return fetch(request).then(function (response) {
return response.metadata_w_blob();
});
}))
.then(function (responses) {
return Promise.all(
responses.map(function (response, i) {
return new Promise(function (resolve, reject) {
var jio_key = requests[i].url;
self.jio_cache_storage.put(jio_key, response.metadata)
.push(function () {
return self.jio_cache_storage.putAttachment(
jio_key,
"body",
response.blob
)
.push(function () {
console.log('jio_save: ' + jio_key);
resolve();
});
new Promise(function (resolve, reject) {
self.CACHE_REQUIRED_FILES.map(function (url, i) {
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, function (error) {
console.log(error);
reject();
});
})
.then(undefined, console.log);
.push(undefined, resolve);
});
})
)
.then(function () {
console.log('cache loaded');
return self.skipWaiting();
})
.then(undefined, console.log);
.then(function (metadata) {
if (metadata.url !== undefined) {
var modification_mark_exist = false;
if (metadata.headers.ETag !== undefined) {
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)
.push(function () {
return self.jio_cache_storage.putAttachment(
jio_key,
"body",
response.blob
)
.push(function () {
console.log('jio_save: ' + jio_key);
resolve();
});
})
.push(undefined, function (error) {
console.log(error);
reject();
});
});
});
if (i === requests_len) {
// latest file saved
result_jio_save = result_jio_save.then(resolve);
}
return Promise.resolve();
});
}
});
});
})
.then(function () {
console.log('cache loaded');
return self.skipWaiting();
})
.then(undefined, console.log)
);
};
......@@ -92,7 +142,6 @@ var global = self,
.push(function (metadata) {
return self.jio_cache_storage.getAttachment(jio_key, 'body')
.push(function (body) {
//console.log(metadata.url + ' return from jio');
resolve(new Response(body, {'headers': metadata.headers}));
});
})
......
......@@ -230,7 +230,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>951.9272.8708.15735</string> </value>
<value> <string>951.20619.45573.30156</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -248,7 +248,7 @@
</tuple>
<state>
<tuple>
<float>1463082861.33</float>
<float>1463735862.94</float>
<string>UTC</string>
</tuple>
</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